Skip to content

Commit ffc04ea

Browse files
authored
Merge pull request #126 from Spell6inder/patch-2
Update SchemaGenerator.php - Fix issue with a nullable Enum
2 parents b0b1dbe + 1edc9cd commit ffc04ea

File tree

10 files changed

+2029
-15
lines changed

10 files changed

+2029
-15
lines changed

src/Generator/SchemaGenerator.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -320,11 +320,9 @@ private function collectSerializationFields(Field $root, Variable $arrayVariable
320320
$value = $methodCall;
321321
}
322322
} elseif ($propertyField->isEnum() && $this->phpVersion->isEnumSupported()) {
323-
$value = $this->builder->propertyFetch($value, 'value');
324-
325-
if ($propertyField->isNullable()) {
326-
$value = $this->builder->nullsafePropertyFetch($value, 'value');
327-
}
323+
$value = $propertyField->isNullable()
324+
? $this->builder->nullsafePropertyFetch($value, 'value')
325+
: $this->builder->propertyFetch($value, 'value');
328326
} elseif ($propertyField->isArrayOfEnums() && $this->phpVersion->isEnumSupported()) {
329327
$enumField = $propertyField->getArrayItem();
330328
$arrayMapCall = $this->builder->funcCall(

test/suite/functional/Generator/Schema/ItemPhp70.php

Lines changed: 108 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ class Item implements SerializableInterface, JsonSerializable
2020

2121
const MANDATORY_ENUM_ANOTHER_OPTION = 'another option';
2222

23+
const MANDATORY_NULLABLE_ENUM_ONE_OPTION = 'one option';
24+
25+
const MANDATORY_NULLABLE_ENUM_ANOTHER_OPTION = 'another option';
26+
2327
const OPTIONAL_ENUM_ONE_OPTION = 'one option';
2428

2529
const OPTIONAL_ENUM_ANOTHER_OPTION = 'another option';
2630

31+
const OPTIONAL_NULLABLE_ENUM_ONE_OPTION = 'one option';
32+
33+
const OPTIONAL_NULLABLE_ENUM_ANOTHER_OPTION = 'another option';
34+
2735
/** @var int */
2836
private $mandatoryInteger;
2937

@@ -33,6 +41,15 @@ class Item implements SerializableInterface, JsonSerializable
3341
/** @var string */
3442
private $mandatoryEnum;
3543

44+
/** @var int */
45+
private $mandatoryIntEnum;
46+
47+
/** @var string|null */
48+
private $mandatoryNullableEnum;
49+
50+
/** @var int|null */
51+
private $mandatoryNullableIntEnum;
52+
3653
/** @var DateTimeInterface */
3754
private $mandatoryDate;
3855

@@ -77,6 +94,12 @@ class Item implements SerializableInterface, JsonSerializable
7794
/** @var int|null */
7895
private $optionalIntEnum;
7996

97+
/** @var string|null */
98+
private $optionalNullableEnum;
99+
100+
/** @var int|null */
101+
private $optionalNullableIntEnum;
102+
80103
/** @var DateTimeInterface|null */
81104
private $optionalDate;
82105

@@ -129,9 +152,11 @@ class Item implements SerializableInterface, JsonSerializable
129152
private $optionalNullableStringWithMinMaxLength;
130153

131154
/** @var array */
132-
private $optionalPropertyChanged = ['optionalInteger' => false, 'optionalString' => false, 'optionalEnum' => false, 'optionalIntEnum' => false, 'optionalDate' => false, 'optionalNullableDate' => false, 'optionalFloat' => false, 'optionalBoolean' => false, 'optionalNullableBoolean' => false, 'optionalArray' => false, 'optionalNullableArray' => false, 'optionalMixedArray' => false, 'optionalArrayWithMinMaxItems' => false, 'optionalStringWithMinMaxLength' => false, 'optionalStringWithPattern' => false, 'optionalIntegerBetweenIncluded' => false, 'optionalIntegerBetweenExcluded' => false, 'optionalNumberBetweenIncluded' => false, 'optionalNumberBetweenExcluded' => false, 'optionalObject' => false, 'optionalNullableStringWithMinMaxLength' => false];
155+
private $optionalPropertyChanged = ['optionalInteger' => false, 'optionalString' => false, 'optionalEnum' => false, 'optionalIntEnum' => false, 'optionalNullableEnum' => false, 'optionalNullableIntEnum' => false, 'optionalDate' => false, 'optionalNullableDate' => false, 'optionalFloat' => false, 'optionalBoolean' => false, 'optionalNullableBoolean' => false, 'optionalArray' => false, 'optionalNullableArray' => false, 'optionalMixedArray' => false, 'optionalArrayWithMinMaxItems' => false, 'optionalStringWithMinMaxLength' => false, 'optionalStringWithPattern' => false, 'optionalIntegerBetweenIncluded' => false, 'optionalIntegerBetweenExcluded' => false, 'optionalNumberBetweenIncluded' => false, 'optionalNumberBetweenExcluded' => false, 'optionalObject' => false, 'optionalNullableStringWithMinMaxLength' => false];
133156

134157
/**
158+
* @param string|null $mandatoryNullableEnum
159+
* @param int|null $mandatoryNullableIntEnum
135160
* @param DateTimeInterface|null $mandatoryNullableDate
136161
* @param string[] $mandatoryArray
137162
* @param string[] $mandatoryArrayWithMinItems
@@ -140,7 +165,7 @@ class Item implements SerializableInterface, JsonSerializable
140165
*
141166
* @throws RequestValidationException
142167
*/
143-
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, $mandatoryNullableObjectWithAllOf, $mandatoryMixed, MandatoryAnyOf $mandatoryAnyOf, $mandatoryNullableStringWithMinMaxLength)
168+
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, int $mandatoryIntEnum, $mandatoryNullableEnum, $mandatoryNullableIntEnum, DateTimeInterface $mandatoryDate, $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, $mandatoryNullableObjectWithAllOf, $mandatoryMixed, MandatoryAnyOf $mandatoryAnyOf, $mandatoryNullableStringWithMinMaxLength)
144169
{
145170
if (count($mandatoryArrayWithMinItems) < 1) {
146171
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
@@ -154,6 +179,9 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
154179
$this->mandatoryInteger = $mandatoryInteger;
155180
$this->mandatoryString = $mandatoryString;
156181
$this->mandatoryEnum = $mandatoryEnum;
182+
$this->mandatoryIntEnum = $mandatoryIntEnum;
183+
$this->mandatoryNullableEnum = $mandatoryNullableEnum;
184+
$this->mandatoryNullableIntEnum = $mandatoryNullableIntEnum;
157185
$this->mandatoryDate = $mandatoryDate;
158186
$this->mandatoryNullableDate = $mandatoryNullableDate;
159187
$this->mandatoryFloat = $mandatoryFloat;
@@ -199,6 +227,28 @@ public function setOptionalIntEnum(int $optionalIntEnum): self
199227
return $this;
200228
}
201229

230+
/**
231+
* @param string|null $optionalNullableEnum
232+
*/
233+
public function setOptionalNullableEnum($optionalNullableEnum): self
234+
{
235+
$this->optionalNullableEnum = $optionalNullableEnum;
236+
$this->optionalPropertyChanged['optionalNullableEnum'] = true;
237+
238+
return $this;
239+
}
240+
241+
/**
242+
* @param int|null $optionalNullableIntEnum
243+
*/
244+
public function setOptionalNullableIntEnum($optionalNullableIntEnum): self
245+
{
246+
$this->optionalNullableIntEnum = $optionalNullableIntEnum;
247+
$this->optionalPropertyChanged['optionalNullableIntEnum'] = true;
248+
249+
return $this;
250+
}
251+
202252
public function setOptionalDate(DateTimeInterface $optionalDate): self
203253
{
204254
$this->optionalDate = $optionalDate;
@@ -443,6 +493,16 @@ public function hasOptionalIntEnum(): bool
443493
return $this->optionalPropertyChanged['optionalIntEnum'];
444494
}
445495

496+
public function hasOptionalNullableEnum(): bool
497+
{
498+
return $this->optionalPropertyChanged['optionalNullableEnum'];
499+
}
500+
501+
public function hasOptionalNullableIntEnum(): bool
502+
{
503+
return $this->optionalPropertyChanged['optionalNullableIntEnum'];
504+
}
505+
446506
public function hasOptionalDate(): bool
447507
{
448508
return $this->optionalPropertyChanged['optionalDate'];
@@ -543,6 +603,27 @@ public function getMandatoryEnum(): string
543603
return $this->mandatoryEnum;
544604
}
545605

606+
public function getMandatoryIntEnum(): int
607+
{
608+
return $this->mandatoryIntEnum;
609+
}
610+
611+
/**
612+
* @return string|null
613+
*/
614+
public function getMandatoryNullableEnum()
615+
{
616+
return $this->mandatoryNullableEnum;
617+
}
618+
619+
/**
620+
* @return int|null
621+
*/
622+
public function getMandatoryNullableIntEnum()
623+
{
624+
return $this->mandatoryNullableIntEnum;
625+
}
626+
546627
public function getMandatoryDate(): DateTimeInterface
547628
{
548629
return $this->mandatoryDate;
@@ -645,6 +726,22 @@ public function getOptionalIntEnum()
645726
return $this->optionalIntEnum;
646727
}
647728

729+
/**
730+
* @return string|null
731+
*/
732+
public function getOptionalNullableEnum()
733+
{
734+
return $this->optionalNullableEnum;
735+
}
736+
737+
/**
738+
* @return int|null
739+
*/
740+
public function getOptionalNullableIntEnum()
741+
{
742+
return $this->optionalNullableIntEnum;
743+
}
744+
648745
/**
649746
* @return DateTimeInterface|null
650747
*/
@@ -787,6 +884,9 @@ public function toArray(): array
787884
$fields['mandatoryInteger'] = $this->mandatoryInteger;
788885
$fields['mandatoryString'] = $this->mandatoryString;
789886
$fields['mandatoryEnum'] = $this->mandatoryEnum;
887+
$fields['mandatoryIntEnum'] = $this->mandatoryIntEnum;
888+
$fields['mandatoryNullableEnum'] = $this->mandatoryNullableEnum;
889+
$fields['mandatoryNullableIntEnum'] = $this->mandatoryNullableIntEnum;
790890
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
791891
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
792892
$fields['mandatoryFloat'] = $this->mandatoryFloat;
@@ -810,6 +910,12 @@ public function toArray(): array
810910
if ($this->hasOptionalIntEnum()) {
811911
$fields['optionalIntEnum'] = $this->optionalIntEnum;
812912
}
913+
if ($this->hasOptionalNullableEnum()) {
914+
$fields['optionalNullableEnum'] = $this->optionalNullableEnum;
915+
}
916+
if ($this->hasOptionalNullableIntEnum()) {
917+
$fields['optionalNullableIntEnum'] = $this->optionalNullableIntEnum;
918+
}
813919
if ($this->hasOptionalDate()) {
814920
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
815921
}

test/suite/functional/Generator/Schema/ItemPhp72.php

Lines changed: 88 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,18 @@ class Item implements SerializableInterface, JsonSerializable
2020

2121
public const MANDATORY_ENUM_ANOTHER_OPTION = 'another option';
2222

23+
public const MANDATORY_NULLABLE_ENUM_ONE_OPTION = 'one option';
24+
25+
public const MANDATORY_NULLABLE_ENUM_ANOTHER_OPTION = 'another option';
26+
2327
public const OPTIONAL_ENUM_ONE_OPTION = 'one option';
2428

2529
public const OPTIONAL_ENUM_ANOTHER_OPTION = 'another option';
2630

31+
public const OPTIONAL_NULLABLE_ENUM_ONE_OPTION = 'one option';
32+
33+
public const OPTIONAL_NULLABLE_ENUM_ANOTHER_OPTION = 'another option';
34+
2735
/** @var int */
2836
private $mandatoryInteger;
2937

@@ -33,6 +41,15 @@ class Item implements SerializableInterface, JsonSerializable
3341
/** @var string */
3442
private $mandatoryEnum;
3543

44+
/** @var int */
45+
private $mandatoryIntEnum;
46+
47+
/** @var string|null */
48+
private $mandatoryNullableEnum;
49+
50+
/** @var int|null */
51+
private $mandatoryNullableIntEnum;
52+
3653
/** @var DateTimeInterface */
3754
private $mandatoryDate;
3855

@@ -77,6 +94,12 @@ class Item implements SerializableInterface, JsonSerializable
7794
/** @var int|null */
7895
private $optionalIntEnum;
7996

97+
/** @var string|null */
98+
private $optionalNullableEnum;
99+
100+
/** @var int|null */
101+
private $optionalNullableIntEnum;
102+
80103
/** @var DateTimeInterface|null */
81104
private $optionalDate;
82105

@@ -129,15 +152,15 @@ class Item implements SerializableInterface, JsonSerializable
129152
private $optionalNullableStringWithMinMaxLength;
130153

131154
/** @var array */
132-
private $optionalPropertyChanged = ['optionalInteger' => false, 'optionalString' => false, 'optionalEnum' => false, 'optionalIntEnum' => false, 'optionalDate' => false, 'optionalNullableDate' => false, 'optionalFloat' => false, 'optionalBoolean' => false, 'optionalNullableBoolean' => false, 'optionalArray' => false, 'optionalNullableArray' => false, 'optionalMixedArray' => false, 'optionalArrayWithMinMaxItems' => false, 'optionalStringWithMinMaxLength' => false, 'optionalStringWithPattern' => false, 'optionalIntegerBetweenIncluded' => false, 'optionalIntegerBetweenExcluded' => false, 'optionalNumberBetweenIncluded' => false, 'optionalNumberBetweenExcluded' => false, 'optionalObject' => false, 'optionalNullableStringWithMinMaxLength' => false];
155+
private $optionalPropertyChanged = ['optionalInteger' => false, 'optionalString' => false, 'optionalEnum' => false, 'optionalIntEnum' => false, 'optionalNullableEnum' => false, 'optionalNullableIntEnum' => false, 'optionalDate' => false, 'optionalNullableDate' => false, 'optionalFloat' => false, 'optionalBoolean' => false, 'optionalNullableBoolean' => false, 'optionalArray' => false, 'optionalNullableArray' => false, 'optionalMixedArray' => false, 'optionalArrayWithMinMaxItems' => false, 'optionalStringWithMinMaxLength' => false, 'optionalStringWithPattern' => false, 'optionalIntegerBetweenIncluded' => false, 'optionalIntegerBetweenExcluded' => false, 'optionalNumberBetweenIncluded' => false, 'optionalNumberBetweenExcluded' => false, 'optionalObject' => false, 'optionalNullableStringWithMinMaxLength' => false];
133156

134157
/**
135158
* @param string[] $mandatoryArray
136159
* @param string[] $mandatoryArrayWithMinItems
137160
*
138161
* @throws RequestValidationException
139162
*/
140-
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, ?DateTimeInterface $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, ?MandatoryNullableObjectWithAllOf $mandatoryNullableObjectWithAllOf, $mandatoryMixed, MandatoryAnyOf $mandatoryAnyOf, ?string $mandatoryNullableStringWithMinMaxLength)
163+
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, int $mandatoryIntEnum, ?string $mandatoryNullableEnum, ?int $mandatoryNullableIntEnum, DateTimeInterface $mandatoryDate, ?DateTimeInterface $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, ?MandatoryNullableObjectWithAllOf $mandatoryNullableObjectWithAllOf, $mandatoryMixed, MandatoryAnyOf $mandatoryAnyOf, ?string $mandatoryNullableStringWithMinMaxLength)
141164
{
142165
if (count($mandatoryArrayWithMinItems) < 1) {
143166
throw new RequestValidationException(sprintf('Invalid %s value. Expected min items: `1`.', 'mandatoryArrayWithMinItems'));
@@ -151,6 +174,9 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
151174
$this->mandatoryInteger = $mandatoryInteger;
152175
$this->mandatoryString = $mandatoryString;
153176
$this->mandatoryEnum = $mandatoryEnum;
177+
$this->mandatoryIntEnum = $mandatoryIntEnum;
178+
$this->mandatoryNullableEnum = $mandatoryNullableEnum;
179+
$this->mandatoryNullableIntEnum = $mandatoryNullableIntEnum;
154180
$this->mandatoryDate = $mandatoryDate;
155181
$this->mandatoryNullableDate = $mandatoryNullableDate;
156182
$this->mandatoryFloat = $mandatoryFloat;
@@ -196,6 +222,22 @@ public function setOptionalIntEnum(int $optionalIntEnum): self
196222
return $this;
197223
}
198224

225+
public function setOptionalNullableEnum(?string $optionalNullableEnum): self
226+
{
227+
$this->optionalNullableEnum = $optionalNullableEnum;
228+
$this->optionalPropertyChanged['optionalNullableEnum'] = true;
229+
230+
return $this;
231+
}
232+
233+
public function setOptionalNullableIntEnum(?int $optionalNullableIntEnum): self
234+
{
235+
$this->optionalNullableIntEnum = $optionalNullableIntEnum;
236+
$this->optionalPropertyChanged['optionalNullableIntEnum'] = true;
237+
238+
return $this;
239+
}
240+
199241
public function setOptionalDate(DateTimeInterface $optionalDate): self
200242
{
201243
$this->optionalDate = $optionalDate;
@@ -432,6 +474,16 @@ public function hasOptionalIntEnum(): bool
432474
return $this->optionalPropertyChanged['optionalIntEnum'];
433475
}
434476

477+
public function hasOptionalNullableEnum(): bool
478+
{
479+
return $this->optionalPropertyChanged['optionalNullableEnum'];
480+
}
481+
482+
public function hasOptionalNullableIntEnum(): bool
483+
{
484+
return $this->optionalPropertyChanged['optionalNullableIntEnum'];
485+
}
486+
435487
public function hasOptionalDate(): bool
436488
{
437489
return $this->optionalPropertyChanged['optionalDate'];
@@ -532,6 +584,21 @@ public function getMandatoryEnum(): string
532584
return $this->mandatoryEnum;
533585
}
534586

587+
public function getMandatoryIntEnum(): int
588+
{
589+
return $this->mandatoryIntEnum;
590+
}
591+
592+
public function getMandatoryNullableEnum(): ?string
593+
{
594+
return $this->mandatoryNullableEnum;
595+
}
596+
597+
public function getMandatoryNullableIntEnum(): ?int
598+
{
599+
return $this->mandatoryNullableIntEnum;
600+
}
601+
535602
public function getMandatoryDate(): DateTimeInterface
536603
{
537604
return $this->mandatoryDate;
@@ -613,6 +680,16 @@ public function getOptionalIntEnum(): ?int
613680
return $this->optionalIntEnum;
614681
}
615682

683+
public function getOptionalNullableEnum(): ?string
684+
{
685+
return $this->optionalNullableEnum;
686+
}
687+
688+
public function getOptionalNullableIntEnum(): ?int
689+
{
690+
return $this->optionalNullableIntEnum;
691+
}
692+
616693
public function getOptionalDate(): ?DateTimeInterface
617694
{
618695
return $this->optionalDate;
@@ -716,6 +793,9 @@ public function toArray(): array
716793
$fields['mandatoryInteger'] = $this->mandatoryInteger;
717794
$fields['mandatoryString'] = $this->mandatoryString;
718795
$fields['mandatoryEnum'] = $this->mandatoryEnum;
796+
$fields['mandatoryIntEnum'] = $this->mandatoryIntEnum;
797+
$fields['mandatoryNullableEnum'] = $this->mandatoryNullableEnum;
798+
$fields['mandatoryNullableIntEnum'] = $this->mandatoryNullableIntEnum;
719799
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
720800
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
721801
$fields['mandatoryFloat'] = $this->mandatoryFloat;
@@ -739,6 +819,12 @@ public function toArray(): array
739819
if ($this->hasOptionalIntEnum()) {
740820
$fields['optionalIntEnum'] = $this->optionalIntEnum;
741821
}
822+
if ($this->hasOptionalNullableEnum()) {
823+
$fields['optionalNullableEnum'] = $this->optionalNullableEnum;
824+
}
825+
if ($this->hasOptionalNullableIntEnum()) {
826+
$fields['optionalNullableIntEnum'] = $this->optionalNullableIntEnum;
827+
}
742828
if ($this->hasOptionalDate()) {
743829
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
744830
}

0 commit comments

Comments
 (0)