Skip to content

Commit fb17e56

Browse files
authored
[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
1 parent fa2b575 commit fb17e56

12 files changed

Lines changed: 426 additions & 341 deletions

File tree

modules/openapi-generator/src/main/resources/php-symfony/composer.mustache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"ext-curl": "*",
2929
"ext-json": "*",
3030
"ext-mbstring": "*",
31+
"doctrine/annotations": "^2.0",
3132
"symfony/validator": "^6.4|^7.0",
3233
"jms/serializer-bundle": "^5.4",
3334
"symfony/framework-bundle": "^6.4|^7.0"

modules/openapi-generator/src/main/resources/php-symfony/model_generic.mustache

Lines changed: 55 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,20 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
3030
return $this->{{name}};
3131
}
3232

33+
/**
34+
* Sets {{name}}.
35+
*
36+
* @param {{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
37+
*
38+
* @return $this
39+
*/
40+
public function {{setter}}({{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{name}}{{^required}} = null{{/required}}): self
41+
{
42+
$this->{{name}} = ${{name}};
43+
44+
return $this;
45+
}
46+
3347
{{#isEnumRef}}
3448
/**
3549
* Gets {{name}} for serialization.
@@ -40,6 +54,24 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
4054
{
4155
return $this->{{name}}?->value ? (string) $this->{{name}}->value : null;
4256
}
57+
58+
/**
59+
* Sets {{name}}.
60+
*
61+
* @param string|{{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
62+
*
63+
* @return $this
64+
*/
65+
public function setDeserialized{{nameInPascalCase}}(string|{{#vendorExtensions.x-comment-type}}{{vendorExtensions.x-comment-type}} {{/vendorExtensions.x-comment-type}}${{name}}{{^required}} = null{{/required}}): self
66+
{
67+
if (is_string(${{name}})) {
68+
${{name}} = {{baseType}}::tryFrom(${{name}});
69+
}
70+
71+
$this->{{name}} = ${{name}};
72+
73+
return $this;
74+
}
4375
{{/isEnumRef}}
4476

4577
{{#isContainer}}
@@ -53,26 +85,36 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}
5385
public function getSerialized{{nameInPascalCase}}(): array
5486
{
5587
return array_map(
56-
static fn ($value) => (string) $value->value,
57-
$this->test ?? []
88+
static fn ($value) => $value?->value ? (string) $value->value : null,
89+
$this->{{name}} ?? []
5890
);
5991
}
60-
{{/isEnumRef}}
61-
{{/items}}
62-
{{/isContainer}}
6392

6493
/**
65-
* Sets {{name}}.
66-
*
67-
* @param {{{vendorExtensions.x-comment-type}}} ${{name}}{{#description}} {{{.}}}{{/description}}
68-
*
69-
* @return $this
70-
*/
71-
public function {{setter}}({{#vendorExtensions.x-parameter-type}}{{vendorExtensions.x-parameter-type}} {{/vendorExtensions.x-parameter-type}}${{name}}{{^required}} = null{{/required}}): self
94+
* Sets {{name}}.
95+
*
96+
* @param {{^required}}?{{/required}}array ${{name}}{{#description}} {{{.}}}{{/description}}
97+
*
98+
* @return $this
99+
*/
100+
public function setDeserialized{{nameInPascalCase}}({{^required}}?{{/required}}array ${{name}}{{^required}} = []{{/required}}): self
72101
{
73-
$this->{{name}} = ${{name}};
102+
$this->{{name}} = array_map(
103+
static function ($value) {
104+
if (is_string($value)) {
105+
$value = {{baseType}}::tryFrom($value);
106+
}
107+
108+
return $value;
109+
},
110+
${{name}} ?? []
111+
);
74112

75113
return $this;
76114
}
115+
{{/isEnumRef}}
116+
{{/items}}
117+
{{/isContainer}}
118+
77119
{{/vars}}
78120
}

modules/openapi-generator/src/main/resources/php-symfony/model_variables.mustache

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,7 @@
55
{{/description}}
66
* @var {{{vendorExtensions.x-comment-type}}}
77
* @SerializedName("{{baseName}}")
8-
{{#required}}
9-
* @Assert\NotNull()
10-
{{^isPrimitiveType}}
11-
* @Assert\Valid()
12-
{{/isPrimitiveType}}
13-
{{/required}}
14-
{{#isEnum}}
15-
{{#isContainer}}
16-
* @Assert\All({
17-
{{#items}}
18-
* @Assert\Choice({ {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}} })
19-
{{/items}}
20-
* })
21-
{{/isContainer}}
22-
{{^isContainer}}
23-
* @Assert\Choice({ {{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}} })
24-
{{/isContainer}}
25-
{{/isEnum}}
268
{{#isContainer}}
27-
* @Assert\All({
28-
{{#items}}
29-
* @Assert\Type("{{dataType}}")
30-
{{/items}}
31-
* })
329
{{#isMap}}
3310
{{#items}}
3411
* @Type("array<string, {{dataType}}>")
@@ -37,7 +14,7 @@
3714
{{^isMap}}
3815
{{#items}}
3916
{{#isEnumRef}}
40-
* @Accessor(getter="getSerialized{{nameInPascalCase}}")
17+
* @Accessor(getter="getSerialized{{nameInPascalCase}}", setter="setDeserialized{{nameInPascalCase}}")
4118
* @Type("array<string>")
4219
{{/isEnumRef}}
4320
{{^isEnumRef}}
@@ -48,66 +25,95 @@
4825
{{/isContainer}}
4926
{{^isContainer}}
5027
{{#isDate}}
51-
* @Assert\Type("\Date")
5228
* @Type("DateTime<'Y-m-d'>")
5329
{{/isDate}}
5430
{{#isDateTime}}
55-
* @Assert\Type("\DateTime"))
5631
* @Type("DateTime")
5732
{{/isDateTime}}
5833
{{#isEnumRef}}
59-
* @Accessor(getter="getSerialized{{nameInPascalCase}}")
60-
* @Type("string")
34+
* @Accessor(getter="getSerialized{{nameInPascalCase}}", setter="setDeserialized{{nameInPascalCase}}")
35+
* @Type("string")
6136
{{/isEnumRef}}
6237
{{^isDate}}
6338
{{^isDateTime}}
6439
{{^isEnumRef}}
65-
* @Assert\Type("{{dataType}}")
6640
* @Type("{{dataType}}")
6741
{{/isEnumRef}}
6842
{{/isDateTime}}
6943
{{/isDate}}
44+
{{/isContainer}}
45+
*/
46+
{{#required}}
47+
#[Assert\NotNull]
48+
{{^isPrimitiveType}}
49+
#[Assert\Valid]
50+
{{/isPrimitiveType}}
51+
{{/required}}
52+
{{#isEnum}}
53+
{{#isContainer}}
54+
#[Assert\All([
55+
{{#items}}
56+
new Assert\Choice([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}]),
57+
{{/items}}
58+
])]
59+
{{/isContainer}}
60+
{{^isContainer}}
61+
#[Assert\Choice([{{#allowableValues}}{{#enumVars}}{{{value}}}{{^-last}}, {{/-last}}{{/enumVars}}{{/allowableValues}}])]
62+
{{/isContainer}}
63+
{{/isEnum}}
64+
{{#isContainer}}
65+
#[Assert\All([
66+
{{#items}}
67+
new Assert\Type("{{dataType}}"),
68+
{{/items}}
69+
])]
70+
{{/isContainer}}
71+
{{^isContainer}}
72+
{{#isDate}}
73+
#[Assert\Type("\DateTime")]
74+
{{/isDate}}
75+
{{#isDateTime}}
76+
#[Assert\Type("\DateTime")]
77+
{{/isDateTime}}
78+
{{^isDate}}
79+
{{^isDateTime}}
80+
{{^isEnumRef}}
81+
#[Assert\Type("{{dataType}}")]
82+
{{/isEnumRef}}
83+
{{/isDateTime}}
84+
{{/isDate}}
7085
{{/isContainer}}
7186
{{#hasValidation}}
7287
{{#maxLength}}
73-
* @Assert\Length(
74-
* max = {{.}}
75-
* )
88+
#[Assert\Length(max: {{.}})]
7689
{{/maxLength}}
7790
{{#minLength}}
78-
* @Assert\Length(
79-
* min = {{.}}
80-
* )
91+
#[Assert\Length(min: {{.}})]
8192
{{/minLength}}
8293
{{#minimum}}
8394
{{#exclusiveMinimum}}
84-
* @Assert\GreaterThan({{minimum}})
95+
#[Assert\GreaterThan({{minimum}})]
8596
{{/exclusiveMinimum}}
8697
{{^exclusiveMinimum}}
87-
* @Assert\GreaterThanOrEqual({{minimum}})
98+
#[Assert\GreaterThanOrEqual({{minimum}})]
8899
{{/exclusiveMinimum}}
89100
{{/minimum}}
90101
{{#maximum}}
91102
{{#exclusiveMaximum}}
92-
* @Assert\LessThan({{maximum}})
103+
#[Assert\LessThan({{maximum}})]
93104
{{/exclusiveMaximum}}
94105
{{^exclusiveMaximum}}
95-
* @Assert\LessThanOrEqual({{maximum}})
106+
#[Assert\LessThanOrEqual({{maximum}})]
96107
{{/exclusiveMaximum}}
97108
{{/maximum}}
98109
{{#pattern}}
99-
* @Assert\Regex("/{{.}}/")
110+
#[Assert\Regex("/{{.}}/")]
100111
{{/pattern}}
101112
{{#maxItems}}
102-
* @Assert\Count(
103-
* max = {{.}}
104-
* )
113+
#[Assert\Count(max: {{.}})]
105114
{{/maxItems}}
106115
{{#minItems}}
107-
* @Assert\Count(
108-
* min = {{.}}
109-
* )
116+
#[Assert\Count(min: {{.}})]
110117
{{/minItems}}
111118
{{/hasValidation}}
112-
*/
113119
protected {{{vendorExtensions.x-parameter-type}}} ${{name}} = {{#defaultValue}}{{{defaultValue}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}};

modules/openapi-generator/src/main/resources/php-symfony/serialization/JmsSerializer.mustache

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,12 +101,16 @@ class JmsSerializer implements SerializerInterface
101101
case '\DateTime':
102102
return is_null($data) ? null :new DateTime($data);
103103
default:
104+
if (is_null($data)) {
105+
return null;
106+
}
107+
104108
if (!class_exists($type)) {
105109
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
106110
}
107111

108112
$reflectionClass = new \ReflectionClass($type);
109-
if (!$reflectionClass->implementsInterface('\BackedENum')) {
113+
if (!$reflectionClass->implementsInterface('\BackedEnum')) {
110114
throw new RuntimeException(sprintf("Type %s is unsupported", $type));
111115
}
112116

0 commit comments

Comments
 (0)