2015-10-27 10:27:13 +01:00
Swagger Support
---------------
2014-06-25 11:16:47 -07:00
2015-10-27 10:27:13 +01:00
It is possible to make your application produce Swagger-compliant JSON output
based on `@ApiDoc` annotations, which can be used for consumption by
[swagger-ui ](https://github.com/wordnik/swagger-ui ).
2014-06-25 11:16:47 -07:00
2015-10-27 10:27:13 +01:00
### Annotation options
2014-06-25 11:16:47 -07:00
A couple of properties has been added to `@ApiDoc` :
To define a __resource description__ :
```php
< ?php
/**
* @ApiDoc (
* resource=true,
* resourceDescription="Operations on users.",
* description="Retrieve list of users."
* )
*/
public function listUsersAction()
{
/* Stuff */
}
```
2015-10-27 10:27:13 +01:00
The `resourceDescription` is distinct from `description` as it applies to the
whole resource group and not just the particular API endpoint.
2014-06-25 11:16:47 -07:00
2014-07-31 10:18:25 -07:00
#### Defining a form-type as a GET form
2015-10-27 10:27:13 +01:00
If you use forms to capture GET requests, you will have to specify the
`paramType` to `query` in the annotation:
2014-07-31 10:18:25 -07:00
```php
< ?php
/**
* @ApiDoc (
* input = {"class" = "Foo\ContentBundle\Form\SearchType", "paramType" = "query"},
* ...
* )
*/
2015-10-27 10:27:13 +01:00
2014-07-31 10:18:25 -07:00
public function searchAction(Request $request)
{
```
### Multiple response models
2015-10-27 10:27:13 +01:00
Swagger provides you the ability to specify alternate output models for
different status codes. Example, `200` would return your default resource object
in JSON form, but `400` may return a custom validation error list object. This
can be specified through the `responseMap` property:
2014-06-25 11:16:47 -07:00
```php
< ?php
/**
* @ApiDoc (
* description="Retrieve list of users.",
* statusCodes={
* 400 = "Validation failed."
* },
* responseMap={
* 200 = "FooBundle\Entity\User",
* 400 = {
* "class"="CommonBundle\Model\ValidationErrors",
* "parsers"={"Nelmio\ApiDocBundle\Parser\JmsMetadataParser"}
* }
* }
* )
*/
public function updateUserAction()
{
/* Stuff */
}
```
This will tell Swagger that `CommonBundle\Model\ValidationErrors` is returned when this endpoint returns a `400 Validation failed.` HTTP response.
__Note:__ You can omit the `200` entry in the `responseMap` property and specify the default `output` property instead. That will result on the same thing.
2014-07-31 10:18:25 -07:00
### Integration with `wordnik/swagger-ui`
2014-06-25 11:16:47 -07:00
2014-07-31 10:18:25 -07:00
You could import the routes for use with [`swagger-ui` ](https://github.com/wordnik/swagger-ui ):
2014-06-25 11:16:47 -07:00
```yml
#app/config/routing.yml
nelmio_api_swagger:
resource: "@NelmioApiDocBundle/Resources/config/swagger_routing .yml"
prefix: /api-docs
```
2014-08-26 17:27:41 -07:00
Et voila!, simply specify http://yourdomain.com/api-docs in your `swagger-ui` instance and you are good to go.
2015-10-27 10:27:13 +01:00
__Note__: If your `swagger-ui` instance does not live under the same domain, you
will probably encounter some problems related to same-origin policy violations.
[NelmioCorsBundle ](https://github.com/nelmio/NelmioCorsBundle ) can solve this
problem for you. Read through how to allow cross-site requests for the
`/api-docs/*` pages.
2014-06-25 11:16:47 -07:00
2014-07-31 10:18:25 -07:00
### Dumping the Swagger-compliant JSON API definitions
2014-06-25 11:16:47 -07:00
2014-07-31 10:18:25 -07:00
To display all JSON definitions:
2014-06-25 11:16:47 -07:00
```
2014-07-31 10:18:25 -07:00
php app/console api:swagger:dump
2014-06-25 11:16:47 -07:00
```
2014-07-31 10:18:25 -07:00
To dump just the resource list:
2014-06-25 11:16:47 -07:00
```
2014-07-31 10:18:25 -07:00
php app/console api:swagger:dump --list-only
2014-06-25 11:16:47 -07:00
```
2014-07-31 10:18:25 -07:00
To dump just the API definition the `users` resource:
2014-06-25 11:16:47 -07:00
```
2014-07-31 10:18:25 -07:00
php app/console api:swagger:dump --resource=users
2014-06-25 11:16:47 -07:00
```
2014-06-25 14:20:30 -07:00
2014-07-31 10:18:25 -07:00
Specify the `--pretty` flag to display a prettified JSON output.
2014-08-07 15:32:39 -07:00
2014-07-31 10:18:25 -07:00
#### Dump to files
2014-08-07 15:32:39 -07:00
2014-07-31 10:18:25 -07:00
You can specify the destination if you wish to dump the JSON definition to a file:
2014-08-07 15:32:39 -07:00
2014-07-31 10:18:25 -07:00
```
php app/console api:swagger:dump --list-only swagger-docs/api-docs.json
php app/console api:swagger:dump --resource=users swagger-docs/users.json
```
Or, you can dump everything into a directory in one command:
2014-08-07 15:32:39 -07:00
```
2014-07-31 10:18:25 -07:00
php app/console api:swagger:dump swagger-docs
```
### Model naming
2015-10-27 10:27:13 +01:00
By default, the model naming strategy used is the `dot_notation` strategy. The
model IDs are simply the Fully Qualified Class Name (FQCN) of the class
associated to it, with the `\` replaced with `.` :
2014-07-31 10:18:25 -07:00
`Vendor\UserBundle\Entity\User => Vendor.UserBundle.Entity.User`
2015-10-27 10:27:13 +01:00
You can also change the `model_naming_strategy` in the configuration to
`last_segment_only` , if you want model IDs to be just the class name minus the
namespaces (`Vendor\UserBundle\Entity\User => User` ). This will not afford you
the guarantee that model IDs are unique, but that would really just depend on
the classes you have in use.
2014-08-07 15:32:39 -07:00
2015-10-27 10:27:13 +01:00
---
2014-06-25 14:20:30 -07:00
2015-10-27 10:27:13 +01:00
[back to index ](index.md )