Skip to content
This repository has been archived by the owner on Nov 29, 2022. It is now read-only.

Commit

Permalink
Add swagger_ui_extra_configuration to Swagger configuration (api-pl…
Browse files Browse the repository at this point in the history
…atform#3731)

(cherry picked from commit 930c930)
  • Loading branch information
camohob committed Mar 25, 2021
1 parent e44cd9e commit a119a37
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 5 deletions.
26 changes: 23 additions & 3 deletions src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace ApiPlatform\Core\Bridge\Symfony\Bundle\Action;

use ApiPlatform\Core\Api\FormatsProviderInterface;
use ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction as OpenApiSwaggerUiAction;
use ApiPlatform\Core\Documentation\Documentation;
use ApiPlatform\Core\Exception\RuntimeException;
use ApiPlatform\Core\Metadata\Resource\Factory\ResourceMetadataFactoryInterface;
Expand All @@ -28,6 +29,8 @@
/**
* Displays the documentation.
*
* @deprecated please refer to ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction for further changes
*
* @author Kévin Dunglas <[email protected]>
*/
final class SwaggerUiAction
Expand Down Expand Up @@ -57,11 +60,15 @@ final class SwaggerUiAction
private $graphiQlEnabled;
private $graphQlPlaygroundEnabled;
private $swaggerVersions;
private $swaggerUiAction;
private $assetPackage;
private $swaggerUiExtraConfiguration;

/**
* @param int[] $swaggerVersions
* @param int[] $swaggerVersions
* @param mixed|null $assetPackage
*/
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, NormalizerInterface $normalizer, TwigEnvironment $twig, UrlGeneratorInterface $urlGenerator, string $title = '', string $description = '', string $version = '', $formats = [], $oauthEnabled = false, $oauthClientId = '', $oauthClientSecret = '', $oauthType = '', $oauthFlow = '', $oauthTokenUrl = '', $oauthAuthorizationUrl = '', $oauthScopes = [], bool $showWebby = true, bool $swaggerUiEnabled = false, bool $reDocEnabled = false, bool $graphqlEnabled = false, bool $graphiQlEnabled = false, bool $graphQlPlaygroundEnabled = false, array $swaggerVersions = [2, 3])
public function __construct(ResourceNameCollectionFactoryInterface $resourceNameCollectionFactory, ResourceMetadataFactoryInterface $resourceMetadataFactory, NormalizerInterface $normalizer, TwigEnvironment $twig, UrlGeneratorInterface $urlGenerator, string $title = '', string $description = '', string $version = '', $formats = [], $oauthEnabled = false, $oauthClientId = '', $oauthClientSecret = '', $oauthType = '', $oauthFlow = '', $oauthTokenUrl = '', $oauthAuthorizationUrl = '', $oauthScopes = [], bool $showWebby = true, bool $swaggerUiEnabled = false, bool $reDocEnabled = false, bool $graphqlEnabled = false, bool $graphiQlEnabled = false, bool $graphQlPlaygroundEnabled = false, array $swaggerVersions = [2, 3], OpenApiSwaggerUiAction $swaggerUiAction = null, $assetPackage = null, array $swaggerUiExtraConfiguration = [])
{
$this->resourceNameCollectionFactory = $resourceNameCollectionFactory;
$this->resourceMetadataFactory = $resourceMetadataFactory;
Expand All @@ -86,19 +93,30 @@ public function __construct(ResourceNameCollectionFactoryInterface $resourceName
$this->graphiQlEnabled = $graphiQlEnabled;
$this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled;
$this->swaggerVersions = $swaggerVersions;
$this->swaggerUiAction = $swaggerUiAction;
$this->swaggerUiExtraConfiguration = $swaggerUiExtraConfiguration;
$this->assetPackage = $assetPackage;

if (null === $this->swaggerUiAction) {
@trigger_error(sprintf('The use of "%s" is deprecated since API Platform 2.6, use "%s" instead.', __CLASS__, OpenApiSwaggerUiAction::class), \E_USER_DEPRECATED);
}

if (\is_array($formats)) {
$this->formats = $formats;

return;
}

@trigger_error(sprintf('Passing an array or an instance of "%s" as 5th parameter of the constructor of "%s" is deprecated since API Platform 2.5, pass an array instead', FormatsProviderInterface::class, __CLASS__), E_USER_DEPRECATED);
@trigger_error(sprintf('Passing an array or an instance of "%s" as 5th parameter of the constructor of "%s" is deprecated since API Platform 2.5, pass an array instead', FormatsProviderInterface::class, __CLASS__), \E_USER_DEPRECATED);
$this->formatsProvider = $formats;
}

public function __invoke(Request $request)
{
if ($this->swaggerUiAction) {
return $this->swaggerUiAction->__invoke($request);
}

$attributes = RequestAttributesExtractor::extractAttributes($request);

// BC check to be removed in 3.0
Expand Down Expand Up @@ -130,6 +148,7 @@ private function getContext(Request $request, Documentation $documentation): arr
'graphqlEnabled' => $this->graphqlEnabled,
'graphiQlEnabled' => $this->graphiQlEnabled,
'graphQlPlaygroundEnabled' => $this->graphQlPlaygroundEnabled,
'assetPackage' => $this->assetPackage,
];

$swaggerContext = ['spec_version' => $request->query->getInt('spec_version', $this->swaggerVersions[0] ?? 2)];
Expand All @@ -140,6 +159,7 @@ private function getContext(Request $request, Documentation $documentation): arr
$swaggerData = [
'url' => $this->urlGenerator->generate('api_doc', ['format' => 'json']),
'spec' => $this->normalizer->normalize($documentation, 'json', $swaggerContext),
'extraConfiguration' => $this->swaggerUiExtraConfiguration,
];

$swaggerData['oauth'] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public function load(array $configs, ContainerBuilder $container): void
$this->registerCommonConfiguration($container, $config, $loader, $formats, $patchFormats, $errorFormats);
$this->registerMetadataConfiguration($container, $config, $loader);
$this->registerOAuthConfiguration($container, $config);
$this->registerOpenApiConfiguration($container, $config);
$this->registerSwaggerConfiguration($container, $config, $loader);
$this->registerJsonApiConfiguration($formats, $loader);
$this->registerJsonLdHydraConfiguration($container, $formats, $loader, $config['enable_docs']);
Expand Down Expand Up @@ -344,6 +345,11 @@ private function registerSwaggerConfiguration(ContainerBuilder $container, array
$container->setParameter('api_platform.enable_swagger_ui', $config['enable_swagger_ui']);
$container->setParameter('api_platform.enable_re_doc', $config['enable_re_doc']);
$container->setParameter('api_platform.swagger.api_keys', $config['swagger']['api_keys']);
$container->setParameter('api_platform.swagger_ui.extra_configuration', $config['openapi']['swagger_ui_extra_configuration'] ?? $config['swagger']['swagger_ui_extra_configuration']);

if (true === $config['openapi']['backward_compatibility_layer']) {
$container->getDefinition('api_platform.swagger.normalizer.documentation')->addArgument($container->getDefinition('api_platform.openapi.normalizer'));
}
}

private function registerJsonApiConfiguration(array $formats, XmlFileLoader $loader): void
Expand Down Expand Up @@ -629,6 +635,16 @@ private function registerSecurityConfiguration(ContainerBuilder $container, XmlF
}
}

