From a3a768d124fc926451ca6bf3e4846d396ec69f76 Mon Sep 17 00:00:00 2001 From: DerManoMann Date: Tue, 13 Aug 2024 10:33:28 +1200 Subject: [PATCH] Add `Context::getVersion()` as safe way to access the OpenApi version --- src/Context.php | 19 +++++++++++-------- tests/Analysers/ReflectionAnalyserTest.php | 3 --- tests/ContextTest.php | 4 ++-- tests/Fixtures/Apis/DocBlocks/basic.php | 1 + tests/Processors/AugmentTagsTest.php | 1 - 5 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Context.php b/src/Context.php index 4282ad66..907617c1 100644 --- a/src/Context.php +++ b/src/Context.php @@ -68,10 +68,6 @@ public function __construct(array $properties = [], ?Context $parent = null) $this->parent = $parent; $this->logger = $this->logger ?: new DefaultLogger(); - - if (!$this->version) { - $this->root()->version = OA\OpenApi::DEFAULT_VERSION; - } } /** @@ -140,6 +136,16 @@ public function root(): Context return $this; } + /** + * Get the OpenApi version. + * + * This is a best guess and only final once parsing is complete. + */ + public function getVersion(): string + { + return $this->root()->version ?: OA\OpenApi::DEFAULT_VERSION; + } + /** * Check if one of the given version numbers matches the current OpenAPI version. * @@ -147,10 +153,7 @@ public function root(): Context */ public function isVersion($versions): bool { - $versions = (array) $versions; - $currentVersion = $this->version ?: OA\OpenApi::DEFAULT_VERSION; - - return in_array($currentVersion, $versions); + return in_array($this->getVersion(), (array) $versions); } /** diff --git a/tests/Analysers/ReflectionAnalyserTest.php b/tests/Analysers/ReflectionAnalyserTest.php index 3a07bf63..d4b676c6 100644 --- a/tests/Analysers/ReflectionAnalyserTest.php +++ b/tests/Analysers/ReflectionAnalyserTest.php @@ -90,7 +90,6 @@ public function testApiDocBlockBasic(AnalyserInterface $analyser): void require_once $this->fixture('Apis/DocBlocks/basic.php'); $analysis = (new Generator()) - ->setVersion(OA\OpenApi::VERSION_3_1_0) ->withContext(function (Generator $generator) use ($analyser) { $analyser->setGenerator($generator); $analysis = $analyser->fromFile($this->fixture('Apis/DocBlocks/basic.php'), $this->getContext([], $generator->getVersion())); @@ -119,7 +118,6 @@ public function testApiAttributesBasic(AnalyserInterface $analyser): void /** @var Analysis $analysis */ $analysis = (new Generator()) - ->setVersion(OA\OpenApi::VERSION_3_1_0) ->addAlias('oaf', 'OpenApi\\Tests\\Annotations') ->addNamespace('OpenApi\\Tests\\Annotations\\') ->withContext(function (Generator $generator) use ($analyser) { @@ -162,7 +160,6 @@ public function testApiMixedBasic(AnalyserInterface $analyser): void require_once $this->fixture('Apis/Mixed/basic.php'); $analysis = (new Generator()) - ->setVersion(OA\OpenApi::VERSION_3_1_0) ->withContext(function (Generator $generator) use ($analyser) { $analyser->setGenerator($generator); $analysis = $analyser->fromFile($this->fixture('Apis/Mixed/basic.php'), $this->getContext([], $generator->getVersion())); diff --git a/tests/ContextTest.php b/tests/ContextTest.php index 229ee243..e2c8d108 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -53,12 +53,12 @@ public function testEnsureRoot(): void // assert defaults set $this->assertNotInstanceOf(NullLogger::class, $context->logger); - $this->assertEquals(OA\OpenApi::VERSION_3_0_0, $context->version); + $this->assertEquals(OA\OpenApi::VERSION_3_0_0, $context->getVersion()); $context->ensureRoot($root); // assert inheriting from root $this->assertInstanceOf(NullLogger::class, $context->logger); - $this->assertEquals(OA\OpenApi::VERSION_3_1_0, $context->version); + $this->assertEquals(OA\OpenApi::VERSION_3_1_0, $context->getVersion()); } } diff --git a/tests/Fixtures/Apis/DocBlocks/basic.php b/tests/Fixtures/Apis/DocBlocks/basic.php index 6801b6d5..ef6f1992 100644 --- a/tests/Fixtures/Apis/DocBlocks/basic.php +++ b/tests/Fixtures/Apis/DocBlocks/basic.php @@ -12,6 +12,7 @@ * The Spec. * * @OA\OpenApi( + * openapi="3.1.0", * security={{"bearerAuth": {}}} * ) * @OA\Info( diff --git a/tests/Processors/AugmentTagsTest.php b/tests/Processors/AugmentTagsTest.php index bdecf1a6..0e419e0f 100644 --- a/tests/Processors/AugmentTagsTest.php +++ b/tests/Processors/AugmentTagsTest.php @@ -35,7 +35,6 @@ public function testDedupedAugmentTags(): void $analysis = $this->analysisFromFixtures(['SurplusTag.php'], static::processors()); - echo $analysis->openapi->toYaml(); $this->assertCount(3, $analysis->openapi->tags, 'Expecting 3 unique tags'); } }