diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index 2f0b4d5..9b22792 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -398,6 +398,22 @@ class ApiDoc return $this->deprecated; } + /** + * @return array + */ + public function getFilters() + { + return $this->filters; + } + + /** + * @return array + */ + public function getRequirements() + { + return $this->requirements; + } + /** * @param boolean $deprecated */ diff --git a/Extractor/Handler/FosRestHandler.php b/Extractor/Handler/FosRestHandler.php index cb394b1..16c0e6c 100644 --- a/Extractor/Handler/FosRestHandler.php +++ b/Extractor/Handler/FosRestHandler.php @@ -36,6 +36,12 @@ class FosRestHandler implements HandlerInterface 'dataType' => '', 'description' => $annot->description, )); + } elseif($annot->default !== null) { + $annotation->addFilter($annot->name, array( + 'requirement' => $annot->requirements, + 'description' => $annot->description, + 'default' => $annot->default, + )); } else { $annotation->addFilter($annot->name, array( 'requirement' => $annot->requirements, diff --git a/Tests/Extractor/ApiDocExtratorTest.php b/Tests/Extractor/ApiDocExtractorTest.php similarity index 98% rename from Tests/Extractor/ApiDocExtratorTest.php rename to Tests/Extractor/ApiDocExtractorTest.php index 68e8f70..057d1ac 100644 --- a/Tests/Extractor/ApiDocExtratorTest.php +++ b/Tests/Extractor/ApiDocExtractorTest.php @@ -15,6 +15,8 @@ use Nelmio\ApiDocBundle\Tests\WebTestCase; class ApiDocExtractorTest extends WebTestCase { + const ROUTES_QUANTITY = 18; + public function testAll() { $container = $this->getContainer(); @@ -24,7 +26,7 @@ class ApiDocExtractorTest extends WebTestCase restore_error_handler(); $this->assertTrue(is_array($data)); - $this->assertCount(16, $data); + $this->assertCount(self::ROUTES_QUANTITY, $data); foreach ($data as $d) { $this->assertTrue(is_array($d)); @@ -193,7 +195,7 @@ class ApiDocExtractorTest extends WebTestCase $annotation->getCache() ); } - + public function testGetWithDeprecated() { $container = $this->getContainer(); diff --git a/Tests/Extractor/Handler/FosRestHandlerTest.php b/Tests/Extractor/Handler/FosRestHandlerTest.php new file mode 100644 index 0000000..28cb2b6 --- /dev/null +++ b/Tests/Extractor/Handler/FosRestHandlerTest.php @@ -0,0 +1,90 @@ + +* +* For the full copyright and license information, please view the LICENSE +* file that was distributed with this source code. +*/ + +namespace Nelmio\ApiDocBundle\Tests\Extractor; + +use Nelmio\ApiDocBundle\Tests\WebTestCase; + +class FosRestHandlerTest extends WebTestCase +{ + + public function testGetWithQueryParamStrict() + { + $container = $this->getContainer(); + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zActionWithQueryParamStrictAction', 'test_route_15'); + + $this->assertNotNull($annotation); + + $requirements = $annotation->getRequirements(); + $this->assertCount(1, $requirements); + $this->assertArrayHasKey('page', $requirements); + + $requirement = $requirements['page']; + + $this->assertArrayHasKey('requirement', $requirement); + $this->assertEquals($requirement['requirement'], '\d+'); + + $this->assertArrayHasKey('description', $requirement); + $this->assertEquals($requirement['description'], 'Page of the overview.'); + + $this->assertArrayHasKey('dataType', $requirement); + + $this->assertArrayNotHasKey('default', $requirement); + } + + public function testGetWithQueryParam() + { + $container = $this->getContainer(); + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zActionWithQueryParamAction', 'test_route_8'); + + $this->assertNotNull($annotation); + + $filters = $annotation->getFilters(); + $this->assertCount(1, $filters); + $this->assertArrayHasKey('page', $filters); + + $filter = $filters['page']; + + $this->assertArrayHasKey('requirement', $filter); + $this->assertEquals($filter['requirement'], '\d+'); + + $this->assertArrayHasKey('description', $filter); + $this->assertEquals($filter['description'], 'Page of the overview.'); + + $this->assertArrayHasKey('default', $filter); + $this->assertEquals($filter['default'], '1'); + } + + public function testGetWithQueryParamNoDefault() + { + $container = $this->getContainer(); + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zActionWithQueryParamNoDefaultAction', 'test_route_16'); + + $this->assertNotNull($annotation); + + $filters = $annotation->getFilters(); + $this->assertCount(1, $filters); + $this->assertArrayHasKey('page', $filters); + + $filter = $filters['page']; + + $this->assertArrayHasKey('requirement', $filter); + $this->assertEquals($filter['requirement'], '\d+'); + + $this->assertArrayHasKey('description', $filter); + $this->assertEquals($filter['description'], 'Page of the overview.'); + + $this->assertArrayNotHasKey('default', $filter); + } + +} \ No newline at end of file diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index 79c67cb..bcb3b0a 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -97,6 +97,14 @@ class TestController { } + /** + * @ApiDoc() + * @QueryParam(strict=true, name="page", requirements="\d+", description="Page of the overview.") + */ + public function zActionWithQueryParamStrictAction() + { + } + /** * @ApiDoc() * @QueryParam(name="page", requirements="\d+", default="1", description="Page of the overview.") @@ -105,6 +113,14 @@ class TestController { } + /** + * @ApiDoc() + * @QueryParam(name="page", requirements="\d+", description="Page of the overview.") + */ + public function zActionWithQueryParamNoDefaultAction() + { + } + /** * @ApiDoc( * description="Testing JMS", diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml index 3467e05..0741d41 100644 --- a/Tests/Fixtures/app/config/routing.yml +++ b/Tests/Fixtures/app/config/routing.yml @@ -99,3 +99,15 @@ test_route_14: defaults: { _controller: NelmioApiDocTestBundle:Test:postTest2, _format: json } requirements: _method: POST + +test_route_15: + pattern: /z-action-with-query-param-strict + defaults: { _controller: NelmioApiDocTestBundle:Test:zActionWithQueryParamStrict } + requirements: + _method: GET + +test_route_16: + pattern: /z-action-with-query-param-no-default + defaults: { _controller: NelmioApiDocTestBundle:Test:zActionWithQueryParamNoDefault } + requirements: + _method: GET diff --git a/Tests/Formatter/MarkdownFormatterTest.php b/Tests/Formatter/MarkdownFormatterTest.php index bdba360..0fd3d39 100644 --- a/Tests/Formatter/MarkdownFormatterTest.php +++ b/Tests/Formatter/MarkdownFormatterTest.php @@ -357,6 +357,33 @@ page: * Requirement: \\d+ * Description: Page of the overview. + * Default: 1 + + +### `GET` /z-action-with-query-param-no-default ### +### This method is deprecated ### + + + +#### Filters #### + +page: + + * Requirement: \d+ + * Description: Page of the overview. + + +### `GET` /z-action-with-query-param-strict ### +### This method is deprecated ### + + + +#### Requirements #### + +**page** + + - Requirement: \d+ + - Description: Page of the overview. ### `POST` /z-action-with-request-param ### diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index 3322a81..2caad83 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -481,6 +481,7 @@ And, it supports multilines until the first \'@\' char.', array( 'requirement' => '\\d+', 'description' => 'Page of the overview.', + 'default' => '1', ), ), 'https' => false, @@ -488,6 +489,39 @@ And, it supports multilines until the first \'@\' char.', 'deprecated' => false, ), 10 => + array( + 'method' => 'GET', + 'uri' => '/z-action-with-query-param-no-default', + 'filters' => + array ( + 'page' => + array ( + 'requirement' => '\\d+', + 'description' => 'Page of the overview.', + ), + ), + 'https' => false, + 'authentication' => false, + 'deprecated' => false, + ), + 11 => + array( + 'method' => 'GET', + 'uri' => '/z-action-with-query-param-strict', + 'requirements' => + array ( + 'page' => + array ( + 'requirement' => '\\d+', + 'dataType' => '', + 'description' => 'Page of the overview.', + ), + ), + 'https' => false, + 'authentication' => false, + 'deprecated' => false, + ), + 12 => array( 'method' => 'POST', 'uri' => '/z-action-with-request-param',