From e2d2e41f7b85d73183326cab59f6d94cf1da0d83 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Florian=20Str=C3=BCbe?= Date: Tue, 27 Oct 2015 13:36:07 +0100 Subject: [PATCH] [ISSUE-739] ApiDoc parameters setting will override lower parameter definitions in the hierarchy --- Extractor/ApiDocExtractor.php | 2 + Tests/Extractor/ApiDocExtractorTest.php | 65 ++++++++++++++++++++ Tests/Fixtures/Controller/TestController.php | 51 +++++++++++++++ Tests/Fixtures/app/config/routing.yml | 5 ++ 4 files changed, 123 insertions(+) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 40c5e9bac..97c3d6c04 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -322,6 +322,8 @@ protected function extractData(ApiDoc $annotation, Route $route, \ReflectionMeth }); } + // merge parameters with parameters block from ApiDoc annotation in controller method + $parameters = $this->mergeParameters($parameters, $annotation->getParameters()); $annotation->setParameters($parameters); } diff --git a/Tests/Extractor/ApiDocExtractorTest.php b/Tests/Extractor/ApiDocExtractorTest.php index 7dbf212e7..9e3cc29f7 100644 --- a/Tests/Extractor/ApiDocExtractorTest.php +++ b/Tests/Extractor/ApiDocExtractorTest.php @@ -371,4 +371,69 @@ public function testForViews($view, $count) $this->assertTrue(is_array($data)); $this->assertCount($count, $data); } + + public function testOverrideJmsAnnotationWithApiDocParameters() + { + $container = $this->getContainer(); + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $annotation = $extractor->get( + 'Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::overrideJmsAnnotationWithApiDocParametersAction', + 'test_route_27' + ); + + $this->assertInstanceOf('Nelmio\ApiDocBundle\Annotation\ApiDoc', $annotation); + + $array = $annotation->toArray(); + $this->assertTrue(is_array($array['parameters'])); + + $this->assertEquals('string', $array['parameters']['foo']['dataType']); + $this->assertEquals('DateTime', $array['parameters']['bar']['dataType']); + + $this->assertEquals('integer', $array['parameters']['number']['dataType']); + $this->assertEquals('string', $array['parameters']['number']['actualType']); + $this->assertEquals(null, $array['parameters']['number']['subType']); + $this->assertEquals(true, $array['parameters']['number']['required']); + $this->assertEquals('This is the new description', $array['parameters']['number']['description']); + $this->assertEquals(false, $array['parameters']['number']['readonly']); + $this->assertEquals('v3.0', $array['parameters']['number']['sinceVersion']); + $this->assertEquals('v4.0', $array['parameters']['number']['untilVersion']); + + $this->assertEquals('object (ArrayCollection)', $array['parameters']['arr']['dataType']); + + $this->assertEquals('object (JmsNested)', $array['parameters']['nested']['dataType']); + $this->assertEquals('integer', $array['parameters']['nested']['children']['bar']['dataType']); + $this->assertEquals('d+', $array['parameters']['nested']['children']['bar']['format']); + } + + public function testJmsAnnotation() + { + $container = $this->getContainer(); + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $annotation = $extractor->get( + 'Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::defaultJmsAnnotations', + 'test_route_27' + ); + + $this->assertInstanceOf('Nelmio\ApiDocBundle\Annotation\ApiDoc', $annotation); + + $array = $annotation->toArray(); + $this->assertTrue(is_array($array['parameters'])); + + $this->assertEquals('string', $array['parameters']['foo']['dataType']); + $this->assertEquals('DateTime', $array['parameters']['bar']['dataType']); + + $this->assertEquals('double', $array['parameters']['number']['dataType']); + $this->assertEquals('float', $array['parameters']['number']['actualType']); + $this->assertEquals(null, $array['parameters']['number']['subType']); + $this->assertEquals(false, $array['parameters']['number']['required']); + $this->assertEquals('', $array['parameters']['number']['description']); + $this->assertEquals(false, $array['parameters']['number']['readonly']); + $this->assertEquals(null, $array['parameters']['number']['sinceVersion']); + $this->assertEquals(null, $array['parameters']['number']['untilVersion']); + + $this->assertEquals('array', $array['parameters']['arr']['dataType']); + + $this->assertEquals('object (JmsNested)', $array['parameters']['nested']['dataType']); + $this->assertEquals('string', $array['parameters']['nested']['children']['bar']['dataType']); + } } diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index 149a8dc01..f748e0d43 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -334,4 +334,55 @@ public function exclusiveAction() public function withLinkAction() { } + + /** + * @ApiDoc( + * output="Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsTest", + * input={ + * "class" = "Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsTest" + * }, + * parameters={ + * { + * "name"="number", + * "dataType"="integer", + * "actualType"="string", + * "subType"=null, + * "required"=true, + * "description"="This is the new description", + * "readonly"=false, + * "sinceVersion"="v3.0", + * "untilVersion"="v4.0" + * }, + * { + * "name"="arr", + * "dataType"="object (ArrayCollection)" + * }, + * { + * "name"="nested", + * "dataType"="object (JmsNested)", + * "children": { + * "bar": { + * "dataType"="integer", + * "format"="d+" + * } + * } + * } + * } + * ) + */ + public function overrideJmsAnnotationWithApiDocParametersAction() + { + } + + /** + * @ApiDoc( + * output="Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsTest", + * input={ + * "class" = "Nelmio\ApiDocBundle\Tests\Fixtures\Model\JmsTest" + * } + * ) + */ + public function defaultJmsAnnotations() + { + } } diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml index bb28bbb16..ee040699f 100644 --- a/Tests/Fixtures/app/config/routing.yml +++ b/Tests/Fixtures/app/config/routing.yml @@ -225,3 +225,8 @@ test_route_26: path: /z-action-with-array-request-param methods: [POST] defaults: { _controller: NelmioApiDocTestBundle:Test:zActionWithArrayRequestParamAction } + +test_route_27: + path: /api/overrride/properties + methods: [POST, PUT] + defaults: { _controller: NelmioApiDocTestBundle:Test:overrideJmsAnnotationWithApiDocPropertiesAction, _format: json } \ No newline at end of file