Skip to content

Commit b46a36d

Browse files
authored
[4.9] Default openapi version in Context to 3.0.0 (zircote#1563)
1 parent 7819531 commit b46a36d

11 files changed

+25
-37
lines changed

phpstan-baseline.neon

-5
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,6 @@ parameters:
4545
count: 1
4646
path: src/Annotations/Schema.php
4747

48-
-
49-
message: "#^Ternary operator condition is always true\\.$#"
50-
count: 1
51-
path: src/Context.php
52-
5348
-
5449
message: "#^If condition is always true\\.$#"
5550
count: 1

src/Analysis.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ public function __construct(array $annotations = [], ?Context $context = null)
7272

7373
public function addAnnotation(object $annotation, Context $context): void
7474
{
75+
assert(!Generator::isDefault($context->version));
76+
7577
if ($this->annotations->contains($annotation)) {
7678
return;
7779
}
@@ -369,8 +371,7 @@ public function getContext(object $annotation): ?Context
369371
return $context;
370372
}
371373

372-
// Weird, did you use the addAnnotation/addAnnotations methods?
373-
throw new \Exception('Annotation has no context');
374+
throw new \RuntimeException('Annotation has no context - did you use addAnnotation()/addAnnotations()');
374375
}
375376

376377
/**

src/Annotations/AbstractAnnotation.php

+4-14
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,6 @@ public function __set(string $property, $value): void
165165
$this->_context->logger->warning('Ignoring unexpected property "' . $property . '" for ' . $this->identity() . ', expecting "' . implode('", "', array_keys($fields)) . '" in ' . $this->_context);
166166
}
167167

168-
/**
169-
* Check if one of the given version numbers matches the current OpenAPI version.
170-
*
171-
* @param string|array $versions One or more version numbers
172-
*/
173-
public function isOpenApiVersion($versions): bool
174-
{
175-
return $this->_context->isVersion($versions);
176-
}
177-
178168
/**
179169
* Merge given annotations to their mapped properties configured in static::$_nested.
180170
*
@@ -360,7 +350,7 @@ public function jsonSerialize()
360350
if (isset($data->ref)) {
361351
// Only specific https://github.com/OAI/OpenAPI-Specification/blob/3.1.0/versions/3.1.0.md#reference-object
362352
$ref = ['$ref' => $data->ref];
363-
if ($this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
353+
if ($this->_context->isVersion(OpenApi::VERSION_3_1_0)) {
364354
foreach (['summary', 'description'] as $prop) {
365355
if (property_exists($this, $prop)) {
366356
if (!Generator::isDefault($this->{$prop})) {
@@ -371,7 +361,7 @@ public function jsonSerialize()
371361
}
372362
if (property_exists($this, 'nullable') && $this->nullable === true) {
373363
$ref = ['oneOf' => [$ref]];
374-
if ($this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
364+
if ($this->_context->isVersion(OpenApi::VERSION_3_1_0)) {
375365
$ref['oneOf'][] = ['type' => 'null'];
376366
} else {
377367
$ref['nullable'] = $data->nullable;
@@ -391,7 +381,7 @@ public function jsonSerialize()
391381
$data = (object) $ref;
392382
}
393383

394-
if ($this->isOpenApiVersion(OpenApi::VERSION_3_0_0)) {
384+
if ($this->_context->isVersion(OpenApi::VERSION_3_0_0)) {
395385
if (isset($data->exclusiveMinimum) && is_numeric($data->exclusiveMinimum)) {
396386
$data->minimum = $data->exclusiveMinimum;
397387
$data->exclusiveMinimum = true;
@@ -402,7 +392,7 @@ public function jsonSerialize()
402392
}
403393
}
404394

405-
if ($this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
395+
if ($this->_context->isVersion(OpenApi::VERSION_3_1_0)) {
406396
if (isset($data->nullable)) {
407397
if (true === $data->nullable) {
408398
if (isset($data->oneOf)) {

src/Annotations/License.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public function jsonSerialize()
7676
{
7777
$data = parent::jsonSerialize();
7878

79-
if ($this->isOpenApiVersion(OpenApi::VERSION_3_0_0)) {
79+
if ($this->_context->isVersion(OpenApi::VERSION_3_0_0)) {
8080
unset($data->identifier);
8181
}
8282

@@ -90,7 +90,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
9090
{
9191
$valid = parent::validate($stack, $skip, $ref, $context);
9292

93-
if ($this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
93+
if ($this->_context->isVersion(OpenApi::VERSION_3_1_0)) {
9494
if (!Generator::isDefault($this->url) && $this->identifier !== Generator::UNDEFINED) {
9595
$this->_context->logger->warning($this->identity() . ' url and identifier are mutually exclusive');
9696
$valid = false;

src/Annotations/OpenApi.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ public function jsonSerialize()
262262
{
263263
$data = parent::jsonSerialize();
264264

265-
if (false === $this->isOpenApiVersion(OpenApi::VERSION_3_1_0)) {
265+
if (!$this->_context->isVersion(OpenApi::VERSION_3_1_0)) {
266266
unset($data->webhooks);
267267
}
268268

src/Annotations/Schema.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ public function jsonSerialize()
484484
{
485485
$data = parent::jsonSerialize();
486486

487-
if ($this->isOpenApiVersion(OpenApi::VERSION_3_0_0)) {
487+
if ($this->_context->isVersion(OpenApi::VERSION_3_0_0)) {
488488
unset($data->examples);
489489
if (isset($data->const)) {
490490
$data->enum = [$data->const];
@@ -506,7 +506,7 @@ public function validate(array $stack = [], array $skip = [], string $ref = '',
506506
return false;
507507
}
508508

509-
if ($this->isOpenApiVersion(OpenApi::VERSION_3_0_0)) {
509+
if ($this->_context->isVersion(OpenApi::VERSION_3_0_0)) {
510510
if (!Generator::isDefault($this->examples)) {
511511
$this->_context->logger->warning($this->identity() . ' is only allowed for ' . OpenApi::VERSION_3_1_0);
512512

src/Context.php

+6-6
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public function __construct(array $properties = [], ?Context $parent = null)
6464
$this->parent = $parent;
6565

6666
$this->logger = $this->logger ?: new DefaultLogger();
67+
68+
if (!$this->version) {
69+
$this->root()->version = OA\OpenApi::DEFAULT_VERSION;
70+
}
6771
}
6872

6973
/**
@@ -120,10 +124,6 @@ public function root(): Context
120124
*/
121125
public function isVersion($versions): bool
122126
{
123-
if (!$this->version) {
124-
throw new \RuntimeException('Version is only available reliably for validation and serialization');
125-
}
126-
127127
$versions = (array) $versions;
128128
$currentVersion = $this->version ?: OA\OpenApi::DEFAULT_VERSION;
129129

@@ -188,13 +188,13 @@ public function __debugInfo()
188188
}
189189

