Skip to content

Commit 0127f6f

Browse files
authored
Merge pull request #95 from DoclerLabs/fix-nullable-optional-object
fix nullable optional object
2 parents 51ab421 + 4360835 commit 0127f6f

File tree

4 files changed

+27
-6
lines changed

4 files changed

+27
-6
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.1.5] - 2023.09.15
8+
### Fixed
9+
- Added missing null check for optional nullable object mapping
10+
711
## [10.1.4] - 2023.08.25
812
### Fixed
913
- Avoid new DateTimeImmutable(null) - adding support for nullable optional datetime

src/Generator/SchemaMapperGenerator.php

+18-6
Original file line numberDiff line numberDiff line change
@@ -320,12 +320,24 @@ protected function generateMapStatementsForObject(Field $root, Variable $payload
320320

321321
foreach ($optionalFields as $i => $field) {
322322
if ($field->isComposite()) {
323-
$mapper = $this->builder->localPropertyFetch(SchemaMapperNaming::getPropertyName($field));
324-
$optionalVar = $this->builder->methodCall(
325-
$mapper,
326-
'toSchema',
327-
[$optionalResponseItems[$i]]
328-
);
323+
$mapper = $this->builder->localPropertyFetch(SchemaMapperNaming::getPropertyName($field));
324+
if ($field->isNullable()) {
325+
$optionalVar = $this->builder->ternary(
326+
$this->builder->notEquals($optionalResponseItems[$i], $this->builder->val(null)),
327+
$this->builder->methodCall(
328+
$mapper,
329+
'toSchema',
330+
[$optionalResponseItems[$i]]
331+
),
332+
$this->builder->val(null)
333+
);
334+
} else {
335+
$optionalVar = $this->builder->methodCall(
336+
$mapper,
337+
'toSchema',
338+
[$optionalResponseItems[$i]]
339+
);
340+
}
329341
} elseif ($field->isDate()) {
330342
$this->addImport(DateTimeImmutable::class);
331343
if ($field->isNullable()) {

test/suite/functional/Generator/SchemaMapper/ItemMapper.php

+3
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,9 @@ public function toSchema(array $payload): Item
6363
if (isset($payload['optionalObject'])) {
6464
$schema->setOptionalObject($this->embeddedObjectMapper->toSchema($payload['optionalObject']));
6565
}
66+
if (\array_key_exists('optionalNullableObject', $payload)) {
67+
$schema->setOptionalNullableObject($payload['optionalNullableObject'] !== null ? $this->embeddedNullableObjectMapper->toSchema($payload['optionalNullableObject']) : null);
68+
}
6669

6770
return $schema;
6871
}

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

+2
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,8 @@ components:
7979
type: string
8080
optionalObject:
8181
$ref: '#/components/schemas/EmbeddedObject'
82+
optionalNullableObject:
83+
$ref: '#/components/schemas/EmbeddedNullableObject'
8284
required:
8385
- mandatoryInteger
8486
- mandatoryString

0 commit comments

Comments
 (0)