Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: move OpenApi 3.1.0 conversions to processor #1531

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 2 additions & 41 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,11 +367,8 @@ 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;
}
$ref['nullable'] = $data->nullable;

unset($data->nullable);

// preserve other properties
Expand All @@ -387,42 +384,6 @@ public function jsonSerialize()
$data = (object) $ref;
}

if ($this->_context->version === OpenApi::VERSION_3_1_0) {
if (isset($data->nullable)) {
if (true === $data->nullable) {
if (isset($data->oneOf)) {
$data->oneOf[] = ['type' => 'null'];
} elseif (isset($data->anyOf)) {
$data->anyOf[] = ['type' => 'null'];
} elseif (isset($data->allOf)) {
$data->allOf[] = ['type' => 'null'];
} else {
$data->type = (array) $data->type;
$data->type[] = 'null';
}
}
unset($data->nullable);
}

if (isset($data->minimum) && isset($data->exclusiveMinimum)) {
if (true === $data->exclusiveMinimum) {
$data->exclusiveMinimum = $data->minimum;
unset($data->minimum);
} elseif (false === $data->exclusiveMinimum) {
unset($data->exclusiveMinimum);
}
}

if (isset($data->maximum) && isset($data->exclusiveMaximum)) {
if (true === $data->exclusiveMaximum) {
$data->exclusiveMaximum = $data->maximum;
unset($data->maximum);
} elseif (false === $data->exclusiveMaximum) {
unset($data->exclusiveMaximum);
}
}
}

return $data;
}

Expand Down
1 change: 1 addition & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ public function getProcessors(): array
new Processors\MergeXmlContent(),
new Processors\OperationId(),
new Processors\CleanUnmerged(),
new Processors\OpenApi31Processor(),
];
}

Expand Down
102 changes: 102 additions & 0 deletions src/Processors/OpenApi31Processor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
<?php

declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Processors;

use OpenApi\Analysis;
use OpenApi\Annotations as OA;
use OpenApi\Generator;

