Skip to content

Commit 5a687f1

Browse files
authored
Merge pull request #706 from dunglas/openapi3
Document OpenAPI v3 support
2 parents a7c85c7 + ad96867 commit 5a687f1

File tree

1 file changed

+42
-26
lines changed

1 file changed

+42
-26
lines changed

core/swagger.md

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,38 @@
1-
# Swagger / Open API Support
1+
# OpenAPI Specification Support (formerly Swagger)
2+
3+
API Platform natively support the [Open API](https://www.openapis.org/) API specification format.
4+
5+
![Screenshot](../distribution/images/swagger-ui-1.png)
6+
7+
The specification of the API is available at the `/docs.json` path.
8+
By default, OpenAPI v2 is used.
9+
You can also get an OpenAPI v3 compliant version thanks to the `spec_version` query parameter: `/docs.json?spec_version=3`
210

3-
API Platform natively support the [Open API](https://www.openapis.org/) (formerly Swagger) API specification format.
411
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
512
API documentation in a user friendly way.
613

7-
![Screenshot](../distribution/images/swagger-ui-1.png)
14+
## Using the OpenAPI Command
15+
16+
You can also dump an OpenAPI specification for your API by using the provided command:
17+
18+
```
19+
$ docker-compose exec php bin/console api:openapi:export
20+
# OpenAPI v2, JSON format
21+
22+
$ docker-compose exec php bin/console api:openapi:export --yaml
23+
# OpenAPI v2, YAML format
24+
25+
$ docker-compose exec php bin/console api:openapi:export --spec-version=3
26+
# OpenAPI v3, JSON format
27+
28+
$ docker-compose exec php bin/console api:openapi:export --spec-version=3 --yaml
29+
# OpenAPI v3, YAML format
830
9-
## Overriding the Swagger Documentation
31+
$ docker-compose exec php bin/console api:openapi:export --output=swagger_docs.json
32+
# Create a file containing the specification
33+
```
34+
35+
## Overriding the OpenAPI Specification
1036

1137
Symfony allows to [decorate services](https://symfony.com/doc/current/service_container/service_decoration.html), here we
1238
need to decorate `api_platform.swagger.normalizer.documentation`.
@@ -73,10 +99,10 @@ final class SwaggerDecorator implements NormalizerInterface
7399
}
74100
```
75101

76-
## Using the Swagger Context
102+
## Using the OpenAPI and Swagger Contexts
77103

78-
Sometimes you may want to change the information included in your Swagger documentation.
79-
The following configuration will give you total control over your Swagger definitions:
104+
Sometimes you may want to change the information included in your OpenAPI documentation.
105+
The following configuration will give you total control over your OpenAPI definitions:
80106

81107
```php
82108
<?php
@@ -110,7 +136,7 @@ class Product // The class name will be used to name exposed resources
110136
*
111137
* @ApiProperty(
112138
* attributes={
113-
* "swagger_context"={
139+
* "openapi_context"={
114140
* "type"="string",
115141
* "enum"={"one", "two"},
116142
* "example"="one"
@@ -185,6 +211,8 @@ Will produce the following Swagger documentation:
185211
}
186212
```
187213

214+
To pass a context to the OpenAPI **v3** generator, use the `openapi_context` attribute (notice the prefix: `openapi_` instead of `swagger_`).
215+
188216
## Changing the Name of a Definition
189217

190218
API Platform generates a definition name based on the serializer `groups` defined
@@ -230,7 +258,7 @@ class User
230258
}
231259
```
232260

233-
## Changing Operations in the Swagger Documentation
261+
## Changing Operations in the OpenAPI Documentation
234262

235263
You also have full control over both built-in and custom operations documentation:
236264

@@ -314,17 +342,20 @@ or with XML:
314342

315343
![Impact on swagger ui](../distribution/images/swagger-ui-2.png)
316344

317-
## Changing the Swagger UI Location
345+
Again, you can use the `openapi_context` key instead of the `swagger_context` one to tweak the OpenAPI **v3** specification.
346+
347+
## Changing the Location of Swagger UI
318348

319349
Sometimes you may want to have the API at one location, and the Swagger UI at a different location. This can be done by disabling the Swagger UI from the API Platform configuration file and manually adding the Swagger UI controller.
320350

321-
### Disabling Swagger UI
351+
### Disabling Swagger UI or of ReDoc
322352

323353
```yaml
324354
# api/config/packages/api_platform.yaml
325355
api_platform:
326356
# ...
327357
enable_swagger_ui: false
358+
enable_re_doc: false
328359
```
329360
330361
### Manually Registering the Swagger UI Controller
@@ -338,21 +369,6 @@ swagger_ui:
338369
339370
Change `/docs` to your desired URI you wish Swagger to be accessible on.
340371

341-
## Using the Swagger Command
342-
343-
You can also dump your current Swagger documentation using the provided command:
344-
345-
```
346-
$ docker-compose exec php bin/console api:swagger:export
347-
# Swagger documentation in JSON format...
348-
349-
$ docker-compose exec php bin/console api:swagger:export --yaml
350-
# Swagger documentation in YAML format...
351-
352-
$ docker-compose exec php bin/console api:swagger:export --output=swagger_docs.json
353-
# Swagger documentation dumped directly into JSON file (add --yaml to change format)
354-
```
355-
356372
## Overriding the UI Template
357373

358374
As described [in the Symfony documentation](https://symfony.com/doc/current/templating/overriding.html), it's possible to override the Twig template that loads Swagger UI and renders the documentation:

0 commit comments

Comments
 (0)