diff --git a/README.md b/README.md index 29267fc..24fa4b9 100644 --- a/README.md +++ b/README.md @@ -74,9 +74,10 @@ swagger: When a call is made that is satisfiable by SwaggerBundle, it uses Symfony Dependency Injection service keys to find the delegation target of the request. It will assume the first segment in the Swagger paths is a resource name, and looks for a service with the key `swagger.controller.%resource_name%`. The class method to be called by defaults corresponds -to the HTTP method name, but may be overridden by including `operationId` in your spec. Controller methods that expect content can either - get the content from the `Request` object, or add a parameter named identical to the parameter with `in: body` set. - +to the HTTP method name, but may be overridden by including `operationId` in your spec. You can also completely override this default by +including an operationId referencing a DI key (using double colon notation, eg "my.controller.key:methodName"). + +Controller methods that expect content can either get the content from the `Request` object, or add a parameter named identical to the parameter with `in: body` set. Any of these will work (assuming the `in: body` parameter is named `body` in your spec): ```php @@ -306,6 +307,7 @@ class PetStoreApiTest extends WebTestCase } ``` +When using ApiTestCase, initSchemaManager() will also validate your Swagger spec against the official schema to ensure it is valid. ## License diff --git a/src/Dev/Test/ApiTestCase.php b/src/Dev/Test/ApiTestCase.php index 5b08929..ac954b4 100644 --- a/src/Dev/Test/ApiTestCase.php +++ b/src/Dev/Test/ApiTestCase.php @@ -60,7 +60,6 @@ public static function initSchemaManager($swaggerPath) ); if (!$validator->isValid()) { - var_dump( $validator->getErrors()); throw new \InvalidArgumentException( "Swagger '$swaggerPath' not valid" ); diff --git a/src/Routing/SwaggerRouteLoader.php b/src/Routing/SwaggerRouteLoader.php index 4ebf5a7..9287da0 100644 --- a/src/Routing/SwaggerRouteLoader.php +++ b/src/Routing/SwaggerRouteLoader.php @@ -62,9 +62,19 @@ public function load($resource, $type = null) ? substr($relativePath, 0, strpos($relativePath, '/')) : $relativePath; foreach ($methods as $methodName => $operationSpec) { - $operationName = isset($operationSpec['operationId']) ? $operationSpec['operationId'] : $methodName; + $operationName = $methodName; + $controllerKey = "swagger.controller.$resourceName:$operationName"; + if (isset($operationSpec['operationId'])) { + $operationName = $operationSpec['operationId']; + if (false !== strpos($operationSpec['operationId'], ':')) { + $controllerKey = $operationSpec['operationId']; + } else { + $controllerKey = "swagger.controller.$resourceName:$operationName"; + } + } + $defaults = [ - '_controller' => "swagger.controller.$resourceName:$operationName", + '_controller' => $controllerKey, '_definition' => $resource, '_swagger_path' => $path ]; diff --git a/src/Tests/Routing/SwaggerRouteLoaderTest.php b/src/Tests/Routing/SwaggerRouteLoaderTest.php index 8a4332c..35d585c 100644 --- a/src/Tests/Routing/SwaggerRouteLoaderTest.php +++ b/src/Tests/Routing/SwaggerRouteLoaderTest.php @@ -149,6 +149,33 @@ public function routeCollectionWillIncludeSeparateRoutesForSubPaths() $this->assertCount(3, $routes); } + /** + * @test + */ + public function canUseDiKeyAsOperationId() + { + $expected = 'my.controller.key:methodName'; + $pathDefinitions = [ + '/a' => [ + 'get' => [], + 'post' => [ + 'operationId' => $expected + ] + ], + '/b' => ['get' => []], + ]; + + $this->documentMock + ->expects($this->any()) + ->method('getPathDefinitions') + ->willReturn($pathDefinitions); + + $routes = $this->loader->load(self::DOCUMENT_PATH); + $actual = $routes->get('swagger.a.my.controller.key:methodName')->getDefault('_controller'); + $this->assertSame($expected, $actual); + var_dump($actual); + } + /** * @test */