Skip to content

Commit

Permalink
Default Context (openapi-)version to 3.0.0
Browse files Browse the repository at this point in the history
Also (re-)enables the deprecated trigger in `Context::detect()`
  • Loading branch information
DerManoMann committed Apr 17, 2024
1 parent 5456b8b commit c1f1199
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
12 changes: 6 additions & 6 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ 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 @@ -120,10 +124,6 @@ public function root(): Context
*/
public function isVersion($versions): bool
{
if (!$this->version) {
throw new \RuntimeException('Version is only available reliably for validation and serialization');
}

$versions = (array) $versions;
$currentVersion = $this->version ?: OA\OpenApi::DEFAULT_VERSION;

Expand Down Expand Up @@ -188,13 +188,13 @@ public function __debugInfo()
}

/**
* Create a Context based on the debug_backtrace.
* Create a Context based on `debug_backtrace`.
*
* @deprecated
*/
public static function detect(int $index = 0): Context
{
// trigger_deprecation('zircote/swagger-php', '4.0', 'Context detecting is deprecated');
trigger_deprecation('zircote/swagger-php', '4.8', 'Context detecting is deprecated');

$context = new Context();
$backtrace = debug_backtrace();
Expand Down
4 changes: 3 additions & 1 deletion src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,9 @@ public function generate(iterable $sources, ?Analysis $analysis = null, bool $va
$analysis->process($this->getProcessors());

if ($analysis->openapi) {
$analysis->openapi->openapi = $this->version ?: $analysis->openapi->openapi;
// overwrite default/annotated version
$analysis->openapi->openapi = $this->getVersion() ?: $analysis->openapi->openapi;
// update context to provide the same to validation/serialisation code
$rootContext->version = $analysis->openapi->openapi;
}

Expand Down
2 changes: 2 additions & 0 deletions tests/Analysers/ReflectionAnalyserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ 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 @@ -161,6 +162,7 @@ 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: 1 addition & 3 deletions tests/OpenApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
use OpenApi\Context;
use OpenApi\Analysers\TokenAnalyser;
use OpenApi\Generator;
use OpenApi\Processors\MergeIntoOpenApi;
use PHPUnit\Framework\TestCase;
use Psr\Log\AbstractLogger;
use Psr\Log\LoggerInterface;
Expand Down Expand Up @@ -243,8 +242,7 @@ public function analysisFromFixtures(array $files, array $processors = [], ?Anal

(new Generator($this->getTrackingLogger()))
->setAnalyser($analyzer ?: $this->getAnalyzer())
// run at least MergeIntoOpenApi to have a valid OpenApi version set
->setProcessors($processors ?: [new MergeIntoOpenApi()])
->setProcessors($processors)
->generate($this->fixtures($files), $analysis, false);

return $analysis;
Expand Down

0 comments on commit c1f1199

Please sign in to comment.