From 718d0e4daeaf564a90a7fef2aeafab80032f17df Mon Sep 17 00:00:00 2001 From: John Kleijn Date: Tue, 26 Jan 2016 07:18:58 +0100 Subject: [PATCH] Now adding requirements for integers, string patterns and enums, fixes #42 --- src/Routing/SwaggerRouteLoader.php | 30 ++++++ src/Tests/Routing/SwaggerRouteLoaderTest.php | 108 +++++++++++++++++++ 2 files changed, 138 insertions(+) diff --git a/src/Routing/SwaggerRouteLoader.php b/src/Routing/SwaggerRouteLoader.php index 67dc866..425b995 100644 --- a/src/Routing/SwaggerRouteLoader.php +++ b/src/Routing/SwaggerRouteLoader.php @@ -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); diff --git a/src/Tests/Routing/SwaggerRouteLoaderTest.php b/src/Tests/Routing/SwaggerRouteLoaderTest.php index b70e406..1c598d2 100644 --- a/src/Tests/Routing/SwaggerRouteLoaderTest.php +++ b/src/Tests/Routing/SwaggerRouteLoaderTest.php @@ -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']); + } }