Skip to content

DI: Improve swagger versions parameter (fix #3115) #3167

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 1 commit into from
Oct 23, 2019
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -312,6 +312,8 @@ private function registerOAuthConfiguration(ContainerBuilder $container, array $
*/
private function registerSwaggerConfiguration(ContainerBuilder $container, array $config, XmlFileLoader $loader): void
{
$container->setParameter('api_platform.swagger.versions', $config['swagger']['versions']);

if (empty($config['swagger']['versions'])) {
return;
}
Expand All @@ -325,7 +327,6 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
$container->setParameter('api_platform.enable_re_doc', $config['enable_re_doc']);
}

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

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

if (!$container->has('api_platform.json_schema.schema_factory')) {
$container->removeDefinition('api_platform.hydra.json_schema.schema_factory');
Copy link
Contributor

@teohhanhui teohhanhui Oct 22, 2019

Choose a reason for hiding this comment

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

You cannot remove a definition when it doesn't exist yet (it's defined in hydra.xml loaded few lines below)...

$loader->load('hydra.xml');

}

if (!$docEnabled) {
$container->removeDefinition('api_platform.hydra.listener.response.add_link_header');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -757,7 +757,46 @@ public function testKeepCachePoolClearerCacheWarmerWithDebug()
$this->extension->load(self::DEFAULT_CONFIG, $containerBuilder);
}

private function getPartialContainerBuilderProphecy()
public function testDisabledSwaggerAndHydra()
{
$config = self::DEFAULT_CONFIG;
$config['api_platform']['enable_swagger'] = false;
$config['api_platform']['enable_swagger_ui'] = false;
$config['api_platform']['formats'] = [
'jsonhal' => ['mime_types' => ['application/hal+json']],
];

$containerBuilderProphecy = $this->getBaseContainerBuilderProphecy(['orm'], $config);
$containerBuilderProphecy->setDefinition('api_platform.swagger.action.ui', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.swagger.listener.ui', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', true)->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', true)->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', false)->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_re_doc', true)->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_re_doc', false)->shouldNotBeCalled();
$containerBuilder = $containerBuilderProphecy->reveal();
$this->extension->load($config, $containerBuilder);
}

public function testDisabledSwagger()
{
$config = self::DEFAULT_CONFIG;
$config['api_platform']['enable_swagger'] = false;
$config['api_platform']['enable_swagger_ui'] = false;

$containerBuilderProphecy = $this->getBaseContainerBuilderProphecy(['orm'], $config);
$containerBuilderProphecy->setDefinition('api_platform.swagger.action.ui', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setDefinition('api_platform.swagger.listener.ui', Argument::type(Definition::class))->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', true)->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', true)->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_swagger_ui', false)->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_re_doc', true)->shouldNotBeCalled();
$containerBuilderProphecy->setParameter('api_platform.enable_re_doc', false)->shouldNotBeCalled();
$containerBuilder = $containerBuilderProphecy->reveal();
$this->extension->load($config, $containerBuilder);
}

private function getPartialContainerBuilderProphecy($configuration = null)
{
$parameterBag = new EnvPlaceholderParameterBag();

Expand All @@ -783,7 +822,7 @@ private function getPartialContainerBuilderProphecy()
'api_platform.collection.order_parameter_name' => 'order',
'api_platform.description' => 'description',
'api_platform.error_formats' => ['jsonproblem' => ['application/problem+json'], 'jsonld' => ['application/ld+json']],
'api_platform.formats' => ['jsonld' => ['application/ld+json'], 'jsonhal' => ['application/hal+json']],
'api_platform.formats' => null === $configuration ? ['jsonld' => ['application/ld+json'], 'jsonhal' => ['application/hal+json']] : $this->getFormatsFromConfiguration($configuration['api_platform']['formats']) ?? [],
'api_platform.patch_formats' => [],
'api_platform.exception_to_status' => [
ExceptionInterface::class => Response::HTTP_BAD_REQUEST,
Expand Down Expand Up @@ -977,9 +1016,12 @@ private function getPartialContainerBuilderProphecy()
return $containerBuilderProphecy;
}

private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLoad = ['orm'])
private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLoad = ['orm'], $configuration = null)
{
$containerBuilderProphecy = $this->getPartialContainerBuilderProphecy();
$hasSwagger = null === $configuration || true === $configuration['api_platform']['enable_swagger'] ?? false;
$hasHydra = null === $configuration || isset($configuration['api_platform']['formats']['jsonld']);

$containerBuilderProphecy = $this->getPartialContainerBuilderProphecy($configuration);

$containerBuilderProphecy->hasParameter('kernel.debug')->willReturn(true);
$containerBuilderProphecy->getParameter('kernel.debug')->willReturn(false);
Expand Down Expand Up @@ -1063,8 +1105,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
'api_platform.oauth.tokenUrl' => '/oauth/v2/token',
'api_platform.oauth.authorizationUrl' => '/oauth/v2/auth',
'api_platform.oauth.scopes' => [],
'api_platform.swagger.versions' => [2, 3],
'api_platform.swagger.api_keys' => [],
'api_platform.enable_swagger_ui' => true,
'api_platform.enable_re_doc' => true,
'api_platform.graphql.enabled' => true,
Expand All @@ -1078,6 +1118,13 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
'api_platform.elasticsearch.enabled' => false,
];

if ($hasSwagger) {
$parameters['api_platform.swagger.versions'] = [2, 3];
$parameters['api_platform.swagger.api_keys'] = [];
} else {
$parameters['api_platform.swagger.versions'] = [];
}

