Skip to content

Commit 5d19d84

Browse files
committed
DI: Improve swagger versions parameter (fix #3115)
1 parent 3485028 commit 5d19d84

File tree

4 files changed

+121
-28
lines changed

4 files changed

+121
-28
lines changed

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -313,6 +313,8 @@ private function registerOAuthConfiguration(ContainerBuilder $container, array $
313313
private function registerSwaggerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
314314
{
315315
if (empty($config['swagger']['versions'])) {
316+
$container->setParameter('api_platform.swagger.versions', []);
317+
316318
return;
317319
}
318320

@@ -344,6 +346,10 @@ private function registerJsonLdHydraConfiguration(ContainerBuilder $container, a
344346
return;
345347
}
346348

349+
if (!$container->has('api_platform.json_schema.schema_factory')) {
350+
$container->removeDefinition('api_platform.hydra.json_schema.schema_factory');
351+
}
352+
347353
$loader->load('jsonld.xml');
348354
$loader->load('hydra.xml');
349355

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<argument type="service" id="api_platform.hydra.json_schema.schema_factory.inner" />
8383
</service>
8484

85+
8586
</services>
8687

8788
</container>

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
<argument>%api_platform.formats%</argument>
2929
<tag name="console.command" />
3030
</service>
31+
3132
</services>
3233

3334
</container>

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

Lines changed: 113 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,46 @@ public function testKeepCachePoolClearerCacheWarmerWithDebug()
756756
$this->extension->load(self::DEFAULT_CONFIG, $containerBuilder);
757757
}
758758

759-
private function getPartialContainerBuilderProphecy()
759+
public function testDisabledSwaggerAndHydra()
760+
{
761+
$config = self::DEFAULT_CONFIG;
762+
$config['api_platform']['enable_swagger'] = false;
763+
$config['api_platform']['enable_swagger_ui'] = false;
764+
$config['api_platform']['formats'] = [
765+
'jsonhal' => ['mime_types' => ['application/hal+json']],
766+
];
767+
768+
$containerBuilderProphecy = $this->getBaseContainerBuilderProphecy(['orm'], $config);
769+
$containerBuilderProphecy->setDefinition('api_platform.swagger.action.ui', Argument::type(Definition::class))->shouldNotBeCalled();
770+
$containerBuilderProphecy->setDefinition('api_platform.swagger.listener.ui', Argument::type(Definition::class))->shouldNotBeCalled();
771+
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', true)->shouldNotBeCalled();
772+
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', true)->shouldNotBeCalled();
773+
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', false)->shouldNotBeCalled();
774+
$containerBuilderProphecy->setParameter('api_platform.enable_re_doc', true)->shouldNotBeCalled();
775+
$containerBuilderProphecy->setParameter('api_platform.enable_re_doc', false)->shouldNotBeCalled();
776+
$containerBuilder = $containerBuilderProphecy->reveal();
777+
$this->extension->load($config, $containerBuilder);
778+
}
779+
780+
public function testDisabledSwagger()
781+
{
782+
$config = self::DEFAULT_CONFIG;
783+
$config['api_platform']['enable_swagger'] = false;
784+
$config['api_platform']['enable_swagger_ui'] = false;
785+
786+
$containerBuilderProphecy = $this->getBaseContainerBuilderProphecy(['orm'], $config);
787+
$containerBuilderProphecy->setDefinition('api_platform.swagger.action.ui', Argument::type(Definition::class))->shouldNotBeCalled();
788+
$containerBuilderProphecy->setDefinition('api_platform.swagger.listener.ui', Argument::type(Definition::class))->shouldNotBeCalled();
789+
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', true)->shouldNotBeCalled();
790+
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', true)->shouldNotBeCalled();
791+
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', false)->shouldNotBeCalled();
792+
$containerBuilderProphecy->setParameter('api_platform.enable_re_doc', true)->shouldNotBeCalled();
793+
$containerBuilderProphecy->setParameter('api_platform.enable_re_doc', false)->shouldNotBeCalled();
794+
$containerBuilder = $containerBuilderProphecy->reveal();
795+
$this->extension->load($config, $containerBuilder);
796+
}
797+
798+
private function getPartialContainerBuilderProphecy($configuration = null)
760799
{
761800
$parameterBag = new EnvPlaceholderParameterBag();
762801

@@ -782,7 +821,7 @@ private function getPartialContainerBuilderProphecy()
782821
'api_platform.collection.order_parameter_name' => 'order',
783822
'api_platform.description' => 'description',
784823
'api_platform.error_formats' => ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']],
785-
'api_platform.formats' => ['jsonld' => ['application/ld+json'], 'jsonhal' => ['application/hal+json']],
824+
'api_platform.formats' => null === $configuration ? ['jsonld' => ['application/ld+json'], 'jsonhal' => ['application/hal+json']] : $this->getFormatsFromConfiguration($configuration['api_platform']['formats']) ?? [],
786825
'api_platform.patch_formats' => [],
787826
'api_platform.exception_to_status' => [
788827
ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
@@ -974,9 +1013,12 @@ private function getPartialContainerBuilderProphecy()
9741013
return $containerBuilderProphecy;
9751014
}
9761015

977-
private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLoad = ['orm'])
1016+
private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLoad = ['orm'], $configuration = null)
9781017
{
979-
$containerBuilderProphecy = $this->getPartialContainerBuilderProphecy();
1018+
$hasSwagger = null === $configuration || true === $configuration['api_platform']['enable_swagger'] ?? false;
1019+
$hasHydra = null === $configuration || isset($configuration['api_platform']['formats']['jsonld']);
1020+
1021+
$containerBuilderProphecy = $this->getPartialContainerBuilderProphecy($configuration);
9801022

9811023
$containerBuilderProphecy->hasParameter('kernel.debug')->willReturn(true);
9821024
$containerBuilderProphecy->getParameter('kernel.debug')->willReturn(false);
@@ -1060,8 +1102,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
10601102
'api_platform.oauth.tokenUrl' => '/oauth/v2/token',
10611103
'api_platform.oauth.authorizationUrl' => '/oauth/v2/auth',
10621104
'api_platform.oauth.scopes' => [],
1063-
'api_platform.swagger.versions' => [2, 3],
1064-
'api_platform.swagger.api_keys' => [],
10651105
'api_platform.enable_swagger_ui' => true,
10661106
'api_platform.enable_re_doc' => true,
10671107
'api_platform.graphql.enabled' => true,
@@ -1075,6 +1115,13 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
10751115
'api_platform.elasticsearch.enabled' => false,
10761116
];
10771117

1118+
if ($hasSwagger) {
1119+
$parameters['api_platform.swagger.versions'] = [2, 3];
1120+
$parameters['api_platform.swagger.api_keys'] = [];
1121+
} else {
1122+
$parameters['api_platform.swagger.versions'] = [];
1123+
}
1124+
10781125
foreach ($parameters as $key => $value) {
10791126
$containerBuilderProphecy->setParameter($key, $value)->shouldBeCalled();
10801127
}
@@ -1150,20 +1197,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11501197
'api_platform.http_cache.listener.response.configure',
11511198
'api_platform.http_cache.purger.varnish_client',
11521199
'api_platform.http_cache.purger.varnish',
1153-
'api_platform.hydra.listener.response.add_link_header',
1154-
'api_platform.hydra.normalizer.collection',
1155-
'api_platform.hydra.normalizer.collection_filters',
1156-
'api_platform.hydra.normalizer.constraint_violation_list',
1157-
'api_platform.hydra.normalizer.documentation',
1158-
'api_platform.hydra.normalizer.entrypoint',
1159-
'api_platform.hydra.normalizer.error',
1160-
'api_platform.hydra.normalizer.partial_collection_view',
1161-
'api_platform.hydra.json_schema.schema_factory',
1162-
'api_platform.jsonld.action.context',
1163-
'api_platform.jsonld.context_builder',
1164-
'api_platform.jsonld.encoder',
1165-
'api_platform.jsonld.normalizer.item',
1166-
'api_platform.jsonld.normalizer.object',
11671200
'api_platform.listener.view.validate',
11681201
'api_platform.listener.view.validate_query_parameters',
11691202
'api_platform.mercure.listener.response.add_link_header',
@@ -1187,13 +1220,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11871220
'api_platform.problem.normalizer.constraint_violation_list',
11881221
'api_platform.problem.normalizer.error',
11891222
'api_platform.swagger.action.ui',
1190-
'api_platform.swagger.command.swagger_command',
11911223
'api_platform.swagger.listener.ui',
1192-
'api_platform.swagger.normalizer.api_gateway',
1193-
'api_platform.swagger.normalizer.documentation',
1194-
'api_platform.json_schema.type_factory',
1195-
'api_platform.json_schema.schema_factory',
1196-
'api_platform.json_schema.json_schema_generate_command',
11971224
'api_platform.validator',
11981225
'test.api_platform.client',
11991226
];
@@ -1226,6 +1253,41 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
12261253
$definitions[] = 'api_platform.doctrine.metadata_factory';
12271254
}
12281255

