Skip to content

Commit

Permalink
Allow a ref to be nullable (#1391)
Browse files Browse the repository at this point in the history
  • Loading branch information
momala454 authored Feb 20, 2023
1 parent 9983523 commit f2bcbd5
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 8 deletions.
15 changes: 12 additions & 3 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -356,8 +356,8 @@ public function jsonSerialize()
if (isset($data->ref)) {
// Only specific https://github.com/OAI/OpenAPI-Specification/blob/3.1.0/versions/3.1.0.md#reference-object
$ref = ['$ref' => $data->ref];
if ($this->_context->version == OpenApi::VERSION_3_1_0) {
$defaultValues = get_class_vars(get_class($this));
$defaultValues = get_class_vars(get_class($this));
if ($this->_context->version === OpenApi::VERSION_3_1_0) {
foreach (['summary', 'description'] as $prop) {
if (property_exists($this, $prop)) {
if ($this->{$prop} !== $defaultValues[$prop]) {
Expand All @@ -366,10 +366,19 @@ public function jsonSerialize()
}
}
}
if (property_exists($this, 'nullable') && $this->nullable === true) {
$ref = ['oneOf' => [$ref]];
if ($this->_context->version == OpenApi::VERSION_3_1_0) {
$ref['oneOf'][] = ['type' => 'null'];
} else {
$ref['nullable'] = $data->nullable;
}
unset($data->nullable);
}
$data = (object) $ref;
}

if ($this->_context->version == OpenApi::VERSION_3_1_0) {
if ($this->_context->version === OpenApi::VERSION_3_1_0) {
if (isset($data->nullable)) {
if (true === $data->nullable) {
$data->type = (array) $data->type;
Expand Down
8 changes: 6 additions & 2 deletions tests/Fixtures/Scratch/NullRef.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/repository'
oneOf:
- { $ref: '#/components/schemas/repository' }
nullable: true
/api/refplus:
get:
operationId: refplus
Expand All @@ -22,7 +24,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/repository'
oneOf:
- { $ref: '#/components/schemas/repository' }
nullable: true
components:
schemas:
repository: { }
9 changes: 6 additions & 3 deletions tests/Fixtures/Scratch/NullRef31.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/repository'
oneOf:
- { $ref: '#/components/schemas/repository' }
- { type: 'null' }
/api/refplus:
get:
operationId: refplus
Expand All @@ -22,8 +24,9 @@ paths:
content:
application/json:
schema:
$ref: '#/components/schemas/repository'
description: 'The repository'
oneOf:
- { $ref: '#/components/schemas/repository', description: 'The repository' }
- { type: 'null' }
components:
schemas:
repository: { }

0 comments on commit f2bcbd5

Please sign in to comment.