Skip to content

Commit 2ef093b

Browse files
authored
Merge pull request #3167 from soyuka/fix-swagger-versions
DI: Improve swagger versions parameter (fix #3115)
2 parents 9425f08 + 3cab522 commit 2ef093b

File tree

2 files changed

+119
-29
lines changed

2 files changed

+119
-29
lines changed

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

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,6 +312,8 @@ private function registerOAuthConfiguration(ContainerBuilder $container, array $
312312
*/
313313
private function registerSwaggerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
314314
{
315+
$container->setParameter('api_platform.swagger.versions', $config['swagger']['versions']);
316+
315317
if (empty($config['swagger']['versions'])) {
316318
return;
317319
}
@@ -325,7 +327,6 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
325327
$container->setParameter('api_platform.enable_re_doc', $config['enable_re_doc']);
326328
}
327329

328-
$container->setParameter('api_platform.swagger.versions', $config['swagger']['versions']);
329330
$container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']);
330331
}
331332

@@ -347,6 +348,10 @@ private function registerJsonLdHydraConfiguration(ContainerBuilder $container, a
347348
$loader->load('jsonld.xml');
348349
$loader->load('hydra.xml');
349350

351+
if (!$container->has('api_platform.json_schema.schema_factory')) {
352+
$container->removeDefinition('api_platform.hydra.json_schema.schema_factory');
353+
}
354+
350355
if (!$docEnabled) {
351356
$container->removeDefinition('api_platform.hydra.listener.response.add_link_header');
352357
}

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

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

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

@@ -783,7 +822,7 @@ private function getPartialContainerBuilderProphecy()
783822
'api_platform.collection.order_parameter_name' => 'order',
784823
'api_platform.description' => 'description',
785824
'api_platform.error_formats' => ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']],
786-
'api_platform.formats' => ['jsonld' => ['application/ld+json'], 'jsonhal' => ['application/hal+json']],
825+
'api_platform.formats' => null === $configuration ? ['jsonld' => ['application/ld+json'], 'jsonhal' => ['application/hal+json']] : $this->getFormatsFromConfiguration($configuration['api_platform']['formats']) ?? [],
787826
'api_platform.patch_formats' => [],
788827
'api_platform.exception_to_status' => [
789828
ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
@@ -977,9 +1016,12 @@ private function getPartialContainerBuilderProphecy()
9771016
return $containerBuilderProphecy;
9781017
}
9791018

980-
private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLoad = ['orm'])
1019+
private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLoad = ['orm'], $configuration = null)
9811020
{
982-
$containerBuilderProphecy = $this->getPartialContainerBuilderProphecy();
1021+
$hasSwagger = null === $configuration || true === $configuration['api_platform']['enable_swagger'] ?? false;
1022+
$hasHydra = null === $configuration || isset($configuration['api_platform']['formats']['jsonld']);
1023+
1024+
$containerBuilderProphecy = $this->getPartialContainerBuilderProphecy($configuration);
9831025

9841026
$containerBuilderProphecy->hasParameter('kernel.debug')->willReturn(true);
9851027
$containerBuilderProphecy->getParameter('kernel.debug')->willReturn(false);
@@ -1063,8 +1105,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
10631105
'api_platform.oauth.tokenUrl' => '/oauth/v2/token',
10641106
'api_platform.oauth.authorizationUrl' => '/oauth/v2/auth',
10651107
'api_platform.oauth.scopes' => [],
1066-
'api_platform.swagger.versions' => [2, 3],
1067-
'api_platform.swagger.api_keys' => [],
10681108
'api_platform.enable_swagger_ui' => true,
10691109
'api_platform.enable_re_doc' => true,
10701110
'api_platform.graphql.enabled' => true,
@@ -1078,6 +1118,13 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
10781118
'api_platform.elasticsearch.enabled' => false,
10791119
];
10801120

