From 6b66edbcda201ba481cdfbe64c74868da21c406a Mon Sep 17 00:00:00 2001 From: FlorianLB Date: Wed, 14 Nov 2012 23:59:06 +0100 Subject: [PATCH 1/2] RequestParam annotation create a parameter item --- Annotation/ApiDoc.php | 9 +++++++++ Extractor/ApiDocExtractor.php | 14 ++++++++++++-- Tests/Extractor/ApiDocExtratorTest.php | 2 +- Tests/Fixtures/Controller/TestController.php | 9 +++++++++ Tests/Fixtures/app/config/routing.yml | 6 ++++++ Tests/Formatter/MarkdownFormatterTest.php | 12 ++++++++++++ Tests/Formatter/SimpleFormatterTest.php | 9 +++++++++ 7 files changed, 58 insertions(+), 3 deletions(-) diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index f7ee000..fd05819 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -205,6 +205,15 @@ class ApiDoc $this->uri = $uri; } + /** + * @param string $name + * @param array $parameter + */ + public function addParameter($name, array $parameter) + { + $this->parameters[$name] = $parameter; + } + /** * @param array $parameters */ diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 2b21238..51cdce7 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -24,7 +24,9 @@ class ApiDocExtractor { const ANNOTATION_CLASS = 'Nelmio\\ApiDocBundle\\Annotation\\ApiDoc'; - const FOS_REST_PARAM_CLASS = 'FOS\\RestBundle\\Controller\\Annotations\\Param'; + const FOS_REST_QUERY_PARAM_CLASS = 'FOS\\RestBundle\\Controller\\Annotations\\QueryParam'; + + const FOS_REST_REQUEST_PARAM_CLASS = 'FOS\\RestBundle\\Controller\\Annotations\\RequestParam'; /** * @var \Symfony\Component\DependencyInjection\ContainerInterface @@ -338,7 +340,7 @@ class ApiDocExtractor protected function parseAnnotations(ApiDoc $annotation, Route $route, \ReflectionMethod $method) { foreach ($this->reader->getMethodAnnotations($method) as $annot) { - if (is_subclass_of($annot, self::FOS_REST_PARAM_CLASS)) { + if (is_a($annot, self::FOS_REST_QUERY_PARAM_CLASS)) { if ($annot->strict) { $annotation->addRequirement($annot->name, array( 'requirement' => $annot->requirements, @@ -352,6 +354,14 @@ class ApiDocExtractor )); } } + else if (is_a($annot, self::FOS_REST_REQUEST_PARAM_CLASS)) { + $annotation->addParameter($annot->name, array( + 'required' => !$annot->nullable, + 'dataType' => $annot->requirements, + 'description' => $annot->description, + 'readonly' => false + )); + } } } } diff --git a/Tests/Extractor/ApiDocExtratorTest.php b/Tests/Extractor/ApiDocExtratorTest.php index 700c01e..17a4e97 100644 --- a/Tests/Extractor/ApiDocExtratorTest.php +++ b/Tests/Extractor/ApiDocExtratorTest.php @@ -22,7 +22,7 @@ class ApiDocExtractorTest extends WebTestCase $data = $extractor->all(); $this->assertTrue(is_array($data)); - $this->assertCount(12, $data); + $this->assertCount(13, $data); foreach ($data as $d) { $this->assertTrue(is_array($d)); diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index 542b7c8..7eaa74b 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -12,6 +12,7 @@ namespace Nelmio\ApiDocBundle\Tests\Fixtures\Controller; use FOS\RestBundle\Controller\Annotations\QueryParam; +use FOS\RestBundle\Controller\Annotations\RequestParam; use Nelmio\ApiDocBundle\Annotation\ApiDoc; use Symfony\Component\HttpFoundation\Response; @@ -111,4 +112,12 @@ class TestController { } + /** + * @ApiDoc() + * @RequestParam(name="param1", requirements="string", nullable=false, description="Param1 description.") + */ + public function zActionWithRequestParamAction() + { + } + } diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml index 25f5ac5..ba5f121 100644 --- a/Tests/Fixtures/app/config/routing.yml +++ b/Tests/Fixtures/app/config/routing.yml @@ -52,6 +52,12 @@ test_route_10: requirements: _method: GET +test_route_11: + pattern: /z-action-with-request-param + defaults: { _controller: NelmioApiDocTestBundle:Test:zActionWithRequestParam } + requirements: + _method: POST + test_service_route_1: pattern: /tests.{_format} defaults: { _controller: nemlio.test.controller:indexAction, _format: json } diff --git a/Tests/Formatter/MarkdownFormatterTest.php b/Tests/Formatter/MarkdownFormatterTest.php index 4138e52..f90f111 100644 --- a/Tests/Formatter/MarkdownFormatterTest.php +++ b/Tests/Formatter/MarkdownFormatterTest.php @@ -266,6 +266,18 @@ page: * Requirement: \d+ * Description: Page of the overview. + + +### `POST` /z-action-with-request-param ### + + +#### Parameters #### + +param1: + + * type: string + * required: true + * description: Param1 description. MARKDOWN; $this->assertEquals($expected, $result); diff --git a/Tests/Formatter/SimpleFormatterTest.php b/Tests/Formatter/SimpleFormatterTest.php index e9624c9..0fef777 100644 --- a/Tests/Formatter/SimpleFormatterTest.php +++ b/Tests/Formatter/SimpleFormatterTest.php @@ -314,6 +314,15 @@ With multiple lines.', 'page' => array('description' => 'Page of the overview.', 'requirement' => '\d+') ), ), + 8 => + array( + 'method' => 'POST', + 'uri' => '/z-action-with-request-param', + 'parameters' => + array( + 'param1' => array('description' => 'Param1 description.', 'required' => true, 'dataType' => 'string', 'readonly' => false) + ), + ), ), ); From e4e9f6223073b325478697402fcc239ffbf30a1b Mon Sep 17 00:00:00 2001 From: FlorianLB Date: Fri, 16 Nov 2012 12:14:46 +0100 Subject: [PATCH 2/2] CS fix --- Extractor/ApiDocExtractor.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Extractor/ApiDocExtractor.php b/Extractor/ApiDocExtractor.php index 51cdce7..f0306f6 100644 --- a/Extractor/ApiDocExtractor.php +++ b/Extractor/ApiDocExtractor.php @@ -353,8 +353,7 @@ class ApiDocExtractor 'description' => $annot->description, )); } - } - else if (is_a($annot, self::FOS_REST_REQUEST_PARAM_CLASS)) { + } else if (is_a($annot, self::FOS_REST_REQUEST_PARAM_CLASS)) { $annotation->addParameter($annot->name, array( 'required' => !$annot->nullable, 'dataType' => $annot->requirements,