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

Add Context::getVersion() as safe way to access the OpenApi version #1644

Merged
merged 1 commit into from
Aug 16, 2024
Merged
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
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');
}
}