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 4 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
70 changes: 70 additions & 0 deletions core/swagger-decorator.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
# Override Swagger documentation
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll suggest to add a new documentation entry for Swagger.

I'll call the file swagger.md and the title Swagger Support.



Symfony allow to [decorates services](https://symfony.com/doc/current/service_container/service_decoration.html), here we need to decorate the
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

allows to decorate


`api_platform.swagger.normalizer.documentation`

1. declare the decorator and class

```yaml
# app/config/services.yml

services:
app.swagger.swagger_decorator:
decorates: api_platform.swagger.normalizer.documentation
class: AppBundle\Swagger\SwaggerDecorator
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you put the class name into single quotes (sometimes can cause problem to not escape it)

arguments: ['@app.swagger.swagger_decorator.inner']
```

```php
<?php
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add a comment with the path of the .php file?


namespace AppBundle\Swagger;

use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

/**
* Class SwaggerDecorator.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless, can be remove (or replaced by an explicit description).

*/
class SwaggerDecorator implements NormalizerInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

final (according to our CS)

{
/**
* @var NormalizerInterface
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Useless, will be inferred by the IDE, can be removed.

*/
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);

// Your code

return $docs;
}

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

2. In Swagger decorator you can acces to the docs

the variable `docs` is an array compose by paths :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

no space before the colon


`$docs['paths']['{path}']['{method}']['parameters']`

for example if you want to remove all references to your path ’/foos’ in method GET in the swagger you can make in the normalize function
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can do it in the normalize method


```php
unset($docs['paths']['/foos']['get']);
```


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. [Override Swagger documentation](core/swagger-decorator.md)

## Schema Generator: Generate Data Models from Open Vocabularies

Expand Down