1256+
// Only when swagger is enabled
1257+
if ($hasSwagger) {
1258+
$definitions[] = 'api_platform.swagger.command.swagger_command';
1259+
$definitions[] = 'api_platform.swagger.normalizer.api_gateway';
1260+
$definitions[] = 'api_platform.swagger.normalizer.documentation';
1261+
$definitions[] = 'api_platform.json_schema.type_factory';
1262+
$definitions[] = 'api_platform.json_schema.schema_factory';
1263+
$definitions[] = 'api_platform.json_schema.json_schema_generate_command';
1264+
}
1265+
1266+
// has jsonld
1267+
if ($hasHydra) {
1268+
$containerBuilderProphecy->has('api_platform.json_schema.schema_factory')->shouldBeCalled()->willReturn($hasSwagger);
1269+
1270+
if (!$hasSwagger) {
1271+
$containerBuilderProphecy->has('api_platform.json_schema.schema_factory')->shouldBeCalled()->willReturn(false);
1272+
$containerBuilderProphecy->removeDefinition('api_platform.hydra.json_schema.schema_factory')->shouldBeCalled();
1273+
}
1274+
1275+
$definitions[] = 'api_platform.hydra.json_schema.schema_factory';
1276+
$definitions[] = 'api_platform.hydra.listener.response.add_link_header';
1277+
$definitions[] = 'api_platform.hydra.normalizer.collection';
1278+
$definitions[] = 'api_platform.hydra.normalizer.collection_filters';
1279+
$definitions[] = 'api_platform.hydra.normalizer.constraint_violation_list';
1280+
$definitions[] = 'api_platform.hydra.normalizer.documentation';
1281+
$definitions[] = 'api_platform.hydra.normalizer.entrypoint';
1282+
$definitions[] = 'api_platform.hydra.normalizer.error';
1283+
$definitions[] = 'api_platform.hydra.normalizer.partial_collection_view';
1284+
$definitions[] = 'api_platform.jsonld.action.context';
1285+
$definitions[] = 'api_platform.jsonld.context_builder';
1286+
$definitions[] = 'api_platform.jsonld.encoder';
1287+
$definitions[] = 'api_platform.jsonld.normalizer.item';
1288+
$definitions[] = 'api_platform.jsonld.normalizer.object';
1289+
}
1290+
12291291
foreach ($definitions as $definition) {
12301292
$containerBuilderProphecy->setDefinition($definition, Argument::type(Definition::class))->shouldBeCalled();
12311293
}
@@ -1247,8 +1309,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
12471309
NumericFilter::class => 'api_platform.doctrine.orm.numeric_filter',
12481310
ExistsFilter::class => 'api_platform.doctrine.orm.exists_filter',
12491311
GraphQlSerializerContextBuilderInterface::class => 'api_platform.graphql.serializer.context_builder',
1250-
TypeFactoryInterface::class => 'api_platform.json_schema.type_factory',
1251-
SchemaFactoryInterface::class => 'api_platform.json_schema.schema_factory',
12521312
];
12531313

