API-Platform change Swagger generated documentation basePath

Just add a SwaggerDecorator as it follows:

Create the docrator class:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
<?php
// src/Swagger/SwaggerDecorator.php
namespace App\Swagger;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
final class SwaggerDecorator implements NormalizerInterface
{
private $decorated;
private $environment;
public function __construct(NormalizerInterface $decorated, $environment)
{
$this->decorated = $decorated;
$this->environment = $environment;
}
public function normalize($object, $format = null, array $context = [])
{
$docs = $this->decorated->normalize($object, $format, $context);
// Only override basePath for prod environment
if ($this->environment === 'prod') {
// Override basePath
$docs['basePath'] = '/your-new-base/v1';
}
return $docs;
}
public function supportsNormalization($data, $format = null)
{
return $this->decorated->supportsNormalization($data, $format);
}
}

And then register your decorator in your services definition:

1
2
3
4
5
6
7
8
# config/services.yml
# add more service definitions when explicit configuration is needed
# please note that last definitions always *replace* previous ones
App\Swagger\SwaggerDecorator:
decorates: 'api_platform.swagger.normalizer.documentation'
arguments: ['@App\Swagger\SwaggerDecorator.inner', '%kernel.environment%']
autoconfigure: false

Comments

TOC

2018-04-10

⬆︎TOP