diff --git a/Annotation/ApiDoc.php b/Annotation/ApiDoc.php index d99d644..1fbba3f 100644 --- a/Annotation/ApiDoc.php +++ b/Annotation/ApiDoc.php @@ -476,6 +476,11 @@ class ApiDoc if (method_exists($route, 'getHost')) { $this->host = $route->getHost() ? : null; + + //replace route placeholders + foreach ($route->getDefaults() as $key => $value) { + $this->host = str_replace('{' . $key . '}', $value, $this->host); + } } else { $this->host = null; } diff --git a/Tests/Extractor/ApiDocExtractorTest.php b/Tests/Extractor/ApiDocExtractorTest.php index f74db2a..15337a6 100644 --- a/Tests/Extractor/ApiDocExtractorTest.php +++ b/Tests/Extractor/ApiDocExtractorTest.php @@ -19,7 +19,7 @@ class ApiDocExtractorTest extends WebTestCase { const NB_ROUTES_ADDED_BY_DUNGLAS_API_BUNDLE = 5; - private static $ROUTES_QUANTITY_DEFAULT = 33; // Routes in the default view + private static $ROUTES_QUANTITY_DEFAULT = 34; // Routes in the default view private static $ROUTES_QUANTITY_PREMIUM = 6; // Routes in the premium view private static $ROUTES_QUANTITY_TEST = 2; // Routes in the test view @@ -40,7 +40,7 @@ class ApiDocExtractorTest extends WebTestCase $data = $extractor->all(); restore_error_handler(); - $httpsKey = 20; + $httpsKey = 21; if (class_exists('Dunglas\ApiBundle\DunglasApiBundle')) { $httpsKey += self::NB_ROUTES_ADDED_BY_DUNGLAS_API_BUNDLE; } @@ -69,13 +69,6 @@ class ApiDocExtractorTest extends WebTestCase $this->assertTrue(is_array($array1['filters'])); $this->assertNull($a1->getInput()); - $a1 = $data[7]['annotation']; - $array1 = $a1->toArray(); - $this->assertTrue($a1->isResource()); - $this->assertEquals('index action', $a1->getDescription()); - $this->assertTrue(is_array($array1['filters'])); - $this->assertNull($a1->getInput()); - $a2 = $data[8]['annotation']; $array2 = $a2->toArray(); $this->assertFalse($a2->isResource()); @@ -90,12 +83,17 @@ class ApiDocExtractorTest extends WebTestCase $this->assertFalse(isset($array2['filters'])); $this->assertEquals('Nelmio\ApiDocBundle\Tests\Fixtures\Form\TestType', $a2->getInput()); + $a3 = $data[$httpsKey]['annotation']; + $this->assertTrue($a3->getHttps()); + $a4 = $data[11]['annotation']; $this->assertTrue($a4->isResource()); $this->assertEquals('TestResource', $a4->getResource()); - $a3 = $data[$httpsKey]['annotation']; - $this->assertTrue($a3->getHttps()); + $a5 = $data[$httpsKey - 1]['annotation']; + $a5requirements = $a5->getRequirements(); + $this->assertEquals('api.test.dev', $a5->getHost()); + $this->assertEquals('test.dev|test.com', $a5requirements['domain']['requirement']); } public function testGet() diff --git a/Tests/Fixtures/Controller/TestController.php b/Tests/Fixtures/Controller/TestController.php index f748e0d..798a5bb 100644 --- a/Tests/Fixtures/Controller/TestController.php +++ b/Tests/Fixtures/Controller/TestController.php @@ -385,4 +385,14 @@ class TestController public function defaultJmsAnnotations() { } + + /** + * @ApiDoc( + * description="Route with host placeholder", + * views={ "default" } + * ) + */ + public function routeWithHostAction() + { + } } diff --git a/Tests/Fixtures/app/config/default.yml b/Tests/Fixtures/app/config/default.yml index 24e6683..8fc5375 100644 --- a/Tests/Fixtures/app/config/default.yml +++ b/Tests/Fixtures/app/config/default.yml @@ -27,6 +27,10 @@ services: tags: - { name: form.type, alias: dependency_type } +parameters: + domain_prod: test.com + domain_dev: test.dev + #JMS Serializer config for testing JmsMetadataParser jms_serializer: handlers: diff --git a/Tests/Fixtures/app/config/routing.yml b/Tests/Fixtures/app/config/routing.yml index ee04069..540802e 100644 --- a/Tests/Fixtures/app/config/routing.yml +++ b/Tests/Fixtures/app/config/routing.yml @@ -229,4 +229,12 @@ test_route_26: test_route_27: path: /api/overrride/properties methods: [POST, PUT] - defaults: { _controller: NelmioApiDocTestBundle:Test:overrideJmsAnnotationWithApiDocPropertiesAction, _format: json } \ No newline at end of file + defaults: { _controller: NelmioApiDocTestBundle:Test:overrideJmsAnnotationWithApiDocPropertiesAction, _format: json } + +test_route_28: + path: /route_with_host.{_format} + host: api.{domain} + methods: [GET] + requirements: + domain: "%domain_dev%|%domain_prod%" + defaults: { _controller: NelmioApiDocTestBundle:Test:routeWithHost, domain: "%domain_dev%", _format: json } diff --git a/Tests/Formatter/testFormat-result-no-dunglas.markdown b/Tests/Formatter/testFormat-result-no-dunglas.markdown index a52a765..3d64133 100644 --- a/Tests/Formatter/testFormat-result-no-dunglas.markdown +++ b/Tests/Formatter/testFormat-result-no-dunglas.markdown @@ -733,6 +733,19 @@ nested_array[]: * type: array of objects (JmsNested) +### `GET` /route_with_host.{_format} ### + +_Route with host placeholder_ + +#### Requirements #### + +**domain** + + - Requirement: test.dev|test.com +**_format** + + + ### `ANY` /secure-route ### diff --git a/Tests/Formatter/testFormat-result-no-dunglas.php b/Tests/Formatter/testFormat-result-no-dunglas.php index 9b3532f..b972843 100644 --- a/Tests/Formatter/testFormat-result-no-dunglas.php +++ b/Tests/Formatter/testFormat-result-no-dunglas.php @@ -1855,17 +1855,49 @@ With multiple lines.', 'deprecated' => false, ), 8 => + array ( + 'method' => 'GET', + 'uri' => '/route_with_host.{_format}', + 'host' => 'api.test.dev', + 'description' => 'Route with host placeholder', + 'requirements' => + array ( + 'domain' => + array ( + 'requirement' => 'test.dev|test.com', + 'dataType' => '', + 'description' => '', + ), + '_format' => + array ( + 'requirement' => '', + 'dataType' => '', + 'description' => '', + ), + ), + 'views' => + array ( + 0 => 'default', + ), + 'https' => false, + 'authentication' => false, + 'authenticationRoles' => + array ( + ), + 'deprecated' => false, + ), + 9 => array ( 'method' => 'ANY', 'uri' => '/secure-route', 'https' => true, 'authentication' => false, 'authenticationRoles' => - array ( - ), + array ( + ), 'deprecated' => false, ), - 9 => + 10 => array ( 'method' => 'ANY', 'uri' => '/yet-another/{id}', @@ -1885,7 +1917,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 10 => + 11 => array ( 'method' => 'GET', 'uri' => '/z-action-with-deprecated-indicator', @@ -1896,7 +1928,7 @@ With multiple lines.', ), 'deprecated' => true, ), - 11 => + 12 => array ( 'method' => 'POST', 'uri' => '/z-action-with-nullable-request-param', @@ -1919,7 +1951,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 12 => + 13 => array ( 'method' => 'GET', 'uri' => '/z-action-with-query-param', @@ -1939,7 +1971,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 13 => + 14 => array ( 'method' => 'GET', 'uri' => '/z-action-with-query-param-no-default', @@ -1958,7 +1990,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 14 => + 15 => array ( 'method' => 'GET', 'uri' => '/z-action-with-query-param-strict', @@ -1978,7 +2010,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 15 => + 16 => array ( 'method' => 'POST', 'uri' => '/z-action-with-request-param', @@ -2001,7 +2033,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 16 => + 17 => array ( 'method' => 'ANY', 'uri' => '/z-return-jms-and-validator-output', @@ -2101,7 +2133,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 17 => + 18 => array ( 'method' => 'ANY', 'uri' => '/z-return-selected-parsers-input', @@ -2155,7 +2187,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 18 => + 19 => array ( 'method' => 'ANY', 'uri' => '/z-return-selected-parsers-output', @@ -2255,7 +2287,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 19 => + 20 => array ( 'method' => 'POST', 'uri' => '/zcached', @@ -2267,7 +2299,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 20 => + 21 => array ( 'method' => 'POST', 'uri' => '/zsecured', diff --git a/Tests/Formatter/testFormat-result.markdown b/Tests/Formatter/testFormat-result.markdown index 64e3285..7663732 100644 --- a/Tests/Formatter/testFormat-result.markdown +++ b/Tests/Formatter/testFormat-result.markdown @@ -817,6 +817,19 @@ nested_array[]: * type: array of objects (JmsNested) +### `GET` /route_with_host.{_format} ### + +_Route with host placeholder_ + +#### Requirements #### + +**domain** + + - Requirement: test.dev|test.com +**_format** + + + ### `ANY` /secure-route ### diff --git a/Tests/Formatter/testFormat-result.php b/Tests/Formatter/testFormat-result.php index 01f0293..ea28757 100644 --- a/Tests/Formatter/testFormat-result.php +++ b/Tests/Formatter/testFormat-result.php @@ -2009,6 +2009,38 @@ With multiple lines.', 'deprecated' => false, ), 13 => + array ( + 'method' => 'GET', + 'uri' => '/route_with_host.{_format}', + 'host' => 'api.test.dev', + 'description' => 'Route with host placeholder', + 'requirements' => + array ( + 'domain' => + array ( + 'requirement' => 'test.dev|test.com', + 'dataType' => '', + 'description' => '', + ), + '_format' => + array ( + 'requirement' => '', + 'dataType' => '', + 'description' => '', + ), + ), + 'views' => + array ( + 0 => 'default', + ), + 'https' => false, + 'authentication' => false, + 'authenticationRoles' => + array ( + ), + 'deprecated' => false, + ), + 14 => array ( 'method' => 'ANY', 'uri' => '/secure-route', @@ -2019,7 +2051,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 14 => + 15 => array ( 'method' => 'ANY', 'uri' => '/yet-another/{id}', @@ -2039,7 +2071,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 15 => + 16 => array ( 'method' => 'GET', 'uri' => '/z-action-with-deprecated-indicator', @@ -2050,7 +2082,7 @@ With multiple lines.', ), 'deprecated' => true, ), - 16 => + 17 => array ( 'method' => 'POST', 'uri' => '/z-action-with-nullable-request-param', @@ -2073,7 +2105,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 17 => + 18 => array ( 'method' => 'GET', 'uri' => '/z-action-with-query-param', @@ -2093,7 +2125,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 18 => + 19 => array ( 'method' => 'GET', 'uri' => '/z-action-with-query-param-no-default', @@ -2112,7 +2144,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 19 => + 20 => array ( 'method' => 'GET', 'uri' => '/z-action-with-query-param-strict', @@ -2132,7 +2164,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 20 => + 21 => array ( 'method' => 'POST', 'uri' => '/z-action-with-request-param', @@ -2155,7 +2187,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 21 => + 22 => array ( 'method' => 'ANY', 'uri' => '/z-return-jms-and-validator-output', @@ -2255,7 +2287,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 22 => + 23 => array ( 'method' => 'ANY', 'uri' => '/z-return-selected-parsers-input', @@ -2309,7 +2341,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 23 => + 24 => array ( 'method' => 'ANY', 'uri' => '/z-return-selected-parsers-output', @@ -2409,7 +2441,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 24 => + 25 => array ( 'method' => 'POST', 'uri' => '/zcached', @@ -2421,7 +2453,7 @@ With multiple lines.', ), 'deprecated' => false, ), - 25 => + 26 => array ( 'method' => 'POST', 'uri' => '/zsecured',