diff --git a/PropertyDescriber/ObjectPropertyDescriber.php b/PropertyDescriber/ObjectPropertyDescriber.php index db9ea2bb0..4b55b6c91 100644 --- a/PropertyDescriber/ObjectPropertyDescriber.php +++ b/PropertyDescriber/ObjectPropertyDescriber.php @@ -38,9 +38,16 @@ public function describe(array $types, OA\Schema $property, array $groups = null ); // ignore nullable field if ($types[0]->isNullable()) { - $weakContext = Util::createWeakContext($property->_context); $property->nullable = true; - $property->oneOf = [new OA\Schema(['ref' => $this->modelRegistry->register(new Model($type, $groups)), '_context' => $weakContext])]; + + $weakContext = Util::createWeakContext($property->_context); + $schemas = [new OA\Schema(['ref' => $this->modelRegistry->register(new Model($type, $groups)), '_context' => $weakContext])]; + + if (function_exists('enum_exists') && enum_exists($type->getClassName())) { + $property->allOf = $schemas; + } else { + $property->oneOf = $schemas; + } return; } diff --git a/Tests/Functional/Entity/Article81.php b/Tests/Functional/Entity/Article81.php index 8a483cd79..e35a2f47c 100644 --- a/Tests/Functional/Entity/Article81.php +++ b/Tests/Functional/Entity/Article81.php @@ -9,6 +9,7 @@ public function __construct( public readonly ArticleType81 $type, public readonly ArticleType81IntBacked $intBackedType, public readonly ArticleType81NotBacked $notBackedType, + public readonly ?ArticleType81 $nullableType, ) { } } diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index 452697b90..8a8ecc953 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -744,6 +744,32 @@ public function testEnumSupport() $this->assertSame('integer', $model->type); $this->assertCount(2, $model->enum); + + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'id' => [ + 'type' => 'integer', + ], + 'type' => [ + '$ref' => '#/components/schemas/ArticleType81', + ], + 'intBackedType' => [ + '$ref' => '#/components/schemas/ArticleType81IntBacked', + ], + 'notBackedType' => [ + '$ref' => '#/components/schemas/ArticleType81NotBacked', + ], + 'nullableType' => [ + 'nullable' => true, + 'allOf' => [ + ['$ref' => '#/components/schemas/ArticleType81'], + ], + ], + ], + 'required' => ['id', 'type', 'intBackedType', 'notBackedType'], + 'schema' => 'Article81', + ], json_decode($this->getModel('Article81')->toJson(), true)); } public function testEntitiesWithOverriddenSchemaTypeDoNotReadOtherProperties()