|
| 1 | +# Swagger Support |
| 2 | + |
| 3 | +## Override Swagger documentation |
| 4 | +Symfony allows to [decorate services](https://symfony.com/doc/current/service_container/service_decoration.html), here we need to decorate |
| 5 | +`api_platform.swagger.normalizer.documentation` |
| 6 | +### Example |
| 7 | +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 |
| 8 | + |
| 9 | +```yaml |
| 10 | + # app/config/services.yml |
| 11 | + |
| 12 | +services: |
| 13 | + app.swagger.swagger_decorator: |
| 14 | + decorates: api_platform.swagger.normalizer.documentation |
| 15 | + class: 'AppBundle\Swagger\SwaggerDecorator' |
| 16 | + arguments: ['@app.swagger.swagger_decorator.inner'] |
| 17 | +``` |
| 18 | +
|
| 19 | +```php |
| 20 | +<?php |
| 21 | + |
| 22 | +// src\AppBundle\Swagger\SwaggerDecorator.php |
| 23 | + |
| 24 | +namespace AppBundle\Swagger; |
| 25 | + |
| 26 | +use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
| 27 | + |
| 28 | +final class SwaggerDecorator implements NormalizerInterface |
| 29 | +{ |
| 30 | + private $decorated; |
| 31 | + |
| 32 | + public function __construct(NormalizerInterface $decorated) |
| 33 | + { |
| 34 | + $this->decorated = $decorated; |
| 35 | + } |
| 36 | + |
| 37 | + public function normalize($object, $format = null, array $context = []) |
| 38 | + { |
| 39 | + $docs = $this->decorated->normalize($object, $format, $context); |
| 40 | + |
| 41 | + $customDefinition = [ |
| 42 | + 'name' => 'fields', |
| 43 | + 'definition' => 'Fields to remove of the outpout', |
| 44 | + 'default' => 'id', |
| 45 | + 'in' => 'query', |
| 46 | + ]; |
| 47 | + |
| 48 | + |
| 49 | + // e.g. add a custom parameter |
| 50 | + $docs['paths']['/foos']['get']['parameters'][] = $customDefinition; |
| 51 | + |
| 52 | + // Override title |
| 53 | + $docs['info']['title'] = 'My Api Foo'; |
| 54 | + |
| 55 | + return $docs; |
| 56 | + } |
| 57 | + |
| 58 | + public function supportsNormalization($data, $format = null) |
| 59 | + { |
| 60 | + return $this->decorated->supportsNormalization($data, $format); |
| 61 | + } |
| 62 | +} |
| 63 | +``` |
| 64 | + |
0 commit comments