Skip to content
This repository has been archived by the owner on Jan 15, 2024. It is now read-only.

Commit

Permalink
Now adding requirements for integers, string patterns and enums, fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kleijnweb committed Jan 26, 2016
1 parent bb77c88 commit 718d0e4
Show file tree
Hide file tree
Showing 2 changed files with 138 additions and 0 deletions.
30 changes: 30 additions & 0 deletions src/Routing/SwaggerRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,37 @@ public function load($resource, $type = null)
'_definition' => $resource,
'_swagger_path' => $path
];


$requirements = [];
$operationDefinition = $document->getOperationDefinition($path, $methodName);

if (isset($operationDefinition['parameters'])) {
foreach ($operationDefinition['parameters'] as $paramDefinition) {
if ($paramDefinition['in'] === 'path' && isset($paramDefinition['type'])) {
switch ($paramDefinition['type']) {
case 'integer':
$requirements[$paramDefinition['name']] = '\d+';
break;
case 'string':
if (isset($paramDefinition['pattern'])) {
$requirements[$paramDefinition['name']] = $paramDefinition['pattern'];
break;
}
if (isset($paramDefinition['enum'])) {
$requirements[$paramDefinition['name']] = '(' .
implode('|', $paramDefinition['enum'])
. ')';
break;
}
break;
default:
//NOOP
}
}
}
}

$route = new Route($path, $defaults, $requirements);
$route->setMethods($methodName);
$fileName = pathinfo($resource, PATHINFO_FILENAME);
Expand Down
108 changes: 108 additions & 0 deletions src/Tests/Routing/SwaggerRouteLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,112 @@ public function routeCollectionWillContainPathFromSwaggerDoc()
sort($routePaths);
$this->assertSame($definitionPaths, $routePaths);
}

/**
* @test
*/
public function willAddRequirementsForIntegerPathParams()
{
$pathDefinitions = [
'/a' => [
'get' => [
'parameters' => [
['name' => 'foo', 'in' => 'path', 'type' => 'integer']
]
]
],
];

$this->documentMock
->expects($this->once())
->method('getPathDefinitions')
->willReturn($pathDefinitions);

$this->documentMock
->expects($this->once())
->method('getOperationDefinition')
->with('/a', 'get')
->willReturn($pathDefinitions['/a']['get']);

$routes = $this->loader->load(self::DOCUMENT_PATH);
$actual = $routes->get('swagger.path.a.get');
$this->assertNotNull($actual);
$requirements = $actual->getRequirements();
$this->assertNotNull($requirements);

$this->assertSame($requirements['foo'], '\d+');
}

/**
* @test
*/
public function willAddRequirementsForStringPatternParams()
{
$expected = '\d{2}hello';
$pathDefinitions = [
'/a' => [
'get' => [
'parameters' => [
['name' => 'aString', 'in' => 'path', 'type' => 'string', 'pattern' => $expected]
]
]
],
];

$this->documentMock
->expects($this->once())
->method('getPathDefinitions')
->willReturn($pathDefinitions);

$this->documentMock
->expects($this->once())
->method('getOperationDefinition')
->with('/a', 'get')
->willReturn($pathDefinitions['/a']['get']);

$routes = $this->loader->load(self::DOCUMENT_PATH);
$actual = $routes->get('swagger.path.a.get');
$this->assertNotNull($actual);
$requirements = $actual->getRequirements();
$this->assertNotNull($requirements);

$this->assertSame($expected, $requirements['aString']);
}

/**
* @test
*/
public function willAddRequirementsForStringEnumParams()
{
$enum = ['a', 'b', 'c'];
$expected = '(a|b|c)';
$pathDefinitions = [
'/a' => [
'get' => [
'parameters' => [
['name' => 'aString', 'in' => 'path', 'type' => 'string', 'enum' => $enum]
]
]
],
];

$this->documentMock
->expects($this->once())
->method('getPathDefinitions')
->willReturn($pathDefinitions);

$this->documentMock
->expects($this->once())
->method('getOperationDefinition')
->with('/a', 'get')
->willReturn($pathDefinitions['/a']['get']);

$routes = $this->loader->load(self::DOCUMENT_PATH);
$actual = $routes->get('swagger.path.a.get');
$this->assertNotNull($actual);
$requirements = $actual->getRequirements();
$this->assertNotNull($requirements);

$this->assertSame($expected, $requirements['aString']);
}
}

0 comments on commit 718d0e4

Please sign in to comment.