diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index 5692171..bed21b5 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -522,6 +522,14 @@ class ApiDoc return $this->requirements; } + /** + * @return array + */ + public function getParameters() + { + return $this->parameters; + } + /** * @return array */ diff --git a/Extractor/Handler/FosRestHandler.php b/Extractor/Handler/FosRestHandler.php index 4b7f18f..b12b583 100644 --- a/Extractor/Handler/FosRestHandler.php +++ b/Extractor/Handler/FosRestHandler.php @@ -32,7 +32,7 @@ class FosRestHandler implements HandlerInterface $requirements = $this->handleRequirements($annot->requirements); $annotation->addParameter($annot->name, array( - 'required' => $annot->strict && $annot->default === null, + 'required' => $annot->strict && $annot->nullable === false && $annot->default === null, 'dataType' => $requirements, 'actualType' => $this->inferType($requirements), 'subType' => null, diff --git a/Tests/Extractor/ApiDocExtractorTest.php b/Tests/Extractor/ApiDocExtractorTest.php index e05a251..4416f49 100644 --- a/Tests/Extractor/ApiDocExtractorTest.php +++ b/Tests/Extractor/ApiDocExtractorTest.php @@ -15,7 +15,7 @@ use Nelmio\ApiDocBundle\Tests\WebTestCase; class ApiDocExtractorTest extends WebTestCase { - const ROUTES_QUANTITY = 24; + const ROUTES_QUANTITY = 25; public function testAll() { diff --git a/Tests/Extractor/Handler/FosRestHandlerTest.php b/Tests/Extractor/Handler/FosRestHandlerTest.php index 1470819..87e31e0 100644 --- a/Tests/Extractor/Handler/FosRestHandlerTest.php +++ b/Tests/Extractor/Handler/FosRestHandlerTest.php @@ -105,4 +105,55 @@ class FosRestHandlerTest extends WebTestCase $this->assertEquals($filter['requirement'], 'Email'); } + public function testGetWithRequestParam() + { + $container = $this->getContainer(); + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zActionWithRequestParamAction', 'test_route_11'); + + $this->assertNotNull($annotation); + + $parameters = $annotation->getParameters(); + $this->assertCount(1, $parameters); + $this->assertArrayHasKey('param1', $parameters); + + $parameter = $parameters['param1']; + + $this->assertArrayHasKey('dataType', $parameter); + $this->assertEquals($parameter['dataType'], 'string'); + + $this->assertArrayHasKey('description', $parameter); + $this->assertEquals($parameter['description'], 'Param1 description.'); + + $this->assertArrayHasKey('required', $parameter); + $this->assertEquals($parameter['required'], true); + + $this->assertArrayNotHasKey('default', $parameter); + } + + public function testGetWithRequestParamNullable() + { + $container = $this->getContainer(); + $extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor'); + $annotation = $extractor->get('Nelmio\ApiDocBundle\Tests\Fixtures\Controller\TestController::zActionWithNullableRequestParamAction', 'test_route_22'); + + $this->assertNotNull($annotation); + + $parameters = $annotation->getParameters(); + $this->assertCount(1, $parameters); + $this->assertArrayHasKey('param1', $parameters); + + $parameter = $parameters['param1']; + + $this->assertArrayHasKey('dataType', $parameter); + $this->assertEquals($parameter['dataType'], 'string'); + + $this->assertArrayHasKey('description', $parameter); + $this->assertEquals($parameter['description'], 'Param1 description.'); + + $this->assertArrayHasKey('required', $parameter); + $this->assertEquals($parameter['required'], false); + + $this->assertArrayNotHasKey('default', $parameter); + } } diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index a299db0..3416f5f 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -167,6 +167,14 @@ class TestController { } + /** + * @ApiDoc() + * @RequestParam(name="param1", requirements="string", description="Param1 description.", nullable=true) + */ + public function zActionWithNullableRequestParamAction() + { + } + /** * @ApiDoc() */ diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml index 5502bee..9fcc164 100644 --- a/Tests/Fixtures/app/config/routing.yml +++ b/Tests/Fixtures/app/config/routing.yml @@ -155,3 +155,9 @@ test_route_21: defaults: { _controller: NelmioApiDocTestBundle:Test:zActionWithConstraintAsRequirements } requirements: _method: GET + +test_route_22: + pattern: /z-action-with-nullable-request-param + defaults: { _controller: NelmioApiDocTestBundle:Test:zActionWithNullableRequestParam } + requirements: + _method: POST diff --git a/Tests/Formatter/MarkdownFormatterTest.php b/Tests/Formatter/MarkdownFormatterTest.php index afec19d..aa7934d 100644 --- a/Tests/Formatter/MarkdownFormatterTest.php +++ b/Tests/Formatter/MarkdownFormatterTest.php @@ -418,6 +418,18 @@ nested_array[]: +### `POST` /z-action-with-nullable-request-param ### + + +#### Parameters #### + +param1: + + * type: string + * required: false + * description: Param1 description. + + ### `GET` /z-action-with-query-param ### diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index 6d8918e..55ec84e 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -837,6 +837,27 @@ With multiple lines.', 'deprecated' => true, ), 11 => + array( + 'method' => 'POST', + 'uri' => '/z-action-with-nullable-request-param', + 'parameters' => + array( + 'param1' => + array( + 'required' => false, + 'dataType' => 'string', + 'description' => 'Param1 description.', + 'readonly' => false, + 'actualType' => 'string', + 'subType' => null, + ), + ), + 'https' => false, + 'authentication' => false, + 'authenticationRoles' => array(), + 'deprecated' => false, + ), + 12 => array( 'method' => 'GET', 'uri' => '/z-action-with-query-param', @@ -854,7 +875,7 @@ With multiple lines.', 'authenticationRoles' => array(), 'deprecated' => false, ), - 12 => + 13 => array( 'method' => 'GET', 'uri' => '/z-action-with-query-param-no-default', @@ -871,7 +892,7 @@ With multiple lines.', 'authenticationRoles' => array(), 'deprecated' => false, ), - 13 => + 14 => array( 'method' => 'GET', 'uri' => '/z-action-with-query-param-strict', @@ -889,7 +910,7 @@ With multiple lines.', 'authenticationRoles' => array(), 'deprecated' => false, ), - 14 => + 15 => array( 'method' => 'POST', 'uri' => '/z-action-with-request-param', @@ -910,7 +931,7 @@ With multiple lines.', 'authenticationRoles' => array(), 'deprecated' => false, ), - 15 => + 16 => array( 'method' => 'ANY', 'uri' => '/z-return-jms-and-validator-output', @@ -962,7 +983,7 @@ With multiple lines.', ), 'authenticationRoles' => array(), ), - 16 => + 17 => array( 'method' => "ANY", 'uri' => "/z-return-selected-parsers-input", @@ -998,7 +1019,7 @@ With multiple lines.', ), ) ), - 17 => + 18 => array( 'method' => "ANY", 'uri' => "/z-return-selected-parsers-output",