Skip to content

Commit

Permalink
Fix deprecated route options
Browse files Browse the repository at this point in the history
  • Loading branch information
soullivaneuh authored and willdurand committed Oct 22, 2015
1 parent 189197d commit 32de80f
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 162 deletions.
4 changes: 2 additions & 2 deletions Annotation/ApiDoc.php
Expand Up @@ -480,8 +480,8 @@ public function setRoute(Route $route)
$this->host = null;
}

$this->uri = $route->getPattern();
$this->method = $route->getRequirement('_method') ?: 'ANY';
$this->uri = $route->getPath();
$this->method = $route->getMethods() ? implode('|', $route->getMethods()) : 'ANY';
}

/**
Expand Down
20 changes: 10 additions & 10 deletions Extractor/ApiDocExtractor.php
Expand Up @@ -132,7 +132,7 @@ public function extractAnnotations(array $routes, $view = ApiDoc::DEFAULT_VIEW)
$resources[] = $resource;
} else {
// remove format from routes used for resource grouping
$resources[] = str_replace('.{_format}', '', $route->getPattern());
$resources[] = str_replace('.{_format}', '', $route->getPath());
}
}

Expand All @@ -151,10 +151,10 @@ public function extractAnnotations(array $routes, $view = ApiDoc::DEFAULT_VIEW)
rsort($resources);
foreach ($array as $index => $element) {
$hasResource = false;
$pattern = $element['annotation']->getRoute()->getPattern();
$path = $element['annotation']->getRoute()->getPath();

foreach ($resources as $resource) {
if (0 === strpos($pattern, $resource) || $resource === $element['annotation']->getResource()) {
if (0 === strpos($path, $resource) || $resource === $element['annotation']->getResource()) {
$array[$index]['resource'] = $resource;

$hasResource = true;
Expand All @@ -170,23 +170,23 @@ public function extractAnnotations(array $routes, $view = ApiDoc::DEFAULT_VIEW)
$methodOrder = array('GET', 'POST', 'PUT', 'DELETE');
usort($array, function ($a, $b) use ($methodOrder) {
if ($a['resource'] === $b['resource']) {
if ($a['annotation']->getRoute()->getPattern() === $b['annotation']->getRoute()->getPattern()) {
$methodA = array_search($a['annotation']->getRoute()->getRequirement('_method'), $methodOrder);
$methodB = array_search($b['annotation']->getRoute()->getRequirement('_method'), $methodOrder);
if ($a['annotation']->getRoute()->getPath() === $b['annotation']->getRoute()->getPath()) {
$methodA = array_search($a['annotation']->getRoute()->getMethods(), $methodOrder);
$methodB = array_search($b['annotation']->getRoute()->getMethods(), $methodOrder);

if ($methodA === $methodB) {
return strcmp(
$a['annotation']->getRoute()->getRequirement('_method'),
$b['annotation']->getRoute()->getRequirement('_method')
implode('|', $a['annotation']->getRoute()->getMethods()),
implode('|', $b['annotation']->getRoute()->getMethods())
);
}

return $methodA > $methodB ? 1 : -1;
}

return strcmp(
$a['annotation']->getRoute()->getPattern(),
$b['annotation']->getRoute()->getPattern()
$a['annotation']->getRoute()->getPath(),
$b['annotation']->getRoute()->getPath()
);
}

Expand Down
10 changes: 5 additions & 5 deletions Formatter/SwaggerFormatter.php
Expand Up @@ -567,17 +567,17 @@ public function setInfo($info)
/**
* Strips the base path from a URL path.
*
* @param $path
* @param $basePath
* @return mixed
*/
protected function stripBasePath($path)
protected function stripBasePath($basePath)
{
if ('/' === $this->basePath) {
return $path;
return $basePath;
}

$pattern = sprintf('#^%s#', preg_quote($this->basePath));
$subPath = preg_replace($pattern, '', $path);
$path = sprintf('#^%s#', preg_quote($this->basePath));
$subPath = preg_replace($path, '', $basePath);

return $subPath;
}
Expand Down
10 changes: 4 additions & 6 deletions Resources/config/swagger_routing.yml
@@ -1,11 +1,9 @@
nelmio_api_doc_swagger_resource_list:
pattern: /
path: /
methods: [GET]
defaults: { _controller: NelmioApiDocBundle:ApiDoc:swagger }
requirements:
_method: GET

nelmio_api_doc_swagger_api_declaration:
pattern: /{resource}
path: /{resource}
methods: [GET]
defaults: { _controller: NelmioApiDocBundle:ApiDoc:swagger }
requirements:
_method: GET
4 changes: 2 additions & 2 deletions Resources/doc/index.md
Expand Up @@ -228,8 +228,8 @@ class YourType extends AbstractType
```

The bundle will also get information from the routing definition
(`requirements`, `pattern`, etc), so to get the best out of it you should
define strict _method requirements etc.
(`requirements`, `path`, etc), so to get the best out of it you should
define strict methods requirements etc.

### Multiple API Documentation ("Views")

Expand Down
4 changes: 2 additions & 2 deletions Tests/Command/DumpCommandTest.php
Expand Up @@ -88,9 +88,9 @@ public static function viewProvider()
'/api/resources[0].uri' => '/api/resources.{_format}',
'/api/resources[1].method' => 'POST',
'/api/resources[1].uri' => '/api/resources.{_format}',
'/api/resources[2].method' => 'GET',
'/api/resources[2].method' => 'DELETE',
'/api/resources[2].uri' => '/api/resources/{id}.{_format}',
'/api/resources[3].method' => 'DELETE',
'/api/resources[3].method' => 'GET',
'/api/resources[3].uri' => '/api/resources/{id}.{_format}',
)
),
Expand Down
2 changes: 1 addition & 1 deletion Tests/Extractor/ApiDocExtractorTest.php
Expand Up @@ -145,7 +145,7 @@ public function testGetWithBadRoute()
$this->assertNull($data);
}

public function testGetWithInvalidPattern()
public function testGetWithInvalidPath()
{
$container = $this->getContainer();
$extractor = $container->get('nelmio_api_doc.extractor.api_doc_extractor');
Expand Down

0 comments on commit 32de80f

Please sign in to comment.