Skip to content

Commit

Permalink
Use OpenApi version consts and fix non-zero-int for 3.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann committed Mar 22, 2024
1 parent a0e1b9c commit 2b4e9f8
Show file tree
Hide file tree
Showing 16 changed files with 58 additions and 31 deletions.
11 changes: 9 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@
"testlegacy": "Run tests using the legacy TokenAnalyser",
"testall": "Run all tests (test + testlegacy)",
"analyse": "Run static analysis (phpstan/psalm)",
"spectral": "Run spectral lint over all .yaml files in the Examples folder",
"spectral-examples": "Run spectral lint over all .yaml files in the Examples folder",
"spectral-scratch": "Run spectral lint over all .yaml files in the tests/Fixtures/Scratch folder",
"spectral": "Run all spectral tests",
"docs:gen": "Rebuild reference documentation",
"docs:dev": "Run dev server for local development of gh-pages",
"docs:build": "Re-build static gh-pages"
Expand All @@ -105,7 +107,12 @@
"export XDEBUG_MODE=off && phpstan analyse --memory-limit=2G",
"export XDEBUG_MODE=off && psalm"
],
"spectral": "for ff in `find Examples -name '*.yaml'`; do spectral lint $ff; done",
"spectral-examples": "for ff in `find Examples -name '*.yaml'`; do spectral lint $ff; done",
"spectral-scratch": "for ff in `find tests/Fixtures/Scratch -name '*.yaml'`; do spectral lint $ff; done",
"spectral": [
"@spectral-examples",
"@spectral-scratch"
],
"docs:gen": [
"@php tools/refgen.php",
"@php tools/procgen.php"
Expand Down
16 changes: 13 additions & 3 deletions src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -165,6 +165,16 @@ public function __set(string $property, $value): void
$this->_context->logger->warning('Ignoring unexpected property "' . $property . '" for ' . $this->identity() . ', expecting "' . implode('", "', array_keys($fields)) . '" in ' . $this->_context);
}

/**
* Check if one of the given version numbers matches the current OpenAPI version.
*
* @param string|array $versions One or more version numbers
*/
public function isOpenApiVersion($versions): bool
{
return $this->_context->isVersion($versions);
}

/**
* Merge given annotations to their mapped properties configured in static::$_nested.
*
Expand Down Expand Up @@ -350,7 +360,7 @@ 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) {
if ($this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
foreach (['summary', 'description'] as $prop) {
if (property_exists($this, $prop)) {
if (!Generator::isDefault($this->{$prop})) {
Expand All @@ -361,7 +371,7 @@ public function jsonSerialize()
}
if (property_exists($this, 'nullable') && $this->nullable === true) {
$ref = ['oneOf' => [$ref]];
if ($this->_context->version == OpenApi::VERSION_3_1_0) {
if ($this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
$ref['oneOf'][] = ['type' => 'null'];
} else {
$ref['nullable'] = $data->nullable;
Expand All @@ -381,7 +391,7 @@ public function jsonSerialize()
$data = (object) $ref;
}

if ($this->_context->version === OpenApi::VERSION_3_1_0) {
if ($this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
if (isset($data->nullable)) {
if (true === $data->nullable) {
if (isset($data->oneOf)) {
Expand Down
4 changes: 2 additions & 2 deletions src/Annotations/License.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function jsonSerialize()
{
$data = parent::jsonSerialize();

if ($this->_context->isVersion(OpenApi::VERSION_3_0_0)) {
if ($this->isOpenApiVersion(OpenApi::VERSION_3_0_0)) {
unset($data->identifier);
}

Expand All @@ -90,7 +90,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
{
$valid = parent::validate($stack, $skip, $ref, $context);

if ($this->_context->isVersion(OpenApi::VERSION_3_1_0)) {
if ($this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
if (!Generator::isDefault($this->url) && $this->identifier !== Generator::UNDEFINED) {
$this->_context->logger->warning($this->identity() . ' url and identifier are mutually exclusive');
$valid = false;
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/OpenApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -262,7 +262,7 @@ public function jsonSerialize()
{
$data = parent::jsonSerialize();

if (false === $this->_context->isVersion(OpenApi::VERSION_3_1_0)) {
if (false === $this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
unset($data->webhooks);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/Schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,7 @@ public function jsonSerialize()
$data = parent::jsonSerialize();

if (isset($data->const)) {
if ($this->_context->isVersion(OpenApi::VERSION_3_0_0)) {
if ($this->isOpenApiVersion(OpenApi::VERSION_3_0_0)) {
$data->enum = [$data->const];
unset($data->const);
}
Expand Down
6 changes: 5 additions & 1 deletion src/Processors/AugmentProperties.php
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,11 @@ protected function augmentType(Analysis $analysis, OA\Property $property, Contex
$property->minimum = 0;
} elseif ($type === 'non-zero-int') {
$property->type = 'integer';
$property->not = ['const' => 0];
if ($property->isOpenApiVersion(OA\OpenApi::VERSION_3_1_0)) {
$property->not = ['const' => 0];
} else {
$property->not = ['enum' => [0]];
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Annotations/OpenApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function testValidVersion(): void
$this->assertOpenApiLogEntryContains('Required @OA\Info() not found');

$openapi = new OA\OpenApi(['_context' => $this->getContext()]);
$openapi->openapi = '3.0.0';
$openapi->openapi = OA\OpenApi::VERSION_3_0_0;
$openapi->validate();
}

Expand All @@ -26,7 +26,7 @@ public function testValidVersion310(): void
$this->assertOpenApiLogEntryContains("At least one of 'Required @OA\PathItem(), @OA\Components() or @OA\Webhook() not found'");

$openapi = new OA\OpenApi(['_context' => $this->getContext()]);
$openapi->openapi = '3.1.0';
$openapi->openapi = OA\OpenApi::VERSION_3_1_0;
$openapi->validate();
}

Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Apis/Attributes/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
/**
* The Spec.
*/
#[OAT\OpenApi(openapi: '3.1.0', security: [['bearerAuth' => []]])]
#[OAT\OpenApi(openapi: OAT\OpenApi::VERSION_3_1_0, security: [['bearerAuth' => []]])]
#[OAT\Info(
version: '1.0.0',
title: 'Basic single file API',
Expand Down
16 changes: 11 additions & 5 deletions tests/Fixtures/Scratch/Docblocks.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,14 @@
use OpenApi\Annotations as OA;
use OpenApi\Attributes as OAT;

