Skip to content

Commit 84e38f6

Browse files
authored
Merge pull request #76 from DoclerLabs/fix-nullable-with-all-of
Fix nullable with all of
2 parents 20559ea + c946a74 commit 84e38f6

File tree

6 files changed

+124
-63
lines changed

6 files changed

+124
-63
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [9.2.0] - 2022.06.06
8+
### Added
9+
- Nullable is now supported with allOf
10+
711
## [9.1.1] - 2022.05.09
812
### Fixed
913
- Return type for ::toSchema cannot be nullable

src/Input/Factory/FieldFactory.php

+18
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ public function create(
6666
}
6767

6868
$objectProperties = $this->mergeAllOfProperties($operationName, $schema);
69+
$schema = $this->mergeAllOfAttributes($schema);
6970
} elseif (FieldType::isSpecificationTypeArray($type)) {
7071
$itemReferenceName = '';
7172
if ($schema->items === null) {
@@ -202,4 +203,21 @@ private function mergeAllOfProperties(string $operationName, SpecObjectInterface
202203

203204
return array_merge([], ...$allOfProperties);
204205
}
206+
207+
private function mergeAllOfAttributes(SpecObjectInterface $schema): SpecObjectInterface
208+
{
209+
foreach ($schema->allOf as $allOfSchema) {
210+
if ($allOfSchema instanceof Reference) {
211+
$allOfSchema = $allOfSchema->resolve();
212+
}
213+
214+
if (isset($allOfSchema->nullable)) {
215+
$schema->nullable = $allOfSchema->nullable;
216+
}
217+
218+
// @TODO add more attributes here later if needed
219+
}
220+
221+
return $schema;
222+
}
205223
}

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

+37-23
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class Item implements SerializableInterface, JsonSerializable
5454
/** @var ItemMandatoryObject */
5555
private $mandatoryObject;
5656

57+
/** @var MandatoryNullableObjectWithAllOf|null */
58+
private $mandatoryNullableObjectWithAllOf;
59+
5760
private $mandatoryMixed;
5861

5962
private $mandatoryAnyOf;
@@ -116,13 +119,14 @@ class Item implements SerializableInterface, JsonSerializable
116119
private $optionalObject;
117120

118121
/**
119-
* @param DateTimeInterface|null $mandatoryNullableDate
120-
* @param string[] $mandatoryArray
121-
* @param string[] $mandatoryArrayWithMinItems
122+
* @param DateTimeInterface|null $mandatoryNullableDate
123+
* @param string[] $mandatoryArray
124+
* @param string[] $mandatoryArrayWithMinItems
125+
* @param MandatoryNullableObjectWithAllOf|null $mandatoryNullableObjectWithAllOf
122126
*
123127
* @throws RequestValidationException
124128
*/
125-
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, $mandatoryMixed, $mandatoryAnyOf)
129+
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)
126130
{
127131
$this->mandatoryInteger = $mandatoryInteger;
128132
$this->mandatoryString = $mandatoryString;
@@ -135,10 +139,11 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
135139
if (\count($mandatoryArrayWithMinItems) < 1) {
136140
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
137141
}
138-
$this->mandatoryArrayWithMinItems = $mandatoryArrayWithMinItems;
139-
$this->mandatoryObject = $mandatoryObject;
140-
$this->mandatoryMixed = $mandatoryMixed;
141-
$this->mandatoryAnyOf = $mandatoryAnyOf;
142+
$this->mandatoryArrayWithMinItems = $mandatoryArrayWithMinItems;
143+
$this->mandatoryObject = $mandatoryObject;
144+
$this->mandatoryNullableObjectWithAllOf = $mandatoryNullableObjectWithAllOf;
145+
$this->mandatoryMixed = $mandatoryMixed;
146+
$this->mandatoryAnyOf = $mandatoryAnyOf;
142147
}
143148