private function registerOpenApiConfiguration(ContainerBuilder $container, array $config): void
{
$container->setParameter('api_platform.openapi.termsOfService', $config['openapi']['termsOfService']);
$container->setParameter('api_platform.openapi.contact.name', $config['openapi']['contact']['name']);
$container->setParameter('api_platform.openapi.contact.url', $config['openapi']['contact']['url']);
$container->setParameter('api_platform.openapi.contact.email', $config['openapi']['contact']['email']);
$container->setParameter('api_platform.openapi.license.name', $config['openapi']['license']['name']);
$container->setParameter('api_platform.openapi.license.url', $config['openapi']['license']['url']);
}

private function buildDeprecationArgs(string $version, string $message): array
{
return method_exists(Definition::class, 'getDeprecation')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,6 @@ public function getConfigTreeBuilder()
'jsonld' => ['mime_types' => ['application/ld+json']],
]);

$this->addDefaultsSection($rootNode);

return $treeBuilder;
}

Expand Down
27 changes: 27 additions & 0 deletions src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,33 @@
<argument>%api_platform.graphql.graphiql.enabled%</argument>
<argument>%api_platform.graphql.graphql_playground.enabled%</argument>
<argument>%api_platform.swagger.versions%</argument>
<argument type="service" id="api_platform.swagger_ui.action" />
<argument>%api_platform.asset_package%</argument>
<argument>%api_platform.swagger_ui.extra_configuration%</argument>
</service>

