Skip to content

Commit

Permalink
Ensure all annotations added to Analysis have a root context (#1586)
Browse files Browse the repository at this point in the history
  • Loading branch information
DerManoMann committed May 2, 2024
1 parent 749542e commit 256d42c
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/Analysis.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,12 @@ public function __construct(array $annotations = [], ?Context $context = null)

public function addAnnotation(object $annotation, Context $context): void
{
assert(!Generator::isDefault($context->version));

if ($this->annotations->contains($annotation)) {
return;
}

$context->ensureRoot($this->context);

if ($annotation instanceof OA\OpenApi) {
$this->openapi = $this->openapi ?: $annotation;
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/Annotations/AbstractAnnotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public function __construct(array $properties)
} elseif (Generator::$context) {
$this->_context = Generator::$context;
} else {
$this->_context = Context::detect(1);
$this->_context = new Context(['generated' => true]);
}

if ($this->_context->is('annotations') === false) {
Expand Down
24 changes: 22 additions & 2 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ class Context
*/
private $parent;

/**
* @deprecated
*/
public function clone()
{
return new Context(get_object_vars($this), $this->parent);
Expand All @@ -70,6 +73,25 @@ public function __construct(array $properties = [], ?Context $parent = null)
}
}

/**
* Ensure this context is part of the context tree.
*/
public function ensureRoot(?Context $rootContext): void
{
if ($rootContext === $this) {
return;
}

if (!$this->parent) {
// use root fallback for these...
foreach (['logger', 'version'] as $property) {
unset($this->{$property});
}

$this->parent = $rootContext;
}
}

/**
* Check if a property is set directly on this context and not its parent context.
*
Expand Down Expand Up @@ -194,8 +216,6 @@ public function __debugInfo()
*/
public static function detect(int $index = 0): Context
{
trigger_deprecation('zircote/swagger-php', '4.9', 'Context detecting is deprecated');

$context = new Context();
$backtrace = debug_backtrace();
$position = $backtrace[$index];
Expand Down
2 changes: 2 additions & 0 deletions src/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,9 @@ public function generate(iterable $sources, ?Analysis $analysis = null, bool $va
'version' => $this->getVersion(),
'logger' => $this->getLogger(),
]);

$analysis = $analysis ?: new Analysis([], $rootContext);
$analysis->context = $analysis->context ?: $rootContext;

$this->configStack->push($this);
try {
Expand Down
18 changes: 18 additions & 0 deletions tests/ContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@

namespace OpenApi\Tests;

use OpenApi\Annotations as OA;
use OpenApi\Analysers\TokenAnalyser;
use OpenApi\Context;
use OpenApi\Generator;
use Psr\Log\NullLogger;

class ContextTest extends OpenApiTestCase
{
Expand Down Expand Up @@ -43,4 +45,20 @@ public function testFullyQualifiedName(): void
$this->assertSame('\\OpenApi\\Generator', $context->fullyQualifiedName('OpenApiGenerator'));
$this->assertSame('\\OpenApi\\Annotations\\QualifiedAlias', $context->fullyQualifiedName('OA\\QualifiedAlias'));
}

public function testEnsureRoot(): void
{
$root = new Context(['logger' => new NullLogger(), 'version' => OA\OpenApi::VERSION_3_1_0]);
$context = new Context(['logger' => $this->getTrackingLogger()]);

// assert defaults set
$this->assertNotInstanceOf(NullLogger::class, $context->logger);
$this->assertEquals(OA\OpenApi::VERSION_3_0_0, $context->version);

$context->ensureRoot($root);

// assert inheriting from root
$this->assertInstanceOf(NullLogger::class, $context->logger);
$this->assertEquals(OA\OpenApi::VERSION_3_1_0, $context->version);
}
}

0 comments on commit 256d42c

Please sign in to comment.