Skip to content

Commit

Permalink
Merge pull request #95 from DoclerLabs/fix-nullable-optional-object
Browse files Browse the repository at this point in the history
fix nullable optional object
  • Loading branch information
vsouz4 authored Sep 17, 2023
2 parents 51ab421 + 4360835 commit 0127f6f
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 6 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 18 additions & 6 deletions src/Generator/SchemaMapperGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()) {
Expand Down
3 changes: 3 additions & 0 deletions test/suite/functional/Generator/SchemaMapper/ItemMapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
2 changes: 2 additions & 0 deletions test/suite/functional/Generator/SchemaMapper/item.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ components:
type: string
optionalObject:
$ref: '#/components/schemas/EmbeddedObject'
optionalNullableObject:
$ref: '#/components/schemas/EmbeddedNullableObject'
required:
- mandatoryInteger
- mandatoryString
Expand Down

0 comments on commit 0127f6f

Please sign in to comment.