144149
/**
@@ -407,6 +412,14 @@ public function getMandatoryObject(): ItemMandatoryObject
407412
return $this->mandatoryObject;
408413
}
409414

415+
/**
416+
* @return MandatoryNullableObjectWithAllOf|null
417+
*/
418+
public function getMandatoryNullableObjectWithAllOf()
419+
{
420+
return $this->mandatoryNullableObjectWithAllOf;
421+
}
422+
410423
public function getMandatoryMixed()
411424
{
412425
return $this->mandatoryMixed;
@@ -571,21 +584,22 @@ public function getOptionalObject()
571584

572585
public function toArray(): array
573586
{
574-
$fields = [];
575-
$fields['mandatoryInteger'] = $this->mandatoryInteger;
576-
$fields['mandatoryString'] = $this->mandatoryString;
577-
$fields['mandatoryEnum'] = $this->mandatoryEnum;
578-
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
579-
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
580-
$fields['mandatoryFloat'] = $this->mandatoryFloat;
581-
$fields['mandatoryBoolean'] = $this->mandatoryBoolean;
582-
$fields['mandatoryArray'] = $this->mandatoryArray;
583-
$fields['mandatoryArrayWithMinItems'] = $this->mandatoryArrayWithMinItems;
584-
$fields['mandatoryObject'] = $this->mandatoryObject->toArray();
585-
$fields['mandatoryMixed'] = $this->mandatoryMixed;
586-
$fields['mandatoryAnyOf'] = $this->mandatoryAnyOf;
587-
$fields['nullableObject'] = $this->nullableObject !== null ? $this->nullableObject->toArray() : null;
588-
$fields['nullableDate'] = $this->nullableDate !== null ? $this->nullableDate->format(DATE_RFC3339) : null;
587+
$fields = [];
588+
$fields['mandatoryInteger'] = $this->mandatoryInteger;
589+
$fields['mandatoryString'] = $this->mandatoryString;
590+
$fields['mandatoryEnum'] = $this->mandatoryEnum;
591+
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
592+
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
593+
$fields['mandatoryFloat'] = $this->mandatoryFloat;
594+
$fields['mandatoryBoolean'] = $this->mandatoryBoolean;
595+
$fields['mandatoryArray'] = $this->mandatoryArray;
596+
$fields['mandatoryArrayWithMinItems'] = $this->mandatoryArrayWithMinItems;
597+
$fields['mandatoryObject'] = $this->mandatoryObject->toArray();
598+
$fields['mandatoryNullableObjectWithAllOf'] = $this->mandatoryNullableObjectWithAllOf !== null ? $this->mandatoryNullableObjectWithAllOf->toArray() : null;
599+
$fields['mandatoryMixed'] = $this->mandatoryMixed;
600+
$fields['mandatoryAnyOf'] = $this->mandatoryAnyOf;
601+
$fields['nullableObject'] = $this->nullableObject !== null ? $this->nullableObject->toArray() : null;
602+
$fields['nullableDate'] = $this->nullableDate !== null ? $this->nullableDate->format(DATE_RFC3339) : null;
589603
if ($this->optionalInteger !== null) {
590604
$fields['optionalInteger'] = $this->optionalInteger;
591605
}

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

+30-20
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class Item implements SerializableInterface, JsonSerializable
5454
/** @var ItemMandatoryObject */
5555
private $mandatoryObject;
5656

57+
/** @var MandatoryNullableObjectWithAllOf|null */
58+
private $mandatoryNullableObjectWithAllOf;
59+
5760
private $mandatoryMixed;
5861

5962
private $mandatoryAnyOf;
@@ -121,7 +124,7 @@ class Item implements SerializableInterface, JsonSerializable
121124
*
122125
* @throws RequestValidationException
123126
*/
124-
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, ?DateTimeInterface $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, $mandatoryMixed, $mandatoryAnyOf)
127+
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)
125128
{
126129
$this->mandatoryInteger = $mandatoryInteger;
127130
$this->mandatoryString = $mandatoryString;
@@ -134,10 +137,11 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
134137
if (\count($mandatoryArrayWithMinItems) < 1) {
135138
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
136139
}
137-
$this->mandatoryArrayWithMinItems = $mandatoryArrayWithMinItems;
138-
$this->mandatoryObject = $mandatoryObject;
139-
$this->mandatoryMixed = $mandatoryMixed;
140-
$this->mandatoryAnyOf = $mandatoryAnyOf;
140+
$this->mandatoryArrayWithMinItems = $mandatoryArrayWithMinItems;
141+
$this->mandatoryObject = $mandatoryObject;
142+
$this->mandatoryNullableObjectWithAllOf = $mandatoryNullableObjectWithAllOf;
143+
$this->mandatoryMixed = $mandatoryMixed;
144+
$this->mandatoryAnyOf = $mandatoryAnyOf;
141145
}
142146

143147
public function setNullableObject(?ItemNullableObject $nullableObject): self
@@ -397,6 +401,11 @@ public function getMandatoryObject(): ItemMandatoryObject
397401
return $this->mandatoryObject;
398402
}
399403

