# OpenAPI Specification Support (formerly Swagger)
API Platform natively support the [OpenAPI](https://www.openapis.org/) API specification format.

The specification of the API is available at the `/docs.json` path.
By default, OpenAPI v2 is used.
You can also get an OpenAPI v3-compliant version thanks to the `spec_version` query parameter: `/docs.json?spec_version=3`
It also integrates a customized version of [Swagger UI](https://swagger.io/swagger-ui/) and [ReDoc](https://rebilly.github.io/ReDoc/), some nice tools to display the
API documentation in a user friendly way.
## Using the OpenAPI Command
You can also dump an OpenAPI specification for your API by using the following command:
```
$ docker-compose exec php bin/console api:openapi:export
# OpenAPI v2, JSON format
$ docker-compose exec php bin/console api:openapi:export --yaml
# OpenAPI v2, YAML format
$ docker-compose exec php bin/console api:openapi:export --spec-version=3
# OpenAPI v3, JSON format
$ docker-compose exec php bin/console api:openapi:export --spec-version=3 --yaml
# OpenAPI v3, YAML format
$ docker-compose exec php bin/console api:openapi:export --output=swagger_docs.json
# Create a file containing the specification
```
## Overriding the OpenAPI Specification
Symfony allows to [decorate services](https://symfony.com/doc/current/service_container/service_decoration.html), here we
need to decorate `api_platform.swagger.normalizer.documentation`.
In the following example, we will see how to override the title of the Swagger documentation and add a custom filter for
the `GET` operation of `/foos` path.
```yaml
# api/config/services.yaml
services:
'App\Swagger\SwaggerDecorator':
decorates: 'api_platform.swagger.normalizer.api_gateway'
arguments: [ '@App\Swagger\SwaggerDecorator.inner' ]
autoconfigure: false
```
```php
decorated = $decorated;
}
public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decorated->normalize($object, $format, $context);
$customDefinition = [
'name' => 'fields',
'description' => 'Fields to remove of the output',
'default' => 'id',
'in' => 'query',
];
// e.g. add a custom parameter
$docs['paths']['/foos']['get']['parameters'][] = $customDefinition;
// e.g. remove an existing parameter
$docs['paths']['/foos']['get']['parameters'] = array_values(array_filter($docs['paths']['/foos']['get']['parameters'], function ($param){
return $param['name'] !== 'bar';
}));
// Override title
$docs['info']['title'] = 'My Api Foo';
return $docs;
}
public function supportsNormalization($data, $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
}
```
## Using the OpenAPI and Swagger Contexts
Sometimes you may want to change the information included in your OpenAPI documentation.
The following configuration will give you total control over your OpenAPI definitions:
```php
['user:read'],
'swagger_definition_name' => 'Read',
];
}
```
## Changing Operations in the OpenAPI Documentation
You also have full control over both built-in and custom operations documentation.
In Yaml:
```yaml
resources:
App\Entity\Rabbit:
collectionOperations:
create_user:
method: get
path: '/rabbit/rand'
controller: App\Controller\RandomRabbit
swagger_context:
summary: Random rabbit picture
description: >
# Pop a great rabbit picture by color!

parameters:
-
in: body
schema:
type: object
properties:
name: {type: string}
description: {type: string}
example:
name: Rabbit
description: Pink rabbit
```
or with XML:
```xml