Skip to content

Commit 0d0dfac

Browse files
Timherlauddunglas
authored andcommitted
Add documentation for override Swagger docs (#236)
1 parent 12b0701 commit 0d0dfac

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

core/swagger.md

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
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+

index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
23. [AngularJS Integration](core/angularjs-integration.md)
8686
1. [Restangular](core/angularjs-integration.md#restangular)
8787
2. [ng-admin](core/angularjs-integration.md#ng-admin)
88+
24. [Swagger Support](core/swagger.md)
8889

8990
## Schema Generator: Generate Data Models from Open Vocabularies
9091

0 commit comments

Comments
 (0)