From 2510b0e102173740f60f4d4b7ddf374fd23673f7 Mon Sep 17 00:00:00 2001 From: DjordyKoert Date: Tue, 9 Jan 2024 13:53:13 +0100 Subject: [PATCH] fix: handling of nullable on union typed property --- PropertyDescriber/NullablePropertyDescriber.php | 14 +++++++++++--- Tests/Functional/Entity/CompoundEntity.php | 5 +++++ Tests/Functional/FunctionalTest.php | 14 ++++++++++++++ 3 files changed, 30 insertions(+), 3 deletions(-) diff --git a/PropertyDescriber/NullablePropertyDescriber.php b/PropertyDescriber/NullablePropertyDescriber.php index 159341844..ae293fe73 100644 --- a/PropertyDescriber/NullablePropertyDescriber.php +++ b/PropertyDescriber/NullablePropertyDescriber.php @@ -34,8 +34,16 @@ public function describe(array $types, OA\Schema $property, array $groups = null public function supports(array $types, array $context = []): bool { - return 1 === count($types) - && $types[0]->isNullable() - && !array_key_exists(self::RECURSIVE, $context); + if (array_key_exists(self::RECURSIVE, $context)) { + return false; + } + + foreach ($types as $type) { + if ($type->isNullable()) { + return true; + } + } + + return false; } } diff --git a/Tests/Functional/Entity/CompoundEntity.php b/Tests/Functional/Entity/CompoundEntity.php index ba0a6cf3c..29faca007 100644 --- a/Tests/Functional/Entity/CompoundEntity.php +++ b/Tests/Functional/Entity/CompoundEntity.php @@ -17,4 +17,9 @@ class CompoundEntity * @var int|CompoundEntity[] */ public $complex; + + /** + * @var int|CompoundEntity[]|null + */ + public $nullableComplex; } diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index d4712d2ba..cac5fe90b 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -642,6 +642,20 @@ public function testCompoundEntityAction() ], ], ], + 'nullableComplex' => [ + 'nullable' => true, + 'oneOf' => [ + [ + 'type' => 'integer', + ], + [ + 'type' => 'array', + 'items' => [ + '$ref' => '#/components/schemas/CompoundEntity', + ], + ], + ], + ], ], ], json_decode($this->getModel('CompoundEntity')->toJson(), true)); }