[PHP-SYMFONY] Debug Symfony version 7, enums ref, array of enums $ref and date assert (#19008)

* [PHP-SYMFONY] Debug for Symfony 7 support & debug enum ref & debug array enum ref & debug date assert

* [PHP-SYMFONY] Debug for Symfony 7 support & debug enum ref & debug array enum ref & debug date assert

* [PHP-SYMFONY] Debug for Symfony 7 support & debug enum ref & debug array enum ref & debug date assert
This commit is contained in:
loicconan 2024-06-30 04:11:26 +02:00 committed by GitHub
parent fa2b5750ce
commit fb17e5699d
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
12 changed files with 428 additions and 343 deletions

View File

@ -28,6 +28,7 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"doctrine/annotations": "^2.0",
"symfony/validator": "^6.4|^7.0",
"jms/serializer-bundle": "^5.4",
"symfony/framework-bundle": "^6.4|^7.0"

View File

@ -30,6 +30,20 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
return $this->{{name}};
}
/**
* Sets {{name}}.
*
* @param {{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
*
* @return $this
*/
public function {{setter}}({{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{name}}{{^required}} = null{{/required}}): self
{
$this->{{name}} = ${{name}};
return $this;
}
{{#isEnumRef}}
/**
* Gets {{name}} for serialization.
@ -40,6 +54,24 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
{
return $this->{{name}}?->value ? (string) $this->{{name}}->value : null;
}
/**
* Sets {{name}}.
*
* @param string|{{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
*
* @return $this
*/
public function setDeserialized{{nameInPascalCase}}(string|{{#vendorExtensions.x-comment-type}}{{vendorExtensions.x-comment-type}} {{/vendorExtensions.x-comment-type}}${{name}}{{^required}} = null{{/required}}): self
{
if (is_string(${{name}})) {
${{name}} = {{baseType}}::tryFrom(${{name}});
}
$this->{{name}} = ${{name}};
return $this;
}
{{/isEnumRef}}
{{#isContainer}}
@ -53,26 +85,36 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
public function getSerialized{{nameInPascalCase}}(): array
{
return array_map(
static fn ($value) => (string) $value->value,
$this->test ?? []
static fn ($value) => $value?->value ? (string) $value->value : null,
$this->{{name}} ?? []
);
}
/**
* Sets {{name}}.
*
* @param {{^required}}?{{/required}}array ${{name}}{{#description}} {{{.}}}{{/description}}
*
* @return $this
*/
public function setDeserialized{{nameInPascalCase}}({{^required}}?{{/required}}array ${{name}}{{^required}} = []{{/required}}): self
{
$this->{{name}} = array_map(
static function ($value) {
if (is_string($value)) {
$value = {{baseType}}::tryFrom($value);
}
return $value;
},
${{name}} ?? []
);
return $this;
}
{{/isEnumRef}}
{{/items}}
{{/isContainer}}
/**
* Sets {{name}}.
*
* @param {{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
*
* @return $this
*/
public function {{setter}}({{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{name}}{{^required}} = null{{/required}}): self
{
$this->{{name}} = ${{name}};
return $this;
}
{{/vars}}
}

View File

@ -5,30 +5,7 @@
{{/description}}
* @var {{{vendorExtensions.x-comment-type}}}
* @SerializedName("{{baseName}}")
{{#required}}
* @Assert\NotNull()
{{^isPrimitiveType}}
* @Assert\Valid()
{{/isPrimitiveType}}
{{/required}}
{{#isEnum}}
{{#isContainer}}
* @Assert\All({
{{#items}}
* @Assert\Choice({ {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}} })
{{/items}}
* })
{{/isContainer}}
{{^isContainer}}
* @Assert\Choice({ {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}} })
{{/isContainer}}
{{/isEnum}}
{{#isContainer}}
* @Assert\All({
{{#items}}
* @Assert\Type("{{dataType}}")
{{/items}}
* })
{{#isMap}}
{{#items}}
* @Type("array<string, {{dataType}}>")
@ -37,7 +14,7 @@
{{^isMap}}
{{#items}}
{{#isEnumRef}}
* @Accessor(getter="getSerialized{{nameInPascalCase}}")
* @Accessor(getter="getSerialized{{nameInPascalCase}}", setter="setDeserialized{{nameInPascalCase}}")
* @Type("array<string>")
{{/isEnumRef}}
{{^isEnumRef}}
@ -48,66 +25,95 @@
{{/isContainer}}
{{^isContainer}}
{{#isDate}}
* @Assert\Type("\Date")
* @Type("DateTime<'Y-m-d'>")
{{/isDate}}
{{#isDateTime}}
* @Assert\Type("\DateTime"))
* @Type("DateTime")
{{/isDateTime}}
{{#isEnumRef}}
* @Accessor(getter="getSerialized{{nameInPascalCase}}")
* @Accessor(getter="getSerialized{{nameInPascalCase}}", setter="setDeserialized{{nameInPascalCase}}")
* @Type("string")
{{/isEnumRef}}
{{^isDate}}
{{^isDateTime}}
{{^isEnumRef}}
* @Assert\Type("{{dataType}}")
* @Type("{{dataType}}")
{{/isEnumRef}}
{{/isDateTime}}
{{/isDate}}
{{/isContainer}}
*/
{{#required}}
#[Assert\NotNull]
{{^isPrimitiveType}}
#[Assert\Valid]
{{/isPrimitiveType}}
{{/required}}
{{#isEnum}}
{{#isContainer}}
#[Assert\All([
{{#items}}
new Assert\Choice([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]),
{{/items}}
])]
{{/isContainer}}
{{^isContainer}}
#[Assert\Choice([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])]
{{/isContainer}}
{{/isEnum}}
{{#isContainer}}
#[Assert\All([
{{#items}}
new Assert\Type("{{dataType}}"),
{{/items}}
])]
{{/isContainer}}
{{^isContainer}}
{{#isDate}}
#[Assert\Type("\DateTime")]
{{/isDate}}
{{#isDateTime}}
#[Assert\Type("\DateTime")]
{{/isDateTime}}
{{^isDate}}
{{^isDateTime}}
{{^isEnumRef}}
#[Assert\Type("{{dataType}}")]
{{/isEnumRef}}
{{/isDateTime}}
{{/isDate}}
{{/isContainer}}
{{#hasValidation}}
{{#maxLength}}
* @Assert\Length(
* max = {{.}}
* )
#[Assert\Length(max: {{.}})]
{{/maxLength}}
{{#minLength}}
* @Assert\Length(
* min = {{.}}
* )
#[Assert\Length(min: {{.}})]
{{/minLength}}
{{#minimum}}
{{#exclusiveMinimum}}
* @Assert\GreaterThan({{minimum}})
#[Assert\GreaterThan({{minimum}})]
{{/exclusiveMinimum}}
{{^exclusiveMinimum}}
* @Assert\GreaterThanOrEqual({{minimum}})
#[Assert\GreaterThanOrEqual({{minimum}})]
{{/exclusiveMinimum}}
{{/minimum}}
{{#maximum}}
{{#exclusiveMaximum}}
* @Assert\LessThan({{maximum}})
#[Assert\LessThan({{maximum}})]
{{/exclusiveMaximum}}
{{^exclusiveMaximum}}
* @Assert\LessThanOrEqual({{maximum}})
#[Assert\LessThanOrEqual({{maximum}})]
{{/exclusiveMaximum}}
{{/maximum}}
{{#pattern}}
* @Assert\Regex("/{{.}}/")
#[Assert\Regex("/{{.}}/")]
{{/pattern}}
{{#maxItems}}
* @Assert\Count(
* max = {{.}}
* )
#[Assert\Count(max: {{.}})]
{{/maxItems}}
{{#minItems}}
* @Assert\Count(
* min = {{.}}
* )
#[Assert\Count(min: {{.}})]
{{/minItems}}
{{/hasValidation}}
*/
protected {{{vendorExtensions.x-parameter-type}}} ${{name}} = {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}};

View File

@ -101,12 +101,16 @@ class JmsSerializer implements SerializerInterface
case '\DateTime':
return is_null($data) ? null :new DateTime($data);
default:
if (is_null($data)) {
return null;
}
if (!class_exists($type)) {
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
}
$reflectionClass = new \ReflectionClass($type);
if (!$reflectionClass->implementsInterface('\BackedENum')) {
if (!$reflectionClass->implementsInterface('\BackedEnum')) {
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
}

View File

@ -48,25 +48,25 @@ class ApiResponse
/**
* @var int|null
* @SerializedName("code")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $code = null;
/**
* @var string|null
* @SerializedName("type")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $type = null;
/**
* @var string|null
* @SerializedName("message")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $message = null;
/**
@ -92,8 +92,6 @@ class ApiResponse
return $this->code;
}
/**
* Sets code.
*
@ -108,6 +106,9 @@ class ApiResponse
return $this;
}
/**
* Gets type.
*
@ -118,8 +119,6 @@ class ApiResponse
return $this->type;
}
/**
* Sets type.
*
@ -134,6 +133,9 @@ class ApiResponse
return $this;
}
/**
* Gets message.
*
@ -144,8 +146,6 @@ class ApiResponse
return $this->message;
}
/**
* Sets message.
*
@ -159,6 +159,9 @@ class ApiResponse
return $this;
}
}

View File

@ -48,18 +48,18 @@ class Category
/**
* @var int|null
* @SerializedName("id")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $id = null;
/**
* @var string|null
* @SerializedName("name")
* @Assert\Type("string")
* @Type("string")
* @Assert\Regex("/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/")
*/
#[Assert\Type("string")]
#[Assert\Regex("/^[a-zA-Z0-9]+[a-zA-Z0-9\\.\\-_]*[a-zA-Z0-9]+$/")]
protected ?string $name = null;
/**
@ -84,8 +84,6 @@ class Category
return $this->id;
}
/**
* Sets id.
*
@ -100,6 +98,9 @@ class Category
return $this;
}
/**
* Gets name.
*
@ -110,8 +111,6 @@ class Category
return $this->name;
}
/**
* Sets name.
*
@ -125,6 +124,9 @@ class Category
return $this;
}
}

View File

@ -48,33 +48,33 @@ class Order
/**
* @var int|null
* @SerializedName("id")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $id = null;
/**
* @var int|null
* @SerializedName("petId")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $petId = null;
/**
* @var int|null
* @SerializedName("quantity")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $quantity = null;
/**
* @var \DateTime|null
* @SerializedName("shipDate")
* @Assert\Type("\DateTime"))
* @Type("DateTime")
*/
#[Assert\Type("\DateTime")]
protected ?\DateTime $shipDate = null;
/**
@ -82,18 +82,18 @@ class Order
*
* @var string|null
* @SerializedName("status")
* @Assert\Choice({ "placed", "approved", "delivered" })
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Choice(["placed", "approved", "delivered"])]
#[Assert\Type("string")]
protected ?string $status = null;
/**
* @var bool|null
* @SerializedName("complete")
* @Assert\Type("bool")
* @Type("bool")
*/
#[Assert\Type("bool")]
protected ?bool $complete = false;
/**
@ -122,8 +122,6 @@ class Order
return $this->id;
}
/**
* Sets id.
*
@ -138,6 +136,9 @@ class Order
return $this;
}
/**
* Gets petId.
*
@ -148,8 +149,6 @@ class Order
return $this->petId;
}
/**
* Sets petId.
*
@ -164,6 +163,9 @@ class Order
return $this;
}
/**
* Gets quantity.
*
@ -174,8 +176,6 @@ class Order
return $this->quantity;
}
/**
* Sets quantity.
*
@ -190,6 +190,9 @@ class Order
return $this;
}
/**
* Gets shipDate.
*
@ -200,8 +203,6 @@ class Order
return $this->shipDate;
}
/**
* Sets shipDate.
*
@ -216,6 +217,9 @@ class Order
return $this;
}
/**
* Gets status.
*
@ -226,8 +230,6 @@ class Order
return $this->status;
}
/**
* Sets status.
*
@ -242,6 +244,9 @@ class Order
return $this;
}
/**
* Gets complete.
*
@ -252,8 +257,6 @@ class Order
return $this->complete;
}
/**
* Sets complete.
*
@ -267,6 +270,9 @@ class Order
return $this;
}
}

View File

@ -48,47 +48,47 @@ class Pet
/**
* @var int|null
* @SerializedName("id")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $id = null;
/**
* @var Category|null
* @SerializedName("category")
* @Assert\Type("OpenAPI\Server\Model\Category")
* @Type("OpenAPI\Server\Model\Category")
*/
#[Assert\Type("OpenAPI\Server\Model\Category")]
protected ?Category $category = null;
/**
* @var string|null
* @SerializedName("name")
* @Assert\NotNull()
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\NotNull]
#[Assert\Type("string")]
protected ?string $name = null;
/**
* @var string[]|null
* @SerializedName("photoUrls")
* @Assert\NotNull()
* @Assert\All({
* @Assert\Type("string")
* })
* @Type("array<string>")
*/
#[Assert\NotNull]
#[Assert\All([
new Assert\Type("string"),
])]
protected ?array $photoUrls = null;
/**
* @var Tag[]|null
* @SerializedName("tags")
* @Assert\All({
* @Assert\Type("OpenAPI\Server\Model\Tag")
* })
* @Type("array<OpenAPI\Server\Model\Tag>")
*/
#[Assert\All([
new Assert\Type("OpenAPI\Server\Model\Tag"),
])]
protected ?array $tags = null;
/**
@ -96,10 +96,10 @@ class Pet
*
* @var string|null
* @SerializedName("status")
* @Assert\Choice({ "available", "pending", "sold" })
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Choice(["available", "pending", "sold"])]
#[Assert\Type("string")]
protected ?string $status = null;
/**
@ -128,8 +128,6 @@ class Pet
return $this->id;
}
/**
* Sets id.
*
@ -144,6 +142,9 @@ class Pet
return $this;
}
/**
* Gets category.
*
@ -154,8 +155,6 @@ class Pet
return $this->category;
}
/**
* Sets category.
*
@ -170,6 +169,9 @@ class Pet
return $this;
}
/**
* Gets name.
*
@ -180,8 +182,6 @@ class Pet
return $this->name;
}
/**
* Sets name.
*
@ -196,6 +196,9 @@ class Pet
return $this;
}
/**
* Gets photoUrls.
*
@ -206,8 +209,6 @@ class Pet
return $this->photoUrls;
}
/**
* Sets photoUrls.
*
@ -222,6 +223,9 @@ class Pet
return $this;
}
/**
* Gets tags.
*
@ -232,8 +236,6 @@ class Pet
return $this->tags;
}
/**
* Sets tags.
*
@ -248,6 +250,9 @@ class Pet
return $this;
}
/**
* Gets status.
*
@ -258,8 +263,6 @@ class Pet
return $this->status;
}
/**
* Sets status.
*
@ -273,6 +276,9 @@ class Pet
return $this;
}
}

View File

@ -48,17 +48,17 @@ class Tag
/**
* @var int|null
* @SerializedName("id")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $id = null;
/**
* @var string|null
* @SerializedName("name")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $name = null;
/**
@ -83,8 +83,6 @@ class Tag
return $this->id;
}
/**
* Sets id.
*
@ -99,6 +97,9 @@ class Tag
return $this;
}
/**
* Gets name.
*
@ -109,8 +110,6 @@ class Tag
return $this->name;
}
/**
* Sets name.
*
@ -124,6 +123,9 @@ class Tag
return $this;
}
}

View File

@ -48,57 +48,57 @@ class User
/**
* @var int|null
* @SerializedName("id")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $id = null;
/**
* @var string|null
* @SerializedName("username")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $username = null;
/**
* @var string|null
* @SerializedName("firstName")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $firstName = null;
/**
* @var string|null
* @SerializedName("lastName")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $lastName = null;
/**
* @var string|null
* @SerializedName("email")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $email = null;
/**
* @var string|null
* @SerializedName("password")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $password = null;
/**
* @var string|null
* @SerializedName("phone")
* @Assert\Type("string")
* @Type("string")
*/
#[Assert\Type("string")]
protected ?string $phone = null;
/**
@ -106,9 +106,9 @@ class User
*
* @var int|null
* @SerializedName("userStatus")
* @Assert\Type("int")
* @Type("int")
*/
#[Assert\Type("int")]
protected ?int $userStatus = null;
/**
@ -139,8 +139,6 @@ class User
return $this->id;
}
/**
* Sets id.
*
@ -155,6 +153,9 @@ class User
return $this;
}
/**
* Gets username.
*
@ -165,8 +166,6 @@ class User
return $this->username;
}
/**
* Sets username.
*
@ -181,6 +180,9 @@ class User
return $this;
}
/**
* Gets firstName.
*
@ -191,8 +193,6 @@ class User
return $this->firstName;
}
/**
* Sets firstName.
*
@ -207,6 +207,9 @@ class User
return $this;
}
/**
* Gets lastName.
*
@ -217,8 +220,6 @@ class User
return $this->lastName;
}
/**
* Sets lastName.
*
@ -233,6 +234,9 @@ class User
return $this;
}
/**
* Gets email.
*
@ -243,8 +247,6 @@ class User
return $this->email;
}
/**
* Sets email.
*
@ -259,6 +261,9 @@ class User
return $this;
}
/**
* Gets password.
*
@ -269,8 +274,6 @@ class User
return $this->password;
}
/**
* Sets password.
*
@ -285,6 +288,9 @@ class User
return $this;
}
/**
* Gets phone.
*
@ -295,8 +301,6 @@ class User
return $this->phone;
}
/**
* Sets phone.
*
@ -311,6 +315,9 @@ class User
return $this;
}
/**
* Gets userStatus.
*
@ -321,8 +328,6 @@ class User
return $this->userStatus;
}
/**
* Sets userStatus.
*
@ -336,6 +341,9 @@ class User
return $this;
}
}

View File

@ -101,12 +101,16 @@ class JmsSerializer implements SerializerInterface
case '\DateTime':
return is_null($data) ? null :new DateTime($data);
default:
if (is_null($data)) {
return null;
}
if (!class_exists($type)) {
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
}
$reflectionClass = new \ReflectionClass($type);
if (!$reflectionClass->implementsInterface('\BackedENum')) {
if (!$reflectionClass->implementsInterface('\BackedEnum')) {
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
}

View File

@ -20,6 +20,7 @@
"ext-curl": "*",
"ext-json": "*",
"ext-mbstring": "*",
"doctrine/annotations": "^2.0",
"symfony/validator": "^6.4|^7.0",
"jms/serializer-bundle": "^5.4",
"symfony/framework-bundle": "^6.4|^7.0"