Skip to content

Commit

Permalink
Add Context::getVersion() as safe way to access the OpenApi version (
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann authored Aug 16, 2024
1 parent 5ba05b8 commit 08b48bb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 14 deletions.
19 changes: 11 additions & 8 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

/**
Expand Down Expand Up @@ -140,17 +136,24 @@ 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.
*
* @param string|array $versions One or more version numbers
*/
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);
}

/**
Expand Down
3 changes: 0 additions & 3 deletions tests/Analysers/ReflectionAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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()));
Expand Down
4 changes: 2 additions & 2 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
1 change: 1 addition & 0 deletions tests/Fixtures/Apis/DocBlocks/basic.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* The Spec.
*
* @OA\OpenApi(
* openapi="3.1.0",
* security={{"bearerAuth": {}}}
* )
* @OA\Info(
Expand Down
1 change: 0 additions & 1 deletion tests/Processors/AugmentTagsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}
}

0 comments on commit 08b48bb

Please sign in to comment.