diff --git a/src/Context.php b/src/Context.php index 907617c15..5b97e490b 100644 --- a/src/Context.php +++ b/src/Context.php @@ -164,8 +164,9 @@ public function isVersion($versions): bool public function getDebugLocation(): string { $location = ''; - if ($this->class && ($this->method || $this->property)) { - $location .= $this->fullyQualifiedName($this->class); + $fqn = $this->fullyQualifiedName($this->class ?? $this->interface ?? $this->trait ?? $this->enum); + if ($fqn && ($this->method || $this->property)) { + $location .= $fqn; if ($this->method) { $location .= ($this->static ? '::' : '->') . $this->method . '()'; } elseif ($this->property) { diff --git a/tests/ContextTest.php b/tests/ContextTest.php index e2c8d1087..f8ef70c4f 100644 --- a/tests/ContextTest.php +++ b/tests/ContextTest.php @@ -10,6 +10,7 @@ use OpenApi\Analysers\TokenAnalyser; use OpenApi\Context; use OpenApi\Generator; +use OpenApi\Tests\Fixtures\Customer; use Psr\Log\NullLogger; class ContextTest extends OpenApiTestCase @@ -61,4 +62,24 @@ public function testEnsureRoot(): void $this->assertInstanceOf(NullLogger::class, $context->logger); $this->assertEquals(OA\OpenApi::VERSION_3_1_0, $context->getVersion()); } + + public function testDebugLocation(): void + { + $this->assertOpenApiLogEntryContains('Required @OA\PathItem() not found'); + $openapi = (new Generator($this->getTrackingLogger())) + ->setAnalyser(new TokenAnalyser()) + ->generate([$this->fixture('Customer.php')]); + + $customerSchema = $openapi->components->schemas[0]; + $this->assertStringContainsString( + 'Fixtures/Customer.php on line 16', + $customerSchema->_context->getDebugLocation() + ); + + $customerPropertyFirstName = $customerSchema->properties[0]; + $this->assertStringContainsString( + Customer::class . '->firstname in ', + $customerPropertyFirstName->_context->getDebugLocation() + ); + } }