Skip to content

Commit 0f3981f

Browse files
committed
Update SchemaGenerator.php - Fix issue with a nullable Enum
fix `$fields['field'] = $this->field->value?->value;` to expected `$fields['field'] = $this->field?->value;`
1 parent b0b1dbe commit 0f3981f

File tree

10 files changed

+2378
-30
lines changed

10 files changed

+2378
-30
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: 160 additions & 5 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|null */
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,7 +152,7 @@ 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 = ['mandatoryIntEnum' => false, 'mandatoryNullableEnum' => false, 'mandatoryNullableIntEnum' => false, '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 DateTimeInterface|null $mandatoryNullableDate
@@ -167,6 +190,36 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
167190
$this->mandatoryNullableStringWithMinMaxLength = $mandatoryNullableStringWithMinMaxLength;
168191
}
169192

193+
public function setMandatoryIntEnum(int $mandatoryIntEnum): self
194+
{
195+
$this->mandatoryIntEnum = $mandatoryIntEnum;
196+
$this->optionalPropertyChanged['mandatoryIntEnum'] = true;
197+
198+
return $this;
199+
}
200+
201+
/**
202+
* @param string|null $mandatoryNullableEnum
203+
*/
204+
public function setMandatoryNullableEnum($mandatoryNullableEnum): self
205+
{
206+
$this->mandatoryNullableEnum = $mandatoryNullableEnum;
207+
$this->optionalPropertyChanged['mandatoryNullableEnum'] = true;
208+
209+
return $this;
210+
}
211+
212+
/**
213+
* @param int|null $mandatoryNullableIntEnum
214+
*/
215+
public function setMandatoryNullableIntEnum($mandatoryNullableIntEnum): self
216+
{
217+
$this->mandatoryNullableIntEnum = $mandatoryNullableIntEnum;
218+
$this->optionalPropertyChanged['mandatoryNullableIntEnum'] = true;
219+
220+
return $this;
221+
}
222+
170223
public function setOptionalInteger(int $optionalInteger): self
171224
{
172225
$this->optionalInteger = $optionalInteger;
@@ -199,6 +252,28 @@ public function setOptionalIntEnum(int $optionalIntEnum): self
199252
return $this;
200253
}
201254