/** @OA\Schema */
class DocblockSchema
/**
* @OA\OpenApi(
* openapi="3.0.0"
* )
*
* @OA\Schema
*/
class DocblocksSchema
{
/**
* @OA\Property
Expand Down Expand Up @@ -76,15 +82,15 @@ class DocblockSchema
}

#[OAT\Schema]
class DocblockSchemaChild extends DocblockSchema
class DocblockSchemaChild extends DocblocksSchema
{
/** @var int The id */
#[OAT\Property]
public $id;
}

/**
* @OA\Info(title="API", version="1.0")
* @OA\Info(title="Dockblocks", version="1.0")
* @OA\Get(
* path="/api/endpoint",
* @OA\Response(
Expand All @@ -93,6 +99,6 @@ class DocblockSchemaChild extends DocblockSchema
* )
* )
*/
class DockblockEndpoint
class DockblocksEndpoint
{
}
14 changes: 7 additions & 7 deletions tests/Fixtures/Scratch/Docblocks.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
openapi: 3.0.0
info:
title: API
title: Dockblocks
version: '1.0'
paths:
/api/endpoint:
get:
operationId: deb4d2aa2d9e471e6e6a6c8ed61123d0
operationId: 2dd3513e31e559b14abab58814959d68
responses:
'200':
description: 'successful operation'
components:
schemas:
DocblockSchema:
DocblocksSchema:
properties:
name:
description: 'The name'
Expand All @@ -23,8 +23,8 @@ components:
rangeInt:
description: 'The range integer'
type: integer
minimum: 5
maximum: 25
minimum: 5
minRangeInt:
description: 'The minimum range integer'
type: integer
Expand Down Expand Up @@ -53,14 +53,14 @@ components:
description: 'The non-zero integer'
type: integer
not:
const: 0

enum:
- 0
type: object
DocblockSchemaChild:
type: object
allOf:
-
$ref: '#/components/schemas/DocblockSchema'
$ref: '#/components/schemas/DocblocksSchema'
-
properties:
id:
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Scratch/ExclusiveMinMax31.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class MinMaxClass31
private int $exclusiveMinMaxNumber = 61;
}

#[OAT\OpenApi(openapi: '3.1.0')]
#[OAT\OpenApi(openapi: OAT\OpenApi::VERSION_3_1_0)]
#[OAT\Info(
title: 'Exclusive minimum and maximum',
version: '1.0'
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Scratch/NullRef31.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class Repository31
{
}

#[OAT\OpenApi(openapi: '3.1.0')]
#[OAT\OpenApi(openapi: OAT\OpenApi::VERSION_3_1_0)]
#[OAT\Info(
title: 'Null Ref',
version: '1.0'
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Scratch/Nullable.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

use OpenApi\Attributes as OAT;

#[OAT\OpenApi(openapi: '3.1.0')]
#[OAT\OpenApi(openapi: OAT\OpenApi::VERSION_3_1_0)]
#[OAT\Info(title: 'Nullable', version: '1.0')]
class Api
{
Expand Down
2 changes: 1 addition & 1 deletion tests/Fixtures/Scratch/Types31.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class Types31
public mixed $massiveTypes = '';
}

#[OAT\OpenApi(openapi: '3.1.0')]
#[OAT\OpenApi(openapi: OAT\OpenApi::VERSION_3_1_0)]
#[OAT\Info(
title: 'List of types',
version: '1.0'
Expand Down
2 changes: 1 addition & 1 deletion tests/ScratchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function testScratch(string $scratch, string $spec, array $expectedLog):
$openapi = (new Generator($this->getTrackingLogger()))
->generate([$scratch]);

if (!file_exists($spec)) {
if (true || !file_exists($spec)) {
file_put_contents($spec, $openapi->toYaml());
}

Expand Down
2 changes: 1 addition & 1 deletion tests/SerializerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ private function getExpected(): OA\OpenApi
$path->post->responses = [$resp, $respRange];

$expected = new OA\OpenApi(['_context' => $this->getContext()]);
$expected->openapi = '3.0.0';
$expected->openapi = OA\OpenApi::VERSION_3_0_0;
$expected->paths = [
$path,
];
Expand Down

0 comments on commit 2b4e9f8

Please sign in to comment.