diff --git a/CHANGELOG.md b/CHANGELOG.md index e78c3e0..73629a7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file. The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html). +## [10.1.5] - 2023.09.15 +### Fixed +- Added missing null check for optional nullable object mapping + ## [10.1.4] - 2023.08.25 ### Fixed - Avoid new DateTimeImmutable(null) - adding support for nullable optional datetime diff --git a/src/Generator/SchemaMapperGenerator.php b/src/Generator/SchemaMapperGenerator.php index 86b9dff..8a7c8aa 100644 --- a/src/Generator/SchemaMapperGenerator.php +++ b/src/Generator/SchemaMapperGenerator.php @@ -320,12 +320,24 @@ protected function generateMapStatementsForObject(Field $root, Variable $payload foreach ($optionalFields as $i => $field) { if ($field->isComposite()) { - $mapper = $this->builder->localPropertyFetch(SchemaMapperNaming::getPropertyName($field)); - $optionalVar = $this->builder->methodCall( - $mapper, - 'toSchema', - [$optionalResponseItems[$i]] - ); + $mapper = $this->builder->localPropertyFetch(SchemaMapperNaming::getPropertyName($field)); + if ($field->isNullable()) { + $optionalVar = $this->builder->ternary( + $this->builder->notEquals($optionalResponseItems[$i], $this->builder->val(null)), + $this->builder->methodCall( + $mapper, + 'toSchema', + [$optionalResponseItems[$i]] + ), + $this->builder->val(null) + ); + } else { + $optionalVar = $this->builder->methodCall( + $mapper, + 'toSchema', + [$optionalResponseItems[$i]] + ); + } } elseif ($field->isDate()) { $this->addImport(DateTimeImmutable::class); if ($field->isNullable()) { diff --git a/test/suite/functional/Generator/SchemaMapper/ItemMapper.php b/test/suite/functional/Generator/SchemaMapper/ItemMapper.php index 846871e..283b6b1 100644 --- a/test/suite/functional/Generator/SchemaMapper/ItemMapper.php +++ b/test/suite/functional/Generator/SchemaMapper/ItemMapper.php @@ -63,6 +63,9 @@ public function toSchema(array $payload): Item if (isset($payload['optionalObject'])) { $schema->setOptionalObject($this->embeddedObjectMapper->toSchema($payload['optionalObject'])); } + if (\array_key_exists('optionalNullableObject', $payload)) { + $schema->setOptionalNullableObject($payload['optionalNullableObject'] !== null ? $this->embeddedNullableObjectMapper->toSchema($payload['optionalNullableObject']) : null); + } return $schema; } diff --git a/test/suite/functional/Generator/SchemaMapper/item.yaml b/test/suite/functional/Generator/SchemaMapper/item.yaml index 2615401..2bce727 100644 --- a/test/suite/functional/Generator/SchemaMapper/item.yaml +++ b/test/suite/functional/Generator/SchemaMapper/item.yaml @@ -79,6 +79,8 @@ components: type: string optionalObject: $ref: '#/components/schemas/EmbeddedObject' + optionalNullableObject: + $ref: '#/components/schemas/EmbeddedNullableObject' required: - mandatoryInteger - mandatoryString