12541314
if (\in_array('odm', $doctrineIntegrationsToLoad, true)) {
@@ -1266,6 +1326,14 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
12661326
];
12671327
}
12681328

1329+
// Only when swagger is enabled
1330+
if ($hasSwagger) {
1331+
$aliases += [
1332+
TypeFactoryInterface::class => 'api_platform.json_schema.type_factory',
1333+
SchemaFactoryInterface::class => 'api_platform.json_schema.schema_factory',
1334+
];
1335+
}
1336+
12691337
foreach ($aliases as $alias => $service) {
12701338
$containerBuilderProphecy->setAlias($alias, $service)->shouldBeCalled();
12711339
}
@@ -1280,4 +1348,21 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
12801348

12811349
return $containerBuilderProphecy;
12821350
}
1351+
1352+
private function getFormatsFromConfiguration(array $formatsConfiguration)
1353+
{
1354+
$formats = [];
1355+
1356+
foreach ($formatsConfiguration as $format => $mimeTypes) {
1357+
if (!isset($formats[$format])) {
1358+
$formats[$format] = [];
1359+
}
1360+
1361+
foreach ($mimeTypes['mime_types'] as $mimeType) {
1362+
$formats[$format][] = $mimeType;
1363+
}
1364+
}
1365+
1366+
return $formats;
1367+
}
12831368
}

0 commit comments

Comments
 (0)