Skip to content

Commit 2839eb3

Browse files
Ocramiusteohhanhui
authored andcommitted
Using oneOf also for simple non-$ref types
As per OpenAPI documentation at https://swagger.io/docs/specification/data-models/data-types/ the OpenAPI v3 documentation does not allow defining union types via `type: ['string', 'integer']`, but requires explicit usage of `oneOf` and nested type declarations or references instead. Ref: api-platform/core#3402 (comment)
1 parent 44566de commit 2839eb3

File tree

1 file changed

+6
-25
lines changed

1 file changed

+6
-25
lines changed

TypeFactory.php

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,6 @@
1717
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
1818
use Ramsey\Uuid\UuidInterface;
1919
use Symfony\Component\PropertyInfo\Type;
20-
use function array_merge;
21-
use function array_unique;
22-
use function array_values;
2320

2421
/**
2522
* {@inheritdoc}
@@ -163,27 +160,11 @@ private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type):
163160
return $jsonSchema;
164161
}
165162

166-
if (!\array_key_exists('type', $jsonSchema)) {
167-
return [
168-
'oneOf' => [
169-
['type' => 'null'],
170-
$jsonSchema,
171-
],
172-
];
173-
}
174-
175-
return array_merge($jsonSchema, ['type' => $this->addNullToTypes((array) $jsonSchema['type'])]);
176-
}
177-
178-
/**
179-
* @param string[] $types
180-
*
181-
* @return string[]
182-
*
183-
* @psalm-param list<string> $types
184-
*/
185-
private function addNullToTypes(array $types): array
186-
{
187-
return array_values(array_unique(array_merge($types, ['null'])));
163+
return [
164+
'oneOf' => [
165+
['type' => 'null'],
166+
$jsonSchema,
167+
],
168+
];
188169
}
189170
}

0 commit comments

Comments
 (0)