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

Commit

Permalink
Refs #26
Browse files Browse the repository at this point in the history
  • Loading branch information
kleijnweb committed Jan 8, 2016
1 parent f8da476 commit 4c0102b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 6 deletions.
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion src/Dev/Test/ApiTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ public static function initSchemaManager($swaggerPath)
);

if (!$validator->isValid()) {
var_dump( $validator->getErrors());
throw new \InvalidArgumentException(
"Swagger '$swaggerPath' not valid"
);
Expand Down
14 changes: 12 additions & 2 deletions src/Routing/SwaggerRouteLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
];
Expand Down
27 changes: 27 additions & 0 deletions src/Tests/Routing/SwaggerRouteLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down

0 comments on commit 4c0102b

Please sign in to comment.