From 8db415afce65604864b631299bed72eebc67e6b1 Mon Sep 17 00:00:00 2001 From: Guilhem N Date: Sat, 2 Jun 2018 13:48:44 +0200 Subject: [PATCH] Merge SWG annotations in manually created `@Operation` (#1321) * Merge SWG annotations in manually created `@Operation` * failing test * Fix tests --- Describer/SwaggerPhpDescriber.php | 12 +++++++++++- Tests/Functional/Controller/ApiController.php | 11 +++++++++++ Tests/Functional/FunctionalTest.php | 11 +++++++++++ 3 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Describer/SwaggerPhpDescriber.php b/Describer/SwaggerPhpDescriber.php index b79672f..b23fd8c 100644 --- a/Describer/SwaggerPhpDescriber.php +++ b/Describer/SwaggerPhpDescriber.php @@ -130,6 +130,7 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface $nestedContext = clone $context; $nestedContext->nested = true; $implicitAnnotations = []; + $operations = []; $tags = []; $security = []; foreach (array_merge($annotations, $classAnnotations[$declaringClass->getName()]) as $annotation) { @@ -143,6 +144,7 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface $operation->path = $path; $operation->mergeProperties($annotation); + $operations[$httpMethod] = $operation; $analysis->addAnnotation($operation, null); } @@ -155,6 +157,7 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface $annotation->path = $path; } + $operations[$annotation->method] = $annotation; $analysis->addAnnotation($annotation, null); continue; @@ -185,6 +188,9 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface continue; } + // Registers new annotations + $analysis->addAnnotations($implicitAnnotations, null); + foreach ($httpMethods as $httpMethod) { $annotationClass = $operationAnnotations[$httpMethod]; $constructorArg = [ @@ -201,7 +207,11 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface } $operation = new $annotationClass($constructorArg); - $analysis->addAnnotation($operation, null); + if (isset($operations[$httpMethod])) { + $operations[$httpMethod]->mergeProperties($operation); + } else { + $analysis->addAnnotation($operation, null); + } } } diff --git a/Tests/Functional/Controller/ApiController.php b/Tests/Functional/Controller/ApiController.php index 3c00f1a..5143f35 100644 --- a/Tests/Functional/Controller/ApiController.php +++ b/Tests/Functional/Controller/ApiController.php @@ -196,4 +196,15 @@ class ApiController public function configReferenceAction() { } + + /** + * @Route("/multi-annotations", methods={"GET", "POST"}) + * @SWG\Get(description="This is the get operation") + * @SWG\Post(description="This is post") + * + * @SWG\Response(response=200, description="Worked well!", @Model(type=DummyType::class)) + */ + public function operationsWithOtherAnnotations() + { + } } diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index e09a096..97823e4 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -362,4 +362,15 @@ class FunctionalTest extends WebTestCase $operation = $this->getOperation('/api/configReference', 'get'); $this->assertEquals('#/definitions/Test', $operation->getResponses()->get('200')->getSchema()->getRef()); } + + public function testOperationsWithOtherAnnotationsAction() + { + $getOperation = $this->getOperation('/api/multi-annotations', 'get'); + $this->assertSame('This is the get operation', $getOperation->getDescription()); + $this->assertSame('Worked well!', $getOperation->getResponses()->get(200)->getDescription()); + + $postOperation = $this->getOperation('/api/multi-annotations', 'post'); + $this->assertSame('This is post', $postOperation->getDescription()); + $this->assertSame('Worked well!', $postOperation->getResponses()->get(200)->getDescription()); + } }