From b25ac10ab2eff8bf4fdaf0bc5ed0472e1485f86e Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Tue, 5 Dec 2023 10:11:27 +1300 Subject: [PATCH] . --- Examples/webhooks/OpenApiSpec.php | 41 +++--- Examples/webhooks/Pet.php | 34 +++-- Examples/webhooks/webhooks.yaml | 12 +- Examples/webhooks/xwebhooks.yaml | 31 ----- Examples/webhooks81/OpenApiSpec.php | 34 +++++ Examples/webhooks81/Pet.php | 18 +++ Examples/webhooks81/webhooks.yaml | 32 +++++ src/Annotations/Attachable.php | 1 - src/Annotations/OpenApi.php | 2 - src/Annotations/Reference.php | 56 --------- src/Annotations/Webhook.php | 33 +++-- src/Attributes/Reference.php | 34 ----- src/Attributes/Webhook.php | 3 +- src/Serializer.php | 1 - tests/Annotations/OpenApiTest.php | 2 +- tests/ExamplesTest.php | 189 +++++++++++++++------------- tests/GeneratorTest.php | 2 +- 17 files changed, 264 insertions(+), 261 deletions(-) delete mode 100644 Examples/webhooks/xwebhooks.yaml create mode 100644 Examples/webhooks81/OpenApiSpec.php create mode 100644 Examples/webhooks81/Pet.php create mode 100644 Examples/webhooks81/webhooks.yaml delete mode 100644 src/Annotations/Reference.php delete mode 100644 src/Attributes/Reference.php diff --git a/Examples/webhooks/OpenApiSpec.php b/Examples/webhooks/OpenApiSpec.php index 409c62bb..eb12499b 100644 --- a/Examples/webhooks/OpenApiSpec.php +++ b/Examples/webhooks/OpenApiSpec.php @@ -1,43 +1,34 @@ -openapi === self::VERSION_3_0_0 && Generator::isDefault($this->paths)) { $this->_context->logger->warning('Required @OA\PathItem() not found'); - - return false; } if ($this->openapi === self::VERSION_3_1_0 diff --git a/src/Annotations/Reference.php b/src/Annotations/Reference.php deleted file mode 100644 index 0b84cd8f..00000000 --- a/src/Annotations/Reference.php +++ /dev/null @@ -1,56 +0,0 @@ - ['attachables'], - ]; - - /** - * @inheritdoc - */ - public static $_types = [ - 'ref' => 'string', - 'summary' => 'string', - 'description' => 'string', - ]; -} diff --git a/src/Annotations/Webhook.php b/src/Annotations/Webhook.php index 17932dec..12019383 100644 --- a/src/Annotations/Webhook.php +++ b/src/Annotations/Webhook.php @@ -8,6 +8,9 @@ use OpenApi\Generator; +/** + * @Annotation + */ class Webhook extends AbstractAnnotation { /** @@ -26,15 +29,6 @@ class Webhook extends AbstractAnnotation */ public $path = Generator::UNDEFINED; - /** - * The reference. - * - * Required unless `path` is set. - * - * @var Reference - */ - public $reference = Generator::UNDEFINED; - /** * @inheritdoc */ @@ -47,7 +41,6 @@ class Webhook extends AbstractAnnotation */ public static $_nested = [ PathItem::class => 'path', - Reference::class => 'reference', Attachable::class => ['attachables'], ]; @@ -57,4 +50,24 @@ class Webhook extends AbstractAnnotation public static $_types = [ 'webhook' => 'string', ]; + + /** + * @inheritdoc + */ + #[\ReturnTypeWillChange] + public function jsonSerialize() + { + $data = parent::jsonSerialize(); + + if (isset($data->path)) { + foreach (get_object_vars($data->path) as $property => $value) { + if ('_' != $property[0] && !Generator::isDefault($value)) { + $data->{$property} = $value; + } + } + unset($data->path); + } + + return $data; + } } diff --git a/src/Attributes/Reference.php b/src/Attributes/Reference.php deleted file mode 100644 index 8ef06104..00000000 --- a/src/Attributes/Reference.php +++ /dev/null @@ -1,34 +0,0 @@ -|null $x - * @param Attachable[]|null $attachables - */ - public function __construct( - ?string $ref = null, - ?string $summary = null, - ?string $description = null, - // annotation - ?array $x = null, - ?array $attachables = null - ) { - parent::__construct([ - 'ref' => $ref ?? Generator::UNDEFINED, - 'summary' => $summary ?? Generator::UNDEFINED, - 'description' => $description ?? Generator::UNDEFINED, - 'x' => $x ?? Generator::UNDEFINED, - 'value' => $this->combine($attachables), - ]); - } -} diff --git a/src/Attributes/Webhook.php b/src/Attributes/Webhook.php index e26d2230..7e4b4222 100644 --- a/src/Attributes/Webhook.php +++ b/src/Attributes/Webhook.php @@ -18,7 +18,6 @@ class Webhook extends \OpenApi\Annotations\Webhook public function __construct( ?string $webhook = null, ?PathItem $path = null, - ?Reference $reference = null, // annotation ?array $x = null, ?array $attachables = null @@ -26,7 +25,7 @@ public function __construct( parent::__construct([ 'webhook' => $webhook ?? Generator::UNDEFINED, 'x' => $x ?? Generator::UNDEFINED, - 'value' => $this->combine($attachables, $path, $reference), + 'value' => $this->combine($attachables, $path), ]); } } diff --git a/src/Serializer.php b/src/Serializer.php index 8f4f6622..52af1805 100644 --- a/src/Serializer.php +++ b/src/Serializer.php @@ -49,7 +49,6 @@ class Serializer OA\Property::class, OA\Put::class, OA\RequestBody::class, - OA\Reference::class, OA\Response::class, OA\Schema::class, OA\SecurityScheme::class, diff --git a/tests/Annotations/OpenApiTest.php b/tests/Annotations/OpenApiTest.php index 1d346040..1473608e 100644 --- a/tests/Annotations/OpenApiTest.php +++ b/tests/Annotations/OpenApiTest.php @@ -13,8 +13,8 @@ class OpenApiTest extends OpenApiTestCase { public function testValidVersion(): void { - $this->assertOpenApiLogEntryContains('Required @OA\Info() not found'); $this->assertOpenApiLogEntryContains('Required @OA\PathItem() not found'); + $this->assertOpenApiLogEntryContains('Required @OA\Info() not found'); $openapi = new OA\OpenApi(['_context' => $this->getContext()]); $openapi->openapi = '3.0.0'; diff --git a/tests/ExamplesTest.php b/tests/ExamplesTest.php index c9943eee..47b540b3 100644 --- a/tests/ExamplesTest.php +++ b/tests/ExamplesTest.php @@ -21,140 +21,166 @@ class ExamplesTest extends OpenApiTestCase public function exampleDetails(): iterable { yield 'example-object' => [ - OA\OpenApi::VERSION_3_0_0, - 'example-object', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'example-object', 'example-object.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'misc' => [ - OA\OpenApi::VERSION_3_0_0, - 'misc', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'misc', 'misc.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'nesting' => [ - OA\OpenApi::VERSION_3_0_0, - 'nesting', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'nesting', 'nesting.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'petstore-3.0' => [ - OA\OpenApi::VERSION_3_0_0, - 'petstore-3.0', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'petstore-3.0', 'petstore-3.0.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'petstore.swagger.io' => [ - OA\OpenApi::VERSION_3_0_0, - 'petstore.swagger.io', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'petstore.swagger.io', 'petstore.swagger.io.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'swagger-spec/petstore' => [ - OA\OpenApi::VERSION_3_0_0, - 'swagger-spec/petstore', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'swagger-spec/petstore', 'petstore.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'swagger-spec/petstore-simple' => [ - OA\OpenApi::VERSION_3_0_0, - 'swagger-spec/petstore-simple', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'swagger-spec/petstore-simple', 'petstore-simple.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'swagger-spec/petstore-simple-3.1.0' => [ - OA\OpenApi::VERSION_3_1_0, - 'swagger-spec/petstore-simple', + 'version' => OA\OpenApi::VERSION_3_1_0, + 'example' => 'swagger-spec/petstore-simple', 'petstore-simple-3.1.0.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'swagger-spec/petstore-with-external-docs' => [ - OA\OpenApi::VERSION_3_0_0, - 'swagger-spec/petstore-with-external-docs', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'swagger-spec/petstore-with-external-docs', 'petstore-with-external-docs.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'polymorphism' => [ - OA\OpenApi::VERSION_3_0_0, - 'polymorphism', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'polymorphism', 'polymorphism.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['reflection'], ]; yield 'polymorphism-3.1.0' => [ - OA\OpenApi::VERSION_3_1_0, - 'polymorphism', + 'version' => OA\OpenApi::VERSION_3_1_0, + 'example' => 'polymorphism', 'polymorphism-3.1.0.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['reflection'], ]; yield 'using-interfaces' => [ - OA\OpenApi::VERSION_3_0_0, - 'using-interfaces', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'using-interfaces', 'using-interfaces.yaml', - false, - [], - ]; - - yield 'using-refs' => [ - OA\OpenApi::VERSION_3_0_0, - 'using-refs', - 'using-refs.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'using-traits' => [ - OA\OpenApi::VERSION_3_0_0, - 'using-traits', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'using-traits', 'using-traits.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; yield 'using-links' => [ - OA\OpenApi::VERSION_3_0_0, - 'using-links', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'using-links', 'using-links.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], ]; if (\PHP_VERSION_ID >= 80100) { + yield 'using-refs' => [ + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'using-refs', + 'using-refs.yaml', + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token', 'reflection'], + ]; + yield 'webhooks' => [ - OA\OpenApi::VERSION_3_1_0, - 'webhooks', + 'version' => OA\OpenApi::VERSION_3_1_0, + 'example' => 'webhooks', + 'webhooks.yaml', + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['token' ,'reflection'], + ]; + + yield 'webhooks81' => [ + 'version' => OA\OpenApi::VERSION_3_1_0, + 'example' => 'webhooks81', 'webhooks.yaml', - false, - [], + 'debug' => false, + 'expectedLog' => [], + 'analysers' => ['reflection'], ]; yield 'using-links-php81' => [ - OA\OpenApi::VERSION_3_0_0, - 'using-links-php81', + 'version' => OA\OpenApi::VERSION_3_0_0, + 'example' => 'using-links-php81', 'using-links-php81.yaml', - true, - ['JetBrains\PhpStorm\ArrayShape'], + 'debug' => true, + 'expectedLog' => ['JetBrains\PhpStorm\ArrayShape'], + 'analysers' => ['reflection'], ]; } } @@ -166,18 +192,11 @@ public function exampleMappings(): iterable 'reflection' => new ReflectionAnalyser([new DocBlockAnnotationFactory(), new AttributeAnnotationFactory()]), ]; - foreach ($this->exampleDetails() as $eKey => $example) { - foreach ($analysers as $aKey => $analyser) { - if (0 === strpos($eKey, 'polymorphism') && 'token' == $aKey) { - continue; - } - if ((\PHP_VERSION_ID < 80100 || 'token' == $aKey) && 'using-refs' == $eKey) { - continue; - } - if ('using-links-php81' == $eKey && 'token' == $aKey) { - continue; - } - yield $eKey . ':' . $aKey => array_merge($example, [$analyser]); + foreach ($this->exampleDetails() as $exampleKey => $example) { + $exampleAnalysers = $example['analysers']; + unset($example['analysers']); + foreach ($exampleAnalysers as $analyserKey) { + yield $exampleKey . ':' . $analyserKey => array_merge($example, [$analysers[$analyserKey]]); } } } @@ -206,7 +225,7 @@ public function testExamples(string $version, string $example, string $spec, boo ->setVersion($version) ->setAnalyser($analyser) ->generate([$path]); - file_put_contents($path . '/' . $spec, $openapi->toYaml()); + file_put_contents($path . '/' . $spec, $openapi->toYaml()); $this->assertSpecEquals( $openapi, file_get_contents($path . '/' . $spec), diff --git a/tests/GeneratorTest.php b/tests/GeneratorTest.php index 3dbd1c3f..be8c4333 100644 --- a/tests/GeneratorTest.php +++ b/tests/GeneratorTest.php @@ -38,8 +38,8 @@ public function testScan(string $sourceDir, iterable $sources): void public function testScanInvalidSource(): void { $this->assertOpenApiLogEntryContains('Skipping invalid source: /tmp/__swagger_php_does_not_exist__'); - $this->assertOpenApiLogEntryContains('Required @OA\Info() not found'); $this->assertOpenApiLogEntryContains('Required @OA\PathItem() not found'); + $this->assertOpenApiLogEntryContains('Required @OA\Info() not found'); (new Generator($this->getTrackingLogger())) ->setAnalyser($this->getAnalyzer())