404+
public function getMandatoryNullableObjectWithAllOf(): ?MandatoryNullableObjectWithAllOf
405+
{
406+
return $this->mandatoryNullableObjectWithAllOf;
407+
}
408+
400409
public function getMandatoryMixed()
401410
{
402411
return $this->mandatoryMixed;
@@ -513,21 +522,22 @@ public function getOptionalObject(): ?EmbeddedObject
513522

514523
public function toArray(): array
515524
{
516-
$fields = [];
517-
$fields['mandatoryInteger'] = $this->mandatoryInteger;
518-
$fields['mandatoryString'] = $this->mandatoryString;
519-
$fields['mandatoryEnum'] = $this->mandatoryEnum;
520-
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
521-
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
522-
$fields['mandatoryFloat'] = $this->mandatoryFloat;
523-
$fields['mandatoryBoolean'] = $this->mandatoryBoolean;
524-
$fields['mandatoryArray'] = $this->mandatoryArray;
525-
$fields['mandatoryArrayWithMinItems'] = $this->mandatoryArrayWithMinItems;
526-
$fields['mandatoryObject'] = $this->mandatoryObject->toArray();
527-
$fields['mandatoryMixed'] = $this->mandatoryMixed;
528-
$fields['mandatoryAnyOf'] = $this->mandatoryAnyOf;
529-
$fields['nullableObject'] = $this->nullableObject !== null ? $this->nullableObject->toArray() : null;
530-
$fields['nullableDate'] = $this->nullableDate !== null ? $this->nullableDate->format(DATE_RFC3339) : null;
525+
$fields = [];
526+
$fields['mandatoryInteger'] = $this->mandatoryInteger;
527+
$fields['mandatoryString'] = $this->mandatoryString;
528+
$fields['mandatoryEnum'] = $this->mandatoryEnum;
529+
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
530+
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
531+
$fields['mandatoryFloat'] = $this->mandatoryFloat;
532+
$fields['mandatoryBoolean'] = $this->mandatoryBoolean;
533+
$fields['mandatoryArray'] = $this->mandatoryArray;
534+
$fields['mandatoryArrayWithMinItems'] = $this->mandatoryArrayWithMinItems;
535+
$fields['mandatoryObject'] = $this->mandatoryObject->toArray();
536+
$fields['mandatoryNullableObjectWithAllOf'] = $this->mandatoryNullableObjectWithAllOf !== null ? $this->mandatoryNullableObjectWithAllOf->toArray() : null;
537+
$fields['mandatoryMixed'] = $this->mandatoryMixed;
538+
$fields['mandatoryAnyOf'] = $this->mandatoryAnyOf;
539+
$fields['nullableObject'] = $this->nullableObject !== null ? $this->nullableObject->toArray() : null;
540+
$fields['nullableDate'] = $this->nullableDate !== null ? $this->nullableDate->format(DATE_RFC3339) : null;
531541
if ($this->optionalInteger !== null) {
532542
$fields['optionalInteger'] = $this->optionalInteger;
533543
}

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

+29-20
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ class Item implements SerializableInterface, JsonSerializable
4444

4545
private ItemMandatoryObject $mandatoryObject;
4646

47+
private ?MandatoryNullableObjectWithAllOf $mandatoryNullableObjectWithAllOf = null;
48+
4749
private $mandatoryMixed;
4850

4951
private $mandatoryAnyOf;
@@ -92,7 +94,7 @@ class Item implements SerializableInterface, JsonSerializable
9294
*
9395
* @throws RequestValidationException
9496
*/
95-
public function __construct(int $mandatoryInteger, string $mandatoryString, string $mandatoryEnum, DateTimeInterface $mandatoryDate, ?DateTimeInterface $mandatoryNullableDate, float $mandatoryFloat, bool $mandatoryBoolean, array $mandatoryArray, array $mandatoryArrayWithMinItems, ItemMandatoryObject $mandatoryObject, $mandatoryMixed, $mandatoryAnyOf)
97+
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)
9698
{
9799
$this->mandatoryInteger = $mandatoryInteger;
98100
$this->mandatoryString = $mandatoryString;
@@ -105,10 +107,11 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
105107
if (\count($mandatoryArrayWithMinItems) < 1) {
106108
throw new RequestValidationException(\sprintf('Invalid %s value. Given: `%s`. Expected min items: `1`.', 'mandatoryArrayWithMinItems', $mandatoryArrayWithMinItems));
107109
}
108-
$this->mandatoryArrayWithMinItems = $mandatoryArrayWithMinItems;
109-
$this->mandatoryObject = $mandatoryObject;
110-
$this->mandatoryMixed = $mandatoryMixed;
111-
$this->mandatoryAnyOf = $mandatoryAnyOf;
110+
$this->mandatoryArrayWithMinItems = $mandatoryArrayWithMinItems;
111+
$this->mandatoryObject = $mandatoryObject;
112+
$this->mandatoryNullableObjectWithAllOf = $mandatoryNullableObjectWithAllOf;
113+
$this->mandatoryMixed = $mandatoryMixed;
114+
$this->mandatoryAnyOf = $mandatoryAnyOf;
112115
}
113116

114117
public function setNullableObject(?ItemNullableObject $nullableObject): self
@@ -368,6 +371,11 @@ public function getMandatoryObject(): ItemMandatoryObject
368371
return $this->mandatoryObject;
369372
}
370373

