Skip to content

Commit

Permalink
Using oneOf also for simple non-$ref types
Browse files Browse the repository at this point in the history
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)
  • Loading branch information
Ocramius authored and teohhanhui committed Feb 27, 2020
1 parent 44566de commit 2839eb3
Showing 1 changed file with 6 additions and 25 deletions.
31 changes: 6 additions & 25 deletions TypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,6 @@
use ApiPlatform\Core\Util\ResourceClassInfoTrait;
use Ramsey\Uuid\UuidInterface;
use Symfony\Component\PropertyInfo\Type;
use function array_merge;
use function array_unique;
use function array_values;

/**
* {@inheritdoc}
Expand Down Expand Up @@ -163,27 +160,11 @@ private function addNullabilityToTypeDefinition(array $jsonSchema, Type $type):
return $jsonSchema;
}

if (!\array_key_exists('type', $jsonSchema)) {
return [
'oneOf' => [
['type' => 'null'],
$jsonSchema,
],
];
}

return array_merge($jsonSchema, ['type' => $this->addNullToTypes((array) $jsonSchema['type'])]);
}

/**
* @param string[] $types
*
* @return string[]
*
* @psalm-param list<string> $types
*/
private function addNullToTypes(array $types): array
{
return array_values(array_unique(array_merge($types, ['null'])));
return [
'oneOf' => [
['type' => 'null'],
$jsonSchema,
],
];
}
}

0 comments on commit 2839eb3

Please sign in to comment.