<service id="api_platform.swagger_ui.context" class="ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiContext">
<argument>%api_platform.enable_swagger_ui%</argument>
<argument>%api_platform.show_webby%</argument>
<argument>%api_platform.enable_re_doc%</argument>
<argument>%api_platform.graphql.enabled%</argument>
<argument>%api_platform.graphql.graphiql.enabled%</argument>
<argument>%api_platform.graphql.graphql_playground.enabled%</argument>
<argument>%api_platform.asset_package%</argument>
<argument>%api_platform.swagger_ui.extra_configuration%</argument>
</service>

<service id="api_platform.swagger_ui.action" class="ApiPlatform\Core\Bridge\Symfony\Bundle\SwaggerUi\SwaggerUiAction" public="true">
<argument type="service" id="api_platform.metadata.resource.metadata_factory" />
<argument type="service" id="twig" />
<argument type="service" id="router" />
<argument type="service" id="api_platform.serializer" />
<argument type="service" id="api_platform.openapi.factory" />
<argument type="service" id="api_platform.openapi.options" />
<argument type="service" id="api_platform.swagger_ui.context" />
<argument>%api_platform.formats%</argument>
<argument>%api_platform.oauth.clientId%</argument>
<argument>%api_platform.oauth.clientSecret%</argument>
</service>

</services>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,7 @@ public function testSetNameConverter()
$containerBuilderProphecy->hasParameter('kernel.debug')->willReturn(true);
$containerBuilderProphecy->getParameter('kernel.debug')->willReturn(false);
$containerBuilderProphecy->setAlias('api_platform.name_converter', $nameConverterId)->shouldBeCalled();
$containerBuilderProphecy->setParameter('api_platform.swagger_ui.extra_configuration', [])->shouldBeCalled();

$containerBuilder = $containerBuilderProphecy->reveal();

Expand Down Expand Up @@ -1090,6 +1091,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo
if ($hasSwagger) {
$parameters['api_platform.swagger.versions'] = [2, 3];
$parameters['api_platform.swagger.api_keys'] = [];
$parameters['api_platform.swagger_ui.extra_configuration'] = [];
} else {
$parameters['api_platform.swagger.versions'] = [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
'swagger' => [
'versions' => [2, 3],
'api_keys' => [],
'swagger_ui_extra_configuration' => [],
],
'eager_loading' => [
'enabled' => true,
Expand Down Expand Up @@ -204,6 +205,21 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm
],
'allow_plain_identifiers' => false,
'resource_class_directories' => [],
'asset_package' => null,
'openapi' => [
'contact' => [
'name' => null,
'url' => null,
'email' => null,
],
'termsOfService' => null,
'license' => [
'name' => null,
'url' => null,
],
'backward_compatibility_layer' => true,
'swagger_ui_extra_configuration' => [],
],
], $config);
}

Expand Down

0 comments on commit a119a37

Please sign in to comment.