Skip to content

Add documentation for override Swagger docs #236

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Aug 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 64 additions & 0 deletions core/swagger.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
# Swagger Support

## Override Swagger documentation
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`
### Example
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
# app/config/services.yml

services:
app.swagger.swagger_decorator:
decorates: api_platform.swagger.normalizer.documentation
class: 'AppBundle\Swagger\SwaggerDecorator'
arguments: ['@app.swagger.swagger_decorator.inner']
```

```php
<?php

// src\AppBundle\Swagger\SwaggerDecorator.php

namespace AppBundle\Swagger;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

final class SwaggerDecorator implements NormalizerInterface
{
private $decorated;

public function __construct(NormalizerInterface $decorated)
{
$this->decorated = $decorated;
}

public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decorated->normalize($object, $format, $context);

$customDefinition = [
'name' => 'fields',
'definition' => 'Fields to remove of the outpout',
'default' => 'id',
'in' => 'query',
];


// e.g. add a custom parameter
$docs['paths']['/foos']['get']['parameters'][] = $customDefinition;

// Override title
$docs['info']['title'] = 'My Api Foo';

return $docs;
}

public function supportsNormalization($data, $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
}
```

1 change: 1 addition & 0 deletions index.md
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
23. [AngularJS Integration](core/angularjs-integration.md)
1. [Restangular](core/angularjs-integration.md#restangular)
2. [ng-admin](core/angularjs-integration.md#ng-admin)
24. [Swagger Support](core/swagger.md)

## Schema Generator: Generate Data Models from Open Vocabularies

Expand Down