Skip to content

Commit 4e55a32

Browse files
authored
Merge pull request #83 from DoclerLabs/optional-nullable-fields-should-not-be-supported
optional nullable fields should not be supported
2 parents 5bf6b38 + ae60a8d commit 4e55a32

File tree

8 files changed

+16
-118
lines changed

8 files changed

+16
-118
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+
## [10.0.0] - 2022.06.27
8+
### Changed
9+
- Generator now fails if field in spec is nullable and optional (unsupported feature)
10+
711
## [9.2.0] - 2022.06.06
812
### Added
913
- Nullable is now supported with allOf

example/gen/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This client is generated using [docler-labs/api-client-generator](https://github
44

55
You can generate the client with the following command:
66
```bash
7-
docker run -it \
7+
docker run -it --rm \
88
-v <local-path-to-api>/doc/openapi.yaml:/openapi.yaml:ro \
99
-v <local-path-to-client>:/client \
1010
-e NAMESPACE=OpenApi\\PetStoreClient \

src/Command/GenerateCommand.php

+2
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,8 @@ public function execute(InputInterface $input, OutputInterface $output): int
107107
$this->generateMetaFiles($ss, $specification);
108108
} catch (Throwable $throwable) {
109109
$this->restoreBackup($ss);
110+
trigger_error($throwable->getMessage(), E_USER_WARNING);
111+
110112
return Command::FAILURE;
111113
}
112114

src/Generator/SchemaGenerator.php

+9-1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use JsonSerializable;
1212
use PhpParser\Node\Expr\Variable;
1313
use PhpParser\Node\Stmt\ClassMethod;
14+
use UnexpectedValueException;
1415

1516
class SchemaGenerator extends MutatorAccessorClassGeneratorAbstract
1617
{
@@ -193,7 +194,14 @@ private function collectSerializationFields(Field $root, Variable $arrayVariable
193194
$fieldName = $this->builder->val($propertyField->getName());
194195
$assignStatement = $this->builder->appendToAssociativeArray($arrayVariable, $fieldName, $value);
195196

196-
if ($propertyField->isOptional() && !$propertyField->isNullable()) {
197+
if (
198+
$propertyField->isOptional()
199+
&& $propertyField->isNullable()
200+
) {
201+
throw new UnexpectedValueException('Optional nullable fields are not supported!');
202+
}
203+
204+
if ($propertyField->isOptional()) {
197205
$ifCondition = $this->builder->notEquals(
198206
$this->builder->localPropertyFetch($propertyField->getPhpVariableName()),
199207
$this->builder->val(null)

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

-44
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ class Item implements SerializableInterface, JsonSerializable
6161

6262
private $mandatoryAnyOf;
6363

64-
/** @var ItemNullableObject|null */
65-
private $nullableObject;
66-
67-
/** @var DateTimeInterface|null */
68-
private $nullableDate;
69-
7064
/** @var int|null */
7165
private $optionalInteger;
7266

@@ -146,26 +140,6 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
146140
$this->mandatoryAnyOf = $mandatoryAnyOf;
147141
}
148142

149-
/**
150-
* @param ItemNullableObject|null $nullableObject
151-
*/
152-
public function setNullableObject($nullableObject): self
153-
{
154-
$this->nullableObject = $nullableObject;
155-
156-
return $this;
157-
}
158-
159-
/**
160-
* @param DateTimeInterface|null $nullableDate
161-
*/
162-
public function setNullableDate($nullableDate): self
163-
{
164-
$this->nullableDate = $nullableDate;
165-
166-
return $this;
167-
}
168-
169143
public function setOptionalInteger(int $optionalInteger): self
170144
{
171145
$this->optionalInteger = $optionalInteger;
@@ -430,22 +404,6 @@ public function getMandatoryAnyOf()
430404
return $this->mandatoryAnyOf;
431405
}
432406

433-
/**
434-
* @return ItemNullableObject|null
435-
*/
436-
public function getNullableObject()
437-
{
438-
return $this->nullableObject;
439-
}
440-
441-
/**
442-
* @return DateTimeInterface|null
443-
*/
444-
public function getNullableDate()
445-
{
446-
return $this->nullableDate;
447-
}
448-
449407
/**
450408
* @return int|null
451409
*/
@@ -598,8 +556,6 @@ public function toArray(): array
598556
$fields['mandatoryNullableObjectWithAllOf'] = $this->mandatoryNullableObjectWithAllOf !== null ? $this->mandatoryNullableObjectWithAllOf->toArray() : null;
599557
$fields['mandatoryMixed'] = $this->mandatoryMixed;
600558
$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;
603559
if ($this->optionalInteger !== null) {
604560
$fields['optionalInteger'] = $this->optionalInteger;
605561
}

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

-32
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,6 @@ class Item implements SerializableInterface, JsonSerializable
6161

6262
private $mandatoryAnyOf;
6363

64-
/** @var ItemNullableObject|null */
65-
private $nullableObject;
66-
67-
/** @var DateTimeInterface|null */
68-
private $nullableDate;
69-
7064
/** @var int|null */
7165
private $optionalInteger;
7266

@@ -144,20 +138,6 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
144138
$this->mandatoryAnyOf = $mandatoryAnyOf;
145139
}
146140

147-
public function setNullableObject(?ItemNullableObject $nullableObject): self
148-
{
149-
$this->nullableObject = $nullableObject;
150-
151-
return $this;
152-
}
153-
154-
public function setNullableDate(?DateTimeInterface $nullableDate): self
155-
{
156-
$this->nullableDate = $nullableDate;
157-
158-
return $this;
159-
}
160-
161141
public function setOptionalInteger(int $optionalInteger): self
162142
{
163143
$this->optionalInteger = $optionalInteger;
@@ -416,16 +396,6 @@ public function getMandatoryAnyOf()
416396
return $this->mandatoryAnyOf;
417397
}
418398

419-
public function getNullableObject(): ?ItemNullableObject
420-
{
421-
return $this->nullableObject;
422-
}
423-
424-
public function getNullableDate(): ?DateTimeInterface
425-
{
426-
return $this->nullableDate;
427-
}
428-
429399
public function getOptionalInteger(): ?int
430400
{
431401
return $this->optionalInteger;
@@ -536,8 +506,6 @@ public function toArray(): array
536506
$fields['mandatoryNullableObjectWithAllOf'] = $this->mandatoryNullableObjectWithAllOf !== null ? $this->mandatoryNullableObjectWithAllOf->toArray() : null;
537507
$fields['mandatoryMixed'] = $this->mandatoryMixed;
538508
$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;
541509
if ($this->optionalInteger !== null) {
542510
$fields['optionalInteger'] = $this->optionalInteger;
543511
}

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

-30
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ class Item implements SerializableInterface, JsonSerializable
5050

5151
private $mandatoryAnyOf;
5252

53-
private ?ItemNullableObject $nullableObject = null;
54-
55-
private ?DateTimeInterface $nullableDate = null;
56-
5753
private ?int $optionalInteger = null;
5854

5955
private ?string $optionalString = null;
@@ -114,20 +110,6 @@ public function __construct(int $mandatoryInteger, string $mandatoryString, stri
114110
$this->mandatoryAnyOf = $mandatoryAnyOf;
115111
}
116112

117-
public function setNullableObject(?ItemNullableObject $nullableObject): self
118-
{
119-
$this->nullableObject = $nullableObject;
120-
121-
return $this;
122-
}
123-
124-
public function setNullableDate(?DateTimeInterface $nullableDate): self
125-
{
126-
$this->nullableDate = $nullableDate;
127-
128-
return $this;
129-
}
130-
131113
public function setOptionalInteger(int $optionalInteger): self
132114
{
133115
$this->optionalInteger = $optionalInteger;
@@ -386,16 +368,6 @@ public function getMandatoryAnyOf()
386368
return $this->mandatoryAnyOf;
387369
}
388370

389-
public function getNullableObject(): ?ItemNullableObject
390-
{
391-
return $this->nullableObject;
392-
}
393-
394-
public function getNullableDate(): ?DateTimeInterface
395-
{
396-
return $this->nullableDate;
397-
}
398-
399371
public function getOptionalInteger(): ?int
400372
{
401373
return $this->optionalInteger;
@@ -506,8 +478,6 @@ public function toArray(): array
506478
$fields['mandatoryNullableObjectWithAllOf'] = $this->mandatoryNullableObjectWithAllOf !== null ? $this->mandatoryNullableObjectWithAllOf->toArray() : null;
507479
$fields['mandatoryMixed'] = $this->mandatoryMixed;
508480
$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;
511481
if ($this->optionalInteger !== null) {
512482
$fields['optionalInteger'] = $this->optionalInteger;
513483
}

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

-10
Original file line numberDiff line numberDiff line change
@@ -68,16 +68,6 @@ components:
6868
anyOf:
6969
- type: number
7070
- type: string
71-
nullableObject:
72-
type: object
73-
properties:
74-
string:
75-
type: string
76-
nullable: true
77-
nullableDate:
78-
type: string
79-
format: 'date-time'
80-
nullable: true
8171
optionalInteger:
8272
type: integer
8373
optionalString:

0 commit comments

Comments
 (0)