Merge SWG annotations in manually created @Operation (#1321)

* Merge SWG annotations in manually created `@Operation`

* failing test

* Fix tests
This commit is contained in:
Guilhem N 2018-06-02 13:48:44 +02:00 committed by GitHub
parent dbfa4ed8bd
commit 8db415afce
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 1 deletions

View File

@ -130,6 +130,7 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface
$nestedContext = clone $context; $nestedContext = clone $context;
$nestedContext->nested = true; $nestedContext->nested = true;
$implicitAnnotations = []; $implicitAnnotations = [];
$operations = [];
$tags = []; $tags = [];
$security = []; $security = [];
foreach (array_merge($annotations, $classAnnotations[$declaringClass->getName()]) as $annotation) { foreach (array_merge($annotations, $classAnnotations[$declaringClass->getName()]) as $annotation) {
@ -143,6 +144,7 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface
$operation->path = $path; $operation->path = $path;
$operation->mergeProperties($annotation); $operation->mergeProperties($annotation);
$operations[$httpMethod] = $operation;
$analysis->addAnnotation($operation, null); $analysis->addAnnotation($operation, null);
} }
@ -155,6 +157,7 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface
$annotation->path = $path; $annotation->path = $path;
} }
$operations[$annotation->method] = $annotation;
$analysis->addAnnotation($annotation, null); $analysis->addAnnotation($annotation, null);
continue; continue;
@ -185,6 +188,9 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface
continue; continue;
} }
// Registers new annotations
$analysis->addAnnotations($implicitAnnotations, null);
foreach ($httpMethods as $httpMethod) { foreach ($httpMethods as $httpMethod) {
$annotationClass = $operationAnnotations[$httpMethod]; $annotationClass = $operationAnnotations[$httpMethod];
$constructorArg = [ $constructorArg = [
@ -201,9 +207,13 @@ final class SwaggerPhpDescriber implements ModelRegistryAwareInterface
} }
$operation = new $annotationClass($constructorArg); $operation = new $annotationClass($constructorArg);
if (isset($operations[$httpMethod])) {
$operations[$httpMethod]->mergeProperties($operation);
} else {
$analysis->addAnnotation($operation, null); $analysis->addAnnotation($operation, null);
} }
} }
}
return $analysis; return $analysis;
} }

View File

@ -196,4 +196,15 @@ class ApiController
public function configReferenceAction() 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()
{
}
} }

View File

@ -362,4 +362,15 @@ class FunctionalTest extends WebTestCase
$operation = $this->getOperation('/api/configReference', 'get'); $operation = $this->getOperation('/api/configReference', 'get');
$this->assertEquals('#/definitions/Test', $operation->getResponses()->get('200')->getSchema()->getRef()); $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());
}
} }