255+
/**
256+
* @param string|null $optionalNullableEnum
257+
*/
258+
public function setOptionalNullableEnum($optionalNullableEnum): self
259+
{
260+
$this->optionalNullableEnum = $optionalNullableEnum;
261+
$this->optionalPropertyChanged['optionalNullableEnum'] = true;
262+
263+
return $this;
264+
}
265+
266+
/**
267+
* @param int|null $optionalNullableIntEnum
268+
*/
269+
public function setOptionalNullableIntEnum($optionalNullableIntEnum): self
270+
{
271+
$this->optionalNullableIntEnum = $optionalNullableIntEnum;
272+
$this->optionalPropertyChanged['optionalNullableIntEnum'] = true;
273+
274+
return $this;
275+
}
276+
202277
public function setOptionalDate(DateTimeInterface $optionalDate): self
203278
{
204279
$this->optionalDate = $optionalDate;
@@ -423,6 +498,21 @@ public function setOptionalNullableStringWithMinMaxLength($optionalNullableStrin
423498
return $this;
424499
}
425500

501+
public function hasMandatoryIntEnum(): bool
502+
{
503+
return $this->optionalPropertyChanged['mandatoryIntEnum'];
504+
}
505+
506+
public function hasMandatoryNullableEnum(): bool
507+
{
508+
return $this->optionalPropertyChanged['mandatoryNullableEnum'];
509+
}
510+
511+
public function hasMandatoryNullableIntEnum(): bool
512+
{
513+
return $this->optionalPropertyChanged['mandatoryNullableIntEnum'];
514+
}
515+
426516
public function hasOptionalInteger(): bool
427517
{
428518
return $this->optionalPropertyChanged['optionalInteger'];
@@ -443,6 +533,16 @@ public function hasOptionalIntEnum(): bool
443533
return $this->optionalPropertyChanged['optionalIntEnum'];
444534
}
445535

536+
public function hasOptionalNullableEnum(): bool
537+
{
538+
return $this->optionalPropertyChanged['optionalNullableEnum'];
539+
}
540+
541+
public function hasOptionalNullableIntEnum(): bool
542+
{
543+
return $this->optionalPropertyChanged['optionalNullableIntEnum'];
544+
}
545+
446546
public function hasOptionalDate(): bool
447547
{
448548
return $this->optionalPropertyChanged['optionalDate'];
@@ -543,6 +643,30 @@ public function getMandatoryEnum(): string
543643
return $this->mandatoryEnum;
544644
}
545645

646+
/**
647+
* @return int|null
648+
*/
649+
public function getMandatoryIntEnum()
650+
{
651+
return $this->mandatoryIntEnum;
652+
}
653+
654+
/**
655+
* @return string|null
656+
*/
657+
public function getMandatoryNullableEnum()
658+
{
659+
return $this->mandatoryNullableEnum;
660+
}
661+
662+
/**
663+
* @return int|null
664+
*/
665+
public function getMandatoryNullableIntEnum()
666+
{
667+
return $this->mandatoryNullableIntEnum;
668+
}
669+
546670
public function getMandatoryDate(): DateTimeInterface
547671
{
548672
return $this->mandatoryDate;
@@ -645,6 +769,22 @@ public function getOptionalIntEnum()
645769
return $this->optionalIntEnum;
646770
}
647771

772+
/**
773+
* @return string|null
774+
*/
775+
public function getOptionalNullableEnum()
776+
{
777+
return $this->optionalNullableEnum;
778+
}
779+
780+
/**
781+
* @return int|null
782+
*/
783+
public function getOptionalNullableIntEnum()
784+
{
785+
return $this->optionalNullableIntEnum;
786+
}
787+
648788
/**
649789
* @return DateTimeInterface|null
650790
*/
@@ -783,10 +923,19 @@ public function getOptionalNullableStringWithMinMaxLength()
783923

784924
public function toArray(): array
785925
{
786-
$fields = [];
787-
$fields['mandatoryInteger'] = $this->mandatoryInteger;
788-
$fields['mandatoryString'] = $this->mandatoryString;
789-
$fields['mandatoryEnum'] = $this->mandatoryEnum;
926+
$fields = [];
927+
$fields['mandatoryInteger'] = $this->mandatoryInteger;
928+
$fields['mandatoryString'] = $this->mandatoryString;
929+
$fields['mandatoryEnum'] = $this->mandatoryEnum;
930+
if ($this->hasMandatoryIntEnum()) {
931+
$fields['mandatoryIntEnum'] = $this->mandatoryIntEnum;
932+
}
933+
if ($this->hasMandatoryNullableEnum()) {
934+
$fields['mandatoryNullableEnum'] = $this->mandatoryNullableEnum;
935+
}
936+
if ($this->hasMandatoryNullableIntEnum()) {
937+
$fields['mandatoryNullableIntEnum'] = $this->mandatoryNullableIntEnum;
938+
}
790939
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
791940
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
792941
$fields['mandatoryFloat'] = $this->mandatoryFloat;
@@ -810,6 +959,12 @@ public function toArray(): array
810959
if ($this->hasOptionalIntEnum()) {
811960
$fields['optionalIntEnum'] = $this->optionalIntEnum;
812961
}
962+
if ($this->hasOptionalNullableEnum()) {
963+
$fields['optionalNullableEnum'] = $this->optionalNullableEnum;
964+
}
965+
if ($this->hasOptionalNullableIntEnum()) {
966+
$fields['optionalNullableIntEnum'] = $this->optionalNullableIntEnum;
967+
}
813968
if ($this->hasOptionalDate()) {
814969
$fields['optionalDate'] = $this->optionalDate->format(DATE_RFC3339);
815970
}

0 commit comments

Comments
 (0)