foreach ($parameters as $key => $value) {
$containerBuilderProphecy->setParameter($key, $value)->shouldBeCalled();
}
Expand Down Expand Up @@ -1153,20 +1200,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
'api_platform.http_cache.listener.response.configure',
'api_platform.http_cache.purger.varnish_client',
'api_platform.http_cache.purger.varnish',
'api_platform.hydra.listener.response.add_link_header',
'api_platform.hydra.normalizer.collection',
'api_platform.hydra.normalizer.collection_filters',
'api_platform.hydra.normalizer.constraint_violation_list',
'api_platform.hydra.normalizer.documentation',
'api_platform.hydra.normalizer.entrypoint',
'api_platform.hydra.normalizer.error',
'api_platform.hydra.normalizer.partial_collection_view',
'api_platform.hydra.json_schema.schema_factory',
'api_platform.jsonld.action.context',
'api_platform.jsonld.context_builder',
'api_platform.jsonld.encoder',
'api_platform.jsonld.normalizer.item',
'api_platform.jsonld.normalizer.object',
'api_platform.listener.view.validate',
'api_platform.listener.view.validate_query_parameters',
'api_platform.mercure.listener.response.add_link_header',
Expand All @@ -1190,13 +1223,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
'api_platform.problem.normalizer.constraint_violation_list',
'api_platform.problem.normalizer.error',
'api_platform.swagger.action.ui',
'api_platform.swagger.command.swagger_command',
'api_platform.swagger.listener.ui',
'api_platform.swagger.normalizer.api_gateway',
'api_platform.swagger.normalizer.documentation',
'api_platform.json_schema.type_factory',
'api_platform.json_schema.schema_factory',
'api_platform.json_schema.json_schema_generate_command',
'api_platform.validator',
'test.api_platform.client',
];
Expand Down Expand Up @@ -1229,6 +1256,41 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
$definitions[] = 'api_platform.doctrine.metadata_factory';
}

// Only when swagger is enabled
if ($hasSwagger) {
$definitions[] = 'api_platform.swagger.command.swagger_command';
$definitions[] = 'api_platform.swagger.normalizer.api_gateway';
$definitions[] = 'api_platform.swagger.normalizer.documentation';
$definitions[] = 'api_platform.json_schema.type_factory';
$definitions[] = 'api_platform.json_schema.schema_factory';
$definitions[] = 'api_platform.json_schema.json_schema_generate_command';
}

// has jsonld
if ($hasHydra) {
$containerBuilderProphecy->has('api_platform.json_schema.schema_factory')->shouldBeCalled()->willReturn($hasSwagger);

if (!$hasSwagger) {
$containerBuilderProphecy->has('api_platform.json_schema.schema_factory')->shouldBeCalled()->willReturn(false);
$containerBuilderProphecy->removeDefinition('api_platform.hydra.json_schema.schema_factory')->shouldBeCalled();
}

$definitions[] = 'api_platform.hydra.json_schema.schema_factory';
$definitions[] = 'api_platform.hydra.listener.response.add_link_header';
$definitions[] = 'api_platform.hydra.normalizer.collection';
$definitions[] = 'api_platform.hydra.normalizer.collection_filters';
$definitions[] = 'api_platform.hydra.normalizer.constraint_violation_list';
$definitions[] = 'api_platform.hydra.normalizer.documentation';
$definitions[] = 'api_platform.hydra.normalizer.entrypoint';
$definitions[] = 'api_platform.hydra.normalizer.error';
$definitions[] = 'api_platform.hydra.normalizer.partial_collection_view';
$definitions[] = 'api_platform.jsonld.action.context';
$definitions[] = 'api_platform.jsonld.context_builder';
$definitions[] = 'api_platform.jsonld.encoder';
$definitions[] = 'api_platform.jsonld.normalizer.item';
$definitions[] = 'api_platform.jsonld.normalizer.object';
}

foreach ($definitions as $definition) {
$containerBuilderProphecy->setDefinition($definition, Argument::type(Definition::class))->shouldBeCalled();
}
Expand All @@ -1250,8 +1312,6 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
NumericFilter::class => 'api_platform.doctrine.orm.numeric_filter',
ExistsFilter::class => 'api_platform.doctrine.orm.exists_filter',
GraphQlSerializerContextBuilderInterface::class => 'api_platform.graphql.serializer.context_builder',
TypeFactoryInterface::class => 'api_platform.json_schema.type_factory',
SchemaFactoryInterface::class => 'api_platform.json_schema.schema_factory',
];

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

// Only when swagger is enabled
if ($hasSwagger) {
$aliases += [
TypeFactoryInterface::class => 'api_platform.json_schema.type_factory',
SchemaFactoryInterface::class => 'api_platform.json_schema.schema_factory',
];
}

foreach ($aliases as $alias => $service) {
$containerBuilderProphecy->setAlias($alias, $service)->shouldBeCalled();
}
Expand All @@ -1283,4 +1351,21 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo

return $containerBuilderProphecy;
}

private function getFormatsFromConfiguration(array $formatsConfiguration)
{
$formats = [];

foreach ($formatsConfiguration as $format => $mimeTypes) {
if (!isset($formats[$format])) {
$formats[$format] = [];
}

foreach ($mimeTypes['mime_types'] as $mimeType) {
$formats[$format][] = $mimeType;
}
}

return $formats;
}
}