final class OpenApi31Processor implements ProcessorInterface
{
public function __invoke(Analysis $analysis)
{
if ($analysis->openapi->openapi !== OA\OpenApi::VERSION_3_1_0) {
return;
}

/** @var OA\Schema[] $annotations */
$annotations = $analysis->getAnnotationsOfType(OA\Schema::class);

foreach ($annotations as $annotation) {
$this->processReference($annotation);
$this->processNullable($annotation);
$this->processExclusiveMinimum($annotation);
$this->processExclusiveMaximum($annotation);
}
}

private function processReference(OA\Schema $annotation): void
{
if (Generator::isDefault($annotation->ref)) {
return;
}


}

private function processNullable(OA\Schema $annotation): void
{
$nullable = $annotation->nullable;
$annotation->nullable = Generator::UNDEFINED; // Unregister nullable property

Check failure on line 46 in src/Processors/OpenApi31Processor.php

View workflow job for this annotation

GitHub Actions / static-analysis

Property OpenApi\Annotations\Schema::$nullable (bool) does not accept string.

if (true !== $nullable) {
return;
}

if (!Generator::isDefault($annotation->ref)) {
if (!Generator::isDefault($annotation->oneOf)) {
return;
}

$annotation->oneOf = [new OA\Schema(['ref' => $annotation->ref]), new OA\Schema(['type' => 'null'])];
$annotation->ref = Generator::UNDEFINED;

return;
}

if (is_array($annotation->oneOf)) {
$annotation->oneOf[] = new OA\Schema(['type' => 'null']);
} elseif (is_array($annotation->anyOf)) {

Check failure on line 65 in src/Processors/OpenApi31Processor.php

View workflow job for this annotation

GitHub Actions / static-analysis

Elseif branch is unreachable because previous condition is always true.
$annotation->anyOf[] = new OA\Schema(['type' => 'null']);
} elseif (is_array($annotation->allOf)) {

Check failure on line 67 in src/Processors/OpenApi31Processor.php

View workflow job for this annotation

GitHub Actions / static-analysis

Elseif branch is unreachable because previous condition is always true.
$annotation->allOf[] = new OA\Schema(['type' => 'null']);
} elseif (!Generator::isDefault($annotation->type)) {

Check failure on line 69 in src/Processors/OpenApi31Processor.php

View workflow job for this annotation

GitHub Actions / static-analysis

Elseif branch is unreachable because previous condition is always true.
$annotation->type = (array) $annotation->type;
$annotation->type[] = 'null';
}
}

private function processExclusiveMinimum(OA\Schema $annotation): void
{
if (Generator::isDefault($annotation->minimum) || Generator::isDefault($annotation->exclusiveMinimum)) {
return;
}

if (true === $annotation->exclusiveMinimum) {
$annotation->exclusiveMinimum = $annotation->minimum;
$annotation->minimum = Generator::UNDEFINED;

Check failure on line 83 in src/Processors/OpenApi31Processor.php

View workflow job for this annotation

GitHub Actions / static-analysis

Property OpenApi\Annotations\Schema::$minimum (float|int) does not accept string.
} elseif (false === $annotation->exclusiveMinimum) {
$annotation->exclusiveMinimum = Generator::UNDEFINED;

Check failure on line 85 in src/Processors/OpenApi31Processor.php

View workflow job for this annotation

GitHub Actions / static-analysis

Property OpenApi\Annotations\Schema::$exclusiveMinimum (bool|float|int) does not accept string.
}
}

private function processExclusiveMaximum(OA\Schema $annotation): void
{
if (Generator::isDefault($annotation->maximum) || Generator::isDefault($annotation->exclusiveMaximum)) {
return;
}

if (true === $annotation->exclusiveMaximum) {
$annotation->exclusiveMaximum = $annotation->maximum;
$annotation->maximum = Generator::UNDEFINED;

Check failure on line 97 in src/Processors/OpenApi31Processor.php

View workflow job for this annotation

GitHub Actions / static-analysis

Property OpenApi\Annotations\Schema::$maximum (float|int) does not accept string.
} elseif (false === $annotation->exclusiveMaximum) {
$annotation->exclusiveMaximum = Generator::UNDEFINED;

Check failure on line 99 in src/Processors/OpenApi31Processor.php

View workflow job for this annotation

GitHub Actions / static-analysis

Property OpenApi\Annotations\Schema::$exclusiveMaximum (bool|float|int) does not accept string.
}
}
}
3 changes: 2 additions & 1 deletion tests/Fixtures/Scratch/NullRef31.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ public function refonly()
description: 'Ref plus response',
content: new OAT\JsonContent(
ref: '#/components/schemas/repository',
description: 'The repository',
description: 'Will either respond with something or null',
example: 'Some example about the content',
nullable: true
)
),
Expand Down
5 changes: 3 additions & 2 deletions tests/Fixtures/Scratch/NullRef31.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,12 @@ paths:
description: 'Ref plus response'
content:
application/json:
example: 'Some example about the content'
schema:
oneOf:
- { $ref: '#/components/schemas/repository', description: 'The repository' }
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://github.com/DjordyKoert/swagger-php/blob/2eedbb3a9693762720a7aff34d6a2af8303bc593/src/Annotations/AbstractAnnotation.php#L358

$ref no longer exists after going through the OpenApi31Processor because it gets set to Generator::UNDEFINED which makes it no longer copy the description.

I have another work in progress branch to test if a new annotation Reference can be implemented for this with instead. https://github.com/DjordyKoert/swagger-php/blob/feat/reference-object-annotation/src/Annotations/Reference.php

- { $ref: '#/components/schemas/repository' }
- { type: 'null' }
description: 'The repository'
description: 'Will either respond with something or null'
components:
schemas:
repository: { }
Loading