Skip to content

Commit 87e2f4c

Browse files
authored
Merge pull request #841 from Simperfit/feature/add-possibility-to-disable-swagger-ui
fix #804 - add a parameter to disable swagger ui when not needed
2 parents f62ab14 + 6c42e66 commit 87e2f4c

File tree

6 files changed

+27
-13
lines changed

6 files changed

+27
-13
lines changed

src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,13 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
141141
}
142142

143143
$loader->load('swagger.xml');
144-
$container->setParameter('api_platform.enable_swagger', (string) $config['enable_swagger']);
144+
145+
if ($config['enable_swagger_ui']) {
146+
$loader->load('swagger-ui.xml');
147+
$container->setParameter('api_platform.enable_swagger_ui', $config['enable_swagger_ui']);
148+
}
149+
150+
$container->setParameter('api_platform.enable_swagger', $config['enable_swagger']);
145151
}
146152

147153
/**

src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function getConfigTreeBuilder()
5454
->booleanNode('enable_fos_user')->defaultValue(false)->info('Enable the FOSUserBundle integration.')->end()
5555
->booleanNode('enable_nelmio_api_doc')->defaultValue(false)->info('Enable the Nelmio Api doc integration.')->end()
5656
->booleanNode('enable_swagger')->defaultValue(true)->info('Enable the Swagger documentation and export.')->end()
57+
->booleanNode('enable_swagger_ui')->defaultValue(true)->info('Enable Swagger ui.')->end()
5758

5859
->arrayNode('collection')
5960
->addDefaultsIfNotSet()
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
<?xml version="1.0" ?>
2+
3+
<container xmlns="http://symfony.com/schema/dic/services"
4+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
5+
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">
6+
7+
<services>
8+
9+
<service id="api_platform.swagger.listener.ui" class="ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener">
10+
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
11+
</service>
12+
13+
</services>
14+
15+
</container>

src/Bridge/Symfony/Bundle/Resources/config/swagger.xml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@
4545
<argument>%api_platform.title%</argument>
4646
<argument>%api_platform.description%</argument>
4747
</service>
48-
49-
<!-- Event listener -->
50-
51-
<service id="api_platform.swagger.listener.ui" class="ApiPlatform\Core\Bridge\Symfony\Bundle\EventListener\SwaggerUiListener">
52-
<tag name="kernel.event_listener" event="kernel.request" method="onKernelRequest" />
53-
</service>
54-
48+
5549
</services>
5650

5751
</container>

tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ public function testPrependWhenNotEnabled()
121121
public function testLoadDefaultConfig()
122122
{
123123
$containerBuilderProphecy = $this->getContainerBuilderProphecy();
124-
$containerBuilderProphecy->setParameter('api_platform.enable_swagger', '1')->shouldBeCalled();
125124
$containerBuilder = $containerBuilderProphecy->reveal();
126125

127126
$this->extension->load(self::DEFAULT_CONFIG, $containerBuilder);
@@ -133,7 +132,6 @@ public function testSetNameConverter()
133132

134133
$containerBuilderProphecy = $this->getContainerBuilderProphecy();
135134
$containerBuilderProphecy->setAlias('api_platform.name_converter', $nameConverterId)->shouldBeCalled();
136-
$containerBuilderProphecy->setParameter('api_platform.enable_swagger', '1')->shouldBeCalled();
137135
$containerBuilder = $containerBuilderProphecy->reveal();
138136

139137
$this->extension->load(array_merge_recursive(self::DEFAULT_CONFIG, ['api_platform' => ['name_converter' => $nameConverterId]]), $containerBuilder);
@@ -147,7 +145,6 @@ public function testEnableFosUser()
147145
'FOSUserBundle' => FOSUserBundle::class,
148146
])->shouldBeCalled();
149147
$containerBuilderProphecy->setDefinition('api_platform.fos_user.event_listener', Argument::type(Definition::class))->shouldBeCalled();
150-
$containerBuilderProphecy->setParameter('api_platform.enable_swagger', '1')->shouldBeCalled();
151148
$containerBuilder = $containerBuilderProphecy->reveal();
152149

153150
$this->extension->load(array_merge_recursive(self::DEFAULT_CONFIG, ['api_platform' => ['enable_fos_user' => true]]), $containerBuilder);
@@ -162,7 +159,6 @@ public function testEnableNelmioApiDoc()
162159
])->shouldBeCalled();
163160
$containerBuilderProphecy->setDefinition('api_platform.nelmio_api_doc.annotations_provider', Argument::type(Definition::class))->shouldBeCalled();
164161
$containerBuilderProphecy->setDefinition('api_platform.nelmio_api_doc.parser', Argument::type(Definition::class))->shouldBeCalled();
165-
$containerBuilderProphecy->setParameter('api_platform.enable_swagger', '1')->shouldBeCalled();
166162
$containerBuilder = $containerBuilderProphecy->reveal();
167163

168164
$this->extension->load(array_merge_recursive(self::DEFAULT_CONFIG, ['api_platform' => ['enable_nelmio_api_doc' => true]]), $containerBuilder);
@@ -172,7 +168,6 @@ public function testDisableEagerLoadingExtension()
172168
{
173169
$containerBuilderProphecy = $this->getContainerBuilderProphecy();
174170
$containerBuilderProphecy->setParameter('api_platform.eager_loading.enabled', false)->shouldBeCalled();
175-
$containerBuilderProphecy->setParameter('api_platform.enable_swagger', '1')->shouldBeCalled();
176171
$containerBuilderProphecy->removeDefinition('api_platform.doctrine.orm.query_extension.eager_loading')->shouldBeCalled();
177172
$containerBuilder = $containerBuilderProphecy->reveal();
178173
$this->extension->load(array_merge_recursive(self::DEFAULT_CONFIG, ['api_platform' => ['eager_loading' => ['enabled' => false]]]), $containerBuilder);
@@ -202,6 +197,8 @@ private function getContainerBuilderProphecy()
202197
'api_platform.collection.pagination.page_parameter_name' => 'page',
203198
'api_platform.description' => 'description',
204199
'api_platform.error_formats' => ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']],
200+
'api_platform.enable_swagger' => true,
201+
'api_platform.enable_swagger_ui' => true,
205202
'api_platform.formats' => ['jsonld' => ['application/ld+json'], 'jsonhal' => ['application/hal+json']],
206203
'api_platform.exception_to_status' => [ExceptionInterface::class => Response::HTTP_BAD_REQUEST, InvalidArgumentException::class => Response::HTTP_BAD_REQUEST],
207204
'api_platform.title' => 'title',

tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ public function testDefaultConfig()
7070
'enable_fos_user' => false,
7171
'enable_nelmio_api_doc' => false,
7272
'enable_swagger' => true,
73+
'enable_swagger_ui' => true,
7374
'eager_loading' => [
7475
'enabled' => true,
7576
'max_joins' => 30,

0 commit comments

Comments
 (0)