Skip to content

Commit

Permalink
Merge pull request #750 from piotrantosik/host-placeholder
Browse files Browse the repository at this point in the history
Fix route host with placeholder
  • Loading branch information
willdurand committed Nov 25, 2015
2 parents 4fad200 + dd8bdd2 commit 97707ea
Show file tree
Hide file tree
Showing 9 changed files with 153 additions and 38 deletions.
5 changes: 5 additions & 0 deletions Annotation/ApiDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,11 @@ public function setRoute(Route $route)

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;
}
Expand Down
20 changes: 9 additions & 11 deletions Tests/Extractor/ApiDocExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -40,7 +40,7 @@ public function testAll()
$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;
}
Expand Down Expand Up @@ -69,13 +69,6 @@ public function testAll()
$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());
Expand All @@ -90,12 +83,17 @@ public function testAll()
$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()
Expand Down
10 changes: 10 additions & 0 deletions Tests/Fixtures/Controller/TestController.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,14 @@ public function overrideJmsAnnotationWithApiDocParametersAction()
public function defaultJmsAnnotations()
{
}

/**
* @ApiDoc(
* description="Route with host placeholder",
* views={ "default" }
* )
*/
public function routeWithHostAction()
{
}
}
4 changes: 4 additions & 0 deletions Tests/Fixtures/app/config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
10 changes: 9 additions & 1 deletion Tests/Fixtures/app/config/routing.yml
Original file line number Diff line number Diff line change
Expand Up @@ -229,4 +229,12 @@ test_route_26:
test_route_27:
path: /api/overrride/properties
methods: [POST, PUT]
defaults: { _controller: NelmioApiDocTestBundle:Test:overrideJmsAnnotationWithApiDocPropertiesAction, _format: json }
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 }
13 changes: 13 additions & 0 deletions Tests/Formatter/testFormat-result-no-dunglas.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###


Expand Down
60 changes: 46 additions & 14 deletions Tests/Formatter/testFormat-result-no-dunglas.php
Original file line number Diff line number Diff line change
Expand Up @@ -1855,17 +1855,49 @@
'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}',
Expand All @@ -1885,7 +1917,7 @@
),
'deprecated' => false,
),
10 =>
11 =>
array (
'method' => 'GET',
'uri' => '/z-action-with-deprecated-indicator',
Expand All @@ -1896,7 +1928,7 @@
),
'deprecated' => true,
),
11 =>
12 =>
array (
'method' => 'POST',
'uri' => '/z-action-with-nullable-request-param',
Expand All @@ -1919,7 +1951,7 @@
),
'deprecated' => false,
),
12 =>
13 =>
array (
'method' => 'GET',
'uri' => '/z-action-with-query-param',
Expand All @@ -1939,7 +1971,7 @@
),
'deprecated' => false,
),
13 =>
14 =>
array (
'method' => 'GET',
'uri' => '/z-action-with-query-param-no-default',
Expand All @@ -1958,7 +1990,7 @@
),
'deprecated' => false,
),
14 =>
15 =>
array (
'method' => 'GET',
'uri' => '/z-action-with-query-param-strict',
Expand All @@ -1978,7 +2010,7 @@
),
'deprecated' => false,
),
15 =>
16 =>
array (
'method' => 'POST',
'uri' => '/z-action-with-request-param',
Expand All @@ -2001,7 +2033,7 @@
),
'deprecated' => false,
),
16 =>
17 =>
array (
'method' => 'ANY',
'uri' => '/z-return-jms-and-validator-output',
Expand Down Expand Up @@ -2101,7 +2133,7 @@
),
'deprecated' => false,
),
17 =>
18 =>
array (
'method' => 'ANY',
'uri' => '/z-return-selected-parsers-input',
Expand Down Expand Up @@ -2155,7 +2187,7 @@
),
'deprecated' => false,
),
18 =>
19 =>
array (
'method' => 'ANY',
'uri' => '/z-return-selected-parsers-output',
Expand Down Expand Up @@ -2255,7 +2287,7 @@
),
'deprecated' => false,
),
19 =>
20 =>
array (
'method' => 'POST',
'uri' => '/zcached',
Expand All @@ -2267,7 +2299,7 @@
),
'deprecated' => false,
),
20 =>
21 =>
array (
'method' => 'POST',
'uri' => '/zsecured',
Expand Down
13 changes: 13 additions & 0 deletions Tests/Formatter/testFormat-result.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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 ###


Expand Down
Loading

0 comments on commit 97707ea

Please sign in to comment.