Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Collect unrelated annotations/attributes in Context::other #1601

Merged
merged 2 commits into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 1 deletion src/Analysers/AttributeAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}
}

Expand Down
15 changes: 14 additions & 1 deletion src/Analysers/DocBlockAnnotationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace OpenApi\Analysers;

use OpenApi\Annotations as OA;
use OpenApi\Context;
use OpenApi\Generator;

Expand Down Expand Up @@ -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 [];
Expand Down
2 changes: 1 addition & 1 deletion src/Analysers/DocBlockParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function setAliases(array $aliases): void
*
* @param string $comment a T_DOC_COMMENT
*
* @return array<OA\AbstractAnnotation>
* @return array<OA\AbstractAnnotation|object>
*/
public function fromComment(string $comment, Context $context): array
{
Expand Down
1 change: 1 addition & 0 deletions src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down