nickshoe / asyncapi-php-template Goto Github PK
View Code? Open in Web Editor NEWAsyncAPI generator template for PHP applications.
License: Apache License 2.0
AsyncAPI generator template for PHP applications.
License: Apache License 2.0
Environment details
-PHP version: 8.1.16
Description
After the php code generation, it will be created a class named "AnonymousSchema" for every schema that inherits its properties from another one.
Example
I have two object-type schemas into the asyncapi .yaml file:
DomainEvent:
type: object
discriminator: _type
required:
- _type
- id
- createdAt
properties:
_type:
type: string
readOnly: true
id:
type: string
readOnly: true
createdAt:
type: string
format: date-time
example: '2012-04-23T18:25:43.511Z'
readOnly: true
UserPasswordResetEvent:
description: A user password reset event.
allOf:
- $ref: "#/components/schemas/DomainEvent"
- type: object
required:
- user
- tempPassword
properties:
user:
$ref: "#/components/schemas/User"
tempPassword:
type: string
In this example, "UserPasswordResetEvent" inherits its properties from "DomainEvent".
After generating the "UserPasswordResetEvent" php class, it will be created another php class named "AnonymousSchema8.class.php". The last one is identical to the first one, exept for missing the hereditariness from "AnonymousSchema8.class.php" and "DomainEvent.class.php"
Environment details
-PHP version: 8.1.16
-Message broker: RabbitMQ
Description
If I have a schema with an array type property, the code generation will not add the annotation for that one into the PHP class.
JMS will not be able to deserialize the array property during the subscription of the message and will give a PHP Fatal error.
Example
I have an object-type schema like this into the asyncapi .yaml file:
Email:
type: object
properties:
subject:
type: string
body:
type: string
recipients:
type: array
items:
type: string
From this schema, it will be generated a model called "Email.class.php" with these 3 properties:
<?php
namespace AsyncAPI\Models;
class Email
{
private string $subject;
private string $body;
private array $recipients;
...
...
...
Now, we publish an event with an "Email" object like this:
$event = new EmailEvent(
new Email(
'Password expired!',
'Hi, your password has been expired',
['[email protected]','[email protected]']
),
...
...
);
Then, If we try to subscribe the message from RabbitMQ, it will give this error:
PHP Fatal error: Uncaught JMS\Serializer\Exception\RuntimeException: You must define a type for AsyncAPI\Models\Email::$recipients.
Workaround
Add this code to the class. In this example, it will be the Email class:
<?php
namespace AsyncAPI\Models;
use JMS\Serializer\Annotation as Serializer; //this
class Email
{
private string $subject;
private string $body;
/**
* @Serializer\Type("array<string>") //and this
*/
private array $recipients;
...
...
...
Environment details
-PHP version: 8.1.16
Description
If I have a schema with a boolean type property, the code won't be generated. It will instead give an error in the console log.
Example
I have an object-type schema like this into the asyncapi .yaml file, with the "superUser" boolean property:
User:
type: object
required:
- id
- firstName
- lastName
- email
properties:
id:
type: integer
firstName:
type: string
lastName:
type: string
email:
type: string
format: email
dateOfBirth:
type: string
format: date
superUser:
type: boolean
If we go to the console log after launching the code generation prompt, we will see this error:
Something went wrong:
Error: Unhandled schema type 'boolean'.
at ClassHierarchyEvaluator.determineSchemaClass
As the error says, the boolean type is not handled in the "class-hierrachy-evaluator.js" file.
The generated datetime variables use the fully qualified name now, so the "Datetime" type is preceded by the "" character. It could be implemented the import of this class through the "use" statement.
It could be useful to expose a parameter during the generation code. This parameter should indicates if the application I'm generating has to publish or to subscribe the events/commands.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.