From 6279683387b09e6f8fac6f4fb12c4ad6239bce18 Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Thu, 31 Aug 2023 10:56:22 +1200 Subject: [PATCH] Handle nullable in 3.1.0 in conjuction with oneOf (#1470) --- Examples/using-links-php81/PullRequest.php | 3 +- .../RepositoriesController.php | 12 ++-- src/Analysis.php | 2 +- src/Annotations/AbstractAnnotation.php | 8 ++- tests/Fixtures/Apis/Attributes/basic.php | 15 +++-- tests/Fixtures/Scratch/Nullable.php | 56 +++++++++++++++++++ 6 files changed, 83 insertions(+), 13 deletions(-) create mode 100644 tests/Fixtures/Scratch/Nullable.php diff --git a/Examples/using-links-php81/PullRequest.php b/Examples/using-links-php81/PullRequest.php index aca919c2..8e80bdbd 100644 --- a/Examples/using-links-php81/PullRequest.php +++ b/Examples/using-links-php81/PullRequest.php @@ -8,7 +8,8 @@ class PullRequest { public function __construct( - #[OAT\Property()] public State $state + #[OAT\Property()] + public State $state ) { } diff --git a/Examples/using-links-php81/RepositoriesController.php b/Examples/using-links-php81/RepositoriesController.php index d959acb8..af912f21 100644 --- a/Examples/using-links-php81/RepositoriesController.php +++ b/Examples/using-links-php81/RepositoriesController.php @@ -66,10 +66,14 @@ public function getRepository() ] #[OAT\Link(link: 'RepositoryPullRequests', operationId: 'getPullRequestsByRepository', parameters: ['username' => '$response.body#/owner/username', 'slug' => '$response.body#/slug'])] public function getPullRequestsByRepository( - #[OAT\PathParameter()] string $username, - #[OAT\PathParameter()] string $slug, - #[OAT\PathParameter()] State $state, - #[OAT\QueryParameter()] ?string $label + #[OAT\PathParameter()] + string $username, + #[OAT\PathParameter()] + string $slug, + #[OAT\PathParameter()] + State $state, + #[OAT\QueryParameter()] + ?string $label ) { } diff --git a/src/Analysis.php b/src/Analysis.php index dc78b8f3..b3de6b7f 100644 --- a/src/Analysis.php +++ b/src/Analysis.php @@ -388,7 +388,7 @@ public function unmerged(): Analysis * Split the annotation into two analysis. * One with annotations that are merged and one with annotations that are not merged. * - * @return object {merged: Analysis, unmerged: Analysis} + * @return \stdClass {merged: Analysis, unmerged: Analysis} */ public function split() { diff --git a/src/Annotations/AbstractAnnotation.php b/src/Annotations/AbstractAnnotation.php index ccf4a02f..c3f2af6d 100644 --- a/src/Annotations/AbstractAnnotation.php +++ b/src/Annotations/AbstractAnnotation.php @@ -381,8 +381,12 @@ public function jsonSerialize() if ($this->_context->version === OpenApi::VERSION_3_1_0) { if (isset($data->nullable)) { if (true === $data->nullable) { - $data->type = (array) $data->type; - $data->type[] = 'null'; + if (isset($data->oneOf)) { + $data->oneOf[] = ['type' => 'null']; + } else { + $data->type = (array) $data->type; + $data->type[] = 'null'; + } } unset($data->nullable); } diff --git a/tests/Fixtures/Apis/Attributes/basic.php b/tests/Fixtures/Apis/Attributes/basic.php index a94d4320..bd87d777 100644 --- a/tests/Fixtures/Apis/Attributes/basic.php +++ b/tests/Fixtures/Apis/Attributes/basic.php @@ -88,10 +88,14 @@ class Product implements ProductInterface public $id; public function __construct( - #[OAT\Property()] public int $quantity, - #[OAT\Property(default: null, example: null)] public ?string $brand, - #[OAT\Property()] public Colour $colour, - #[OAT\Property(type: 'string')] public \DateTimeInterface $releasedAt, + #[OAT\Property()] + public int $quantity, + #[OAT\Property(default: null, example: null)] + public ?string $brand, + #[OAT\Property()] + public Colour $colour, + #[OAT\Property(type: 'string')] + public \DateTimeInterface $releasedAt, ) { } } @@ -118,7 +122,8 @@ class ProductController #[OAT\Response(response: 401, description: 'oops')] #[OAF\CustomAttachable(value: 'operation')] public function getProduct( - #[OAT\PathParameter] ?int $product_id + #[OAT\PathParameter] + ?int $product_id ) { } diff --git a/tests/Fixtures/Scratch/Nullable.php b/tests/Fixtures/Scratch/Nullable.php new file mode 100644 index 00000000..f8628928 --- /dev/null +++ b/tests/Fixtures/Scratch/Nullable.php @@ -0,0 +1,56 @@ +