Skip to content

Commit

Permalink
fix: describe nullable enums with allOf (#2178)
Browse files Browse the repository at this point in the history
* fix: describe nullable enums with allOf

* style fix

* remove comment
  • Loading branch information
DjordyKoert committed Jan 5, 2024
1 parent ed742a7 commit 23d157c
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 2 deletions.
11 changes: 9 additions & 2 deletions PropertyDescriber/ObjectPropertyDescriber.php
Expand Up @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions Tests/Functional/Entity/Article81.php
Expand Up @@ -9,6 +9,7 @@ public function __construct(
public readonly ArticleType81 $type,
public readonly ArticleType81IntBacked $intBackedType,
public readonly ArticleType81NotBacked $notBackedType,
public readonly ?ArticleType81 $nullableType,
) {
}
}
26 changes: 26 additions & 0 deletions Tests/Functional/FunctionalTest.php
Expand Up @@ -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()
Expand Down

0 comments on commit 23d157c

Please sign in to comment.