Add @QueryParam default support

This commit is contained in:
marapper 2013-04-30 16:21:12 +04:00 committed by William DURAND
parent 5567f74692
commit 80b5162c83
8 changed files with 205 additions and 2 deletions

View File

@ -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
*/

View File

@ -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,

View File

@ -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();

View File

@ -0,0 +1,90 @@
<?php
/*
* This file is part of the NelmioApiDocBundle.
*
* (c) Nelmio <hello@nelm.io>
*
* 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);
}
}

View File

@ -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",

View File

@ -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

View File

@ -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 ###

View File

@ -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',