1121+
if ($hasSwagger) {
1122+
$parameters['api_platform.swagger.versions'] = [2, 3];
1123+
$parameters['api_platform.swagger.api_keys'] = [];
1124+
} else {
1125+
$parameters['api_platform.swagger.versions'] = [];
1126+
}
1127+
10811128
foreach ($parameters as $key => $value) {
10821129
$containerBuilderProphecy->setParameter($key, $value)->shouldBeCalled();
10831130
}
@@ -1153,20 +1200,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11531200
'api_platform.http_cache.listener.response.configure',
11541201
'api_platform.http_cache.purger.varnish_client',
11551202
'api_platform.http_cache.purger.varnish',
1156-
'api_platform.hydra.listener.response.add_link_header',
1157-
'api_platform.hydra.normalizer.collection',
1158-
'api_platform.hydra.normalizer.collection_filters',
1159-
'api_platform.hydra.normalizer.constraint_violation_list',
1160-
'api_platform.hydra.normalizer.documentation',
1161-
'api_platform.hydra.normalizer.entrypoint',
1162-
'api_platform.hydra.normalizer.error',
1163-
'api_platform.hydra.normalizer.partial_collection_view',
1164-
'api_platform.hydra.json_schema.schema_factory',
1165-
'api_platform.jsonld.action.context',
1166-
'api_platform.jsonld.context_builder',
1167-
'api_platform.jsonld.encoder',
1168-
'api_platform.jsonld.normalizer.item',
1169-
'api_platform.jsonld.normalizer.object',
11701203
'api_platform.listener.view.validate',
11711204
'api_platform.listener.view.validate_query_parameters',
11721205
'api_platform.mercure.listener.response.add_link_header',
@@ -1190,13 +1223,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
11901223
'api_platform.problem.normalizer.constraint_violation_list',
11911224
'api_platform.problem.normalizer.error',
11921225
'api_platform.swagger.action.ui',
1193-
'api_platform.swagger.command.swagger_command',
11941226
'api_platform.swagger.listener.ui',
1195-
'api_platform.swagger.normalizer.api_gateway',
1196-
'api_platform.swagger.normalizer.documentation',
1197-
'api_platform.json_schema.type_factory',
1198-
'api_platform.json_schema.schema_factory',
1199-
'api_platform.json_schema.json_schema_generate_command',
12001227
'api_platform.validator',
12011228
'test.api_platform.client',
12021229
];
@@ -1229,6 +1256,41 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
12291256
$definitions[] = 'api_platform.doctrine.metadata_factory';
12301257
}
12311258

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

12571317
if (\in_array('odm', $doctrineIntegrationsToLoad, true)) {
@@ -1269,6 +1329,14 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
12691329
];
12701330
}
12711331

1332+
// Only when swagger is enabled
1333+
if ($hasSwagger) {
1334+
$aliases += [
1335+
TypeFactoryInterface::class => 'api_platform.json_schema.type_factory',
1336+
SchemaFactoryInterface::class => 'api_platform.json_schema.schema_factory',
1337+
];
1338+
}
1339+
12721340
foreach ($aliases as $alias => $service) {
12731341
$containerBuilderProphecy->setAlias($alias, $service)->shouldBeCalled();
12741342
}
@@ -1283,4 +1351,21 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
12831351

12841352
return $containerBuilderProphecy;
12851353
}
1354+
1355+
private function getFormatsFromConfiguration(array $formatsConfiguration)
1356+
{
1357+
$formats = [];
1358+
1359+
foreach ($formatsConfiguration as $format => $mimeTypes) {
1360+
if (!isset($formats[$format])) {
1361+
$formats[$format] = [];
1362+
}
1363+
1364+
foreach ($mimeTypes['mime_types'] as $mimeType) {
1365+
$formats[$format][] = $mimeType;
1366+
}
1367+
}
1368+
1369+
return $formats;
1370+
}
12861371
}

0 commit comments

Comments
 (0)