From b1a792e59ebec2219be8356a1b22c8e9f0278fab Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Thu, 6 Jun 2024 12:56:21 +1200 Subject: [PATCH] Collect unrelated annotations/attributes in `Context::other` (#1601) --- src/Analysers/AttributeAnnotationFactory.php | 7 ++++++- src/Analysers/DocBlockAnnotationFactory.php | 15 ++++++++++++++- src/Analysers/DocBlockParser.php | 2 +- src/Context.php | 1 + 4 files changed, 22 insertions(+), 3 deletions(-) diff --git a/src/Analysers/AttributeAnnotationFactory.php b/src/Analysers/AttributeAnnotationFactory.php index 8c096878..507f64e2 100644 --- a/src/Analysers/AttributeAnnotationFactory.php +++ b/src/Analysers/AttributeAnnotationFactory.php @@ -47,9 +47,14 @@ public function build(\Reflector $reflector, Context $context): array $instance = $attribute->newInstance(); if ($instance instanceof OA\AbstractAnnotation) { $annotations[] = $instance; + } else { + if ($context->is('other') === false) { + $context->other = []; + } + $context->other[] = $instance; } } else { - $context->logger->debug(sprintf('Could not instantiate attribute "%s", because class not found.', $attribute->getName())); + $context->logger->debug(sprintf('Could not instantiate attribute "%s"; class not found.', $attribute->getName())); } } diff --git a/src/Analysers/DocBlockAnnotationFactory.php b/src/Analysers/DocBlockAnnotationFactory.php index d5503158..435c4776 100644 --- a/src/Analysers/DocBlockAnnotationFactory.php +++ b/src/Analysers/DocBlockAnnotationFactory.php @@ -6,6 +6,7 @@ namespace OpenApi\Analysers; +use OpenApi\Annotations as OA; use OpenApi\Context; use OpenApi\Generator; @@ -55,7 +56,19 @@ public function build(\Reflector $reflector, Context $context): array $this->docBlockParser->setAliases($aliases); if (method_exists($reflector, 'getDocComment') && ($comment = $reflector->getDocComment())) { - return $this->docBlockParser->fromComment($comment, $context); + $annotations = []; + foreach ($this->docBlockParser->fromComment($comment, $context) as $instance) { + if ($instance instanceof OA\AbstractAnnotation) { + $annotations[] = $instance; + } else { + if ($context->is('other') === false) { + $context->other = []; + } + $context->other[] = $instance; + } + } + + return $annotations; } return []; diff --git a/src/Analysers/DocBlockParser.php b/src/Analysers/DocBlockParser.php index c8e24549..5e68cc39 100644 --- a/src/Analysers/DocBlockParser.php +++ b/src/Analysers/DocBlockParser.php @@ -55,7 +55,7 @@ public function setAliases(array $aliases): void * * @param string $comment a T_DOC_COMMENT * - * @return array + * @return array */ public function fromComment(string $comment, Context $context): array { diff --git a/src/Context.php b/src/Context.php index 6fcffcfd..4282ad66 100644 --- a/src/Context.php +++ b/src/Context.php @@ -37,6 +37,7 @@ * the serializer * @property OA\AbstractAnnotation|null $nested * @property OA\AbstractAnnotation[]|null $annotations + * @property OA\AbstractAnnotation[]|null $other Annotations not related to OpenApi * @property LoggerInterface|null $logger Guaranteed to be set when using the `Generator` * @property array|null $scanned Details of file scanner when using ReflectionAnalyser * @property string|null $version The OpenAPI version in use