374+
public function getMandatoryNullableObjectWithAllOf(): ?MandatoryNullableObjectWithAllOf
375+
{
376+
return $this->mandatoryNullableObjectWithAllOf;
377+
}
378+
371379
public function getMandatoryMixed()
372380
{
373381
return $this->mandatoryMixed;
@@ -484,21 +492,22 @@ public function getOptionalObject(): ?EmbeddedObject
484492

485493
public function toArray(): array
486494
{
487-
$fields = [];
488-
$fields['mandatoryInteger'] = $this->mandatoryInteger;
489-
$fields['mandatoryString'] = $this->mandatoryString;
490-
$fields['mandatoryEnum'] = $this->mandatoryEnum;
491-
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
492-
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
493-
$fields['mandatoryFloat'] = $this->mandatoryFloat;
494-
$fields['mandatoryBoolean'] = $this->mandatoryBoolean;
495-
$fields['mandatoryArray'] = $this->mandatoryArray;
496-
$fields['mandatoryArrayWithMinItems'] = $this->mandatoryArrayWithMinItems;
497-
$fields['mandatoryObject'] = $this->mandatoryObject->toArray();
498-
$fields['mandatoryMixed'] = $this->mandatoryMixed;
499-
$fields['mandatoryAnyOf'] = $this->mandatoryAnyOf;
500-
$fields['nullableObject'] = $this->nullableObject !== null ? $this->nullableObject->toArray() : null;
501-
$fields['nullableDate'] = $this->nullableDate !== null ? $this->nullableDate->format(DATE_RFC3339) : null;
495+
$fields = [];
496+
$fields['mandatoryInteger'] = $this->mandatoryInteger;
497+
$fields['mandatoryString'] = $this->mandatoryString;
498+
$fields['mandatoryEnum'] = $this->mandatoryEnum;
499+
$fields['mandatoryDate'] = $this->mandatoryDate->format(DATE_RFC3339);
500+
$fields['mandatoryNullableDate'] = $this->mandatoryNullableDate !== null ? $this->mandatoryNullableDate->format(DATE_RFC3339) : null;
501+
$fields['mandatoryFloat'] = $this->mandatoryFloat;
502+
$fields['mandatoryBoolean'] = $this->mandatoryBoolean;
503+
$fields['mandatoryArray'] = $this->mandatoryArray;
504+
$fields['mandatoryArrayWithMinItems'] = $this->mandatoryArrayWithMinItems;
505+
$fields['mandatoryObject'] = $this->mandatoryObject->toArray();
506+
$fields['mandatoryNullableObjectWithAllOf'] = $this->mandatoryNullableObjectWithAllOf !== null ? $this->mandatoryNullableObjectWithAllOf->toArray() : null;
507+
$fields['mandatoryMixed'] = $this->mandatoryMixed;
508+
$fields['mandatoryAnyOf'] = $this->mandatoryAnyOf;
509+
$fields['nullableObject'] = $this->nullableObject !== null ? $this->nullableObject->toArray() : null;
510+
$fields['nullableDate'] = $this->nullableDate !== null ? $this->nullableDate->format(DATE_RFC3339) : null;
502511
if ($this->optionalInteger !== null) {
503512
$fields['optionalInteger'] = $this->optionalInteger;
504513
}

test/suite/functional/Generator/Schema/item.yaml

+6
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@ components:
5858
properties:
5959
string:
6060
type: string
61+
mandatoryNullableObjectWithAllOf:
62+
allOf:
63+
- $ref: '#/components/schemas/EmbeddedObject'
64+
- type: object
65+
nullable: true
6166
mandatoryMixed: {}
6267
mandatoryAnyOf:
6368
anyOf:
@@ -150,6 +155,7 @@ components:
150155
- mandatoryArray
151156
- mandatoryArrayWithMinItems
152157
- mandatoryObject
158+
- mandatoryNullableObjectWithAllOf
153159
- mandatoryMixed
154160
- mandatoryAnyOf
155161
EmbeddedObject:

0 commit comments

Comments
 (0)