From 2a6fa1dda2cfcff7c563879c61b7552f872b4c7a Mon Sep 17 00:00:00 2001 From: Martin Rademacher Date: Mon, 15 Jul 2024 13:31:20 +1200 Subject: [PATCH] Preserve docblocks for consecutive promoted properties (#1627) --- src/Analysers/AttributeAnnotationFactory.php | 3 ++- tests/Fixtures/Scratch/PromotedProperty.php | 17 +++++++++++++++++ .../Fixtures/Scratch/PromotedProperty3.0.0.yaml | 6 ++++++ .../Fixtures/Scratch/PromotedProperty3.1.0.yaml | 6 ++++++ 4 files changed, 31 insertions(+), 1 deletion(-) diff --git a/src/Analysers/AttributeAnnotationFactory.php b/src/Analysers/AttributeAnnotationFactory.php index ba3c52f5..c93609a2 100644 --- a/src/Analysers/AttributeAnnotationFactory.php +++ b/src/Analysers/AttributeAnnotationFactory.php @@ -65,6 +65,7 @@ public function build(\Reflector $reflector, Context $context): array foreach ($rp->getAttributes($attributeName, \ReflectionAttribute::IS_INSTANCEOF) as $attribute) { /** @var OA\Property|OA\Parameter|OA\RequestBody $instance */ $instance = $attribute->newInstance(); + $type = (($rnt = $rp->getType()) && $rnt instanceof \ReflectionNamedType) ? $rnt->getName() : Generator::UNDEFINED; $nullable = $rnt ? $rnt->allowsNull() : true; @@ -81,7 +82,7 @@ public function build(\Reflector $reflector, Context $context): array if ($rp->isPromoted()) { // ensure each property has its own context - $instance->_context = new Context([], $instance->_context); + $instance->_context = new Context(['generated' => true, 'annotations' => [$instance]], $context); // promoted parameter - docblock is available via class/property if ($comment = $rp->getDeclaringClass()->getProperty($rp->getName())->getDocComment()) { diff --git a/tests/Fixtures/Scratch/PromotedProperty.php b/tests/Fixtures/Scratch/PromotedProperty.php index f0733cce..3740bb6d 100644 --- a/tests/Fixtures/Scratch/PromotedProperty.php +++ b/tests/Fixtures/Scratch/PromotedProperty.php @@ -27,6 +27,14 @@ public function __construct( #[OAT\Property(property: 'thevalue')] public string $value = '', + /** + * Other value. + * + * @var string + */ + #[OAT\Property(property: 'other')] + public string $other = '', + /** * Property meta. * @@ -35,6 +43,15 @@ public function __construct( * @OA\Property(property="themeta") */ public string $meta = '', + + /** + * Property different. + * + * @var string + * + * @OA\Property() + */ + public string $different = '', ) { } } diff --git a/tests/Fixtures/Scratch/PromotedProperty3.0.0.yaml b/tests/Fixtures/Scratch/PromotedProperty3.0.0.yaml index 6c4e5a3c..eccdf87c 100644 --- a/tests/Fixtures/Scratch/PromotedProperty3.0.0.yaml +++ b/tests/Fixtures/Scratch/PromotedProperty3.0.0.yaml @@ -17,10 +17,16 @@ components: thevalue: description: 'Property value.' type: string + other: + description: 'Other value.' + type: string thename: description: 'Property name.' type: string themeta: description: 'Property meta.' type: string + different: + description: 'Property different.' + type: string type: object diff --git a/tests/Fixtures/Scratch/PromotedProperty3.1.0.yaml b/tests/Fixtures/Scratch/PromotedProperty3.1.0.yaml index 977e3b6e..e7059d6c 100644 --- a/tests/Fixtures/Scratch/PromotedProperty3.1.0.yaml +++ b/tests/Fixtures/Scratch/PromotedProperty3.1.0.yaml @@ -17,10 +17,16 @@ components: thevalue: description: 'Property value.' type: string + other: + description: 'Other value.' + type: string thename: description: 'Property name.' type: string themeta: description: 'Property meta.' type: string + different: + description: 'Property different.' + type: string type: object