190190
/**
191-
* Create a Context based on the debug_backtrace.
191+
* Create a Context based on `debug_backtrace`.
192192
*
193193
* @deprecated
194194
*/
195195
public static function detect(int $index = 0): Context
196196
{
197-
// trigger_deprecation('zircote/swagger-php', '4.0', 'Context detecting is deprecated');
197+
trigger_deprecation('zircote/swagger-php', '4.9', 'Context detecting is deprecated');
198198

199199
$context = new Context();
200200
$backtrace = debug_backtrace();

src/Generator.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,9 @@ public function generate(iterable $sources, ?Analysis $analysis = null, bool $va
456456
$analysis->process($this->getProcessors());
457457

458458
if ($analysis->openapi) {
459-
$analysis->openapi->openapi = $this->version ?: $analysis->openapi->openapi;
459+
// overwrite default/annotated version
460+
$analysis->openapi->openapi = $this->getVersion() ?: $analysis->openapi->openapi;
461+
// update context to provide the same to validation/serialisation code
460462
$rootContext->version = $analysis->openapi->openapi;
461463
}
462464

src/Processors/AugmentProperties.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ protected function augmentType(Analysis $analysis, OA\Property $property, Contex
144144
$property->minimum = 0;
145145
} elseif ($type === 'non-zero-int') {
146146
$property->type = 'integer';
147-
if ($property->isOpenApiVersion(OA\OpenApi::VERSION_3_1_0)) {
147+
if ($property->_context->isVersion(OA\OpenApi::VERSION_3_1_0)) {
148148
$property->not = ['const' => 0];
149149
} else {
150150
$property->not = ['enum' => [0]];

tests/Analysers/ReflectionAnalyserTest.php

+2
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ public function testApiAttributesBasic(AnalyserInterface $analyser): void
119119

120120
/** @var Analysis $analysis */
121121
$analysis = (new Generator())
122+
->setVersion(OA\OpenApi::VERSION_3_1_0)
122123
->addAlias('oaf', 'OpenApi\\Tests\\Annotations')
123124
->addNamespace('OpenApi\\Tests\\Annotations\\')
124125
->withContext(function (Generator $generator) use ($analyser) {
@@ -161,6 +162,7 @@ public function testApiMixedBasic(AnalyserInterface $analyser): void
161162
require_once $this->fixture('Apis/Mixed/basic.php');
162163

163164
$analysis = (new Generator())
165+
->setVersion(OA\OpenApi::VERSION_3_1_0)
164166
->withContext(function (Generator $generator) use ($analyser) {
165167
$analyser->setGenerator($generator);
166168
$analysis = $analyser->fromFile($this->fixture('Apis/Mixed/basic.php'), $this->getContext([], $generator->getVersion()));

tests/OpenApiTestCase.php

+1-3
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
use OpenApi\Context;
1717
use OpenApi\Analysers\TokenAnalyser;
1818
use OpenApi\Generator;
19-
use OpenApi\Processors\MergeIntoOpenApi;
2019
use PHPUnit\Framework\TestCase;
2120
use Psr\Log\AbstractLogger;
2221
use Psr\Log\LoggerInterface;
@@ -243,8 +242,7 @@ public function analysisFromFixtures(array $files, array $processors = [], ?Anal
243242

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

250248
return $analysis;

0 commit comments

Comments
 (0)