From 8c69482aef9be970b29cd13e523399dcda1e78c9 Mon Sep 17 00:00:00 2001 From: Matyas Somfai Date: Thu, 15 Oct 2020 11:20:56 +0200 Subject: [PATCH] Add configurable asset_package Fix unit tests cs fix minor fixes --- .../Symfony/Bundle/Action/SwaggerUiAction.php | 5 ++++- .../ApiPlatformExtension.php | 1 + .../DependencyInjection/Configuration.php | 1 + .../Bundle/Resources/config/swagger-ui.xml | 2 ++ .../Resources/views/SwaggerUi/index.html.twig | 22 +++++++++---------- .../Bundle/SwaggerUi/SwaggerUiAction.php | 1 + .../Bundle/SwaggerUi/SwaggerUiContext.php | 9 +++++++- .../Bundle/Action/SwaggerUiActionTest.php | 1 + .../ApiPlatformExtensionTest.php | 2 ++ .../DependencyInjection/ConfigurationTest.php | 1 + .../Bundle/SwaggerUi/SwaggerUiActionTest.php | 1 + 11 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php b/src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php index 654736f229c..523369f3740 100644 --- a/src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php +++ b/src/Bridge/Symfony/Bundle/Action/SwaggerUiAction.php @@ -61,11 +61,12 @@ final class SwaggerUiAction private $graphQlPlaygroundEnabled; private $swaggerVersions; private $swaggerUiAction; + private $assetPackage; /** * @param int[] $swaggerVersions */ - 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) + 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) { $this->resourceNameCollectionFactory = $resourceNameCollectionFactory; $this->resourceMetadataFactory = $resourceMetadataFactory; @@ -91,6 +92,7 @@ public function __construct(ResourceNameCollectionFactoryInterface $resourceName $this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled; $this->swaggerVersions = $swaggerVersions; $this->swaggerUiAction = $swaggerUiAction; + $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); @@ -143,6 +145,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)]; diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php index b7bf18a94d1..e0a432999e1 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtension.php @@ -217,6 +217,7 @@ private function registerCommonConfiguration(ContainerBuilder $container, array if ($config['name_converter']) { $container->setAlias('api_platform.name_converter', $config['name_converter']); } + $container->setParameter('api_platform.asset_package', $config['asset_package']); $container->setParameter('api_platform.defaults', $this->normalizeDefaults($config['defaults'] ?? [])); } diff --git a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php index 51ca4471e2b..8ecbe699d96 100644 --- a/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php +++ b/src/Bridge/Symfony/Bundle/DependencyInjection/Configuration.php @@ -92,6 +92,7 @@ public function getConfigTreeBuilder() ->info('Specify the default operation path resolver to use for generating resources operations path.') ->end() ->scalarNode('name_converter')->defaultNull()->info('Specify a name converter to use.')->end() + ->scalarNode('asset_package')->defaultNull()->info('Specify an asset package name to use.')->end() ->scalarNode('path_segment_name_generator')->defaultValue('api_platform.path_segment_name_generator.underscore')->info('Specify a path name generator to use.')->end() ->booleanNode('allow_plain_identifiers')->defaultFalse()->info('Allow plain identifiers, for example "id" instead of "@id" when denormalizing a relation.')->end() ->arrayNode('validator') diff --git a/src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml b/src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml index 2b7b80bd8f1..c56d3a3440c 100644 --- a/src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml +++ b/src/Bridge/Symfony/Bundle/Resources/config/swagger-ui.xml @@ -37,6 +37,7 @@ %api_platform.graphql.graphql_playground.enabled% %api_platform.swagger.versions% + %api_platform.asset_package% @@ -46,6 +47,7 @@ %api_platform.graphql.enabled% %api_platform.graphql.graphiql.enabled% %api_platform.graphql.graphql_playground.enabled% + %api_platform.asset_package% diff --git a/src/Bridge/Symfony/Bundle/Resources/views/SwaggerUi/index.html.twig b/src/Bridge/Symfony/Bundle/Resources/views/SwaggerUi/index.html.twig index 88547ad5e0c..83fccc03799 100644 --- a/src/Bridge/Symfony/Bundle/Resources/views/SwaggerUi/index.html.twig +++ b/src/Bridge/Symfony/Bundle/Resources/views/SwaggerUi/index.html.twig @@ -6,11 +6,11 @@ {% block stylesheet %} - - + + {% endblock %} - {% set oauth_data = {'oauth': swagger_data.oauth|merge({'redirectUrl' : absolute_url(asset('bundles/apiplatform/swagger-ui/oauth2-redirect.html')) })} %} + {% set oauth_data = {'oauth': swagger_data.oauth|merge({'redirectUrl' : absolute_url(asset('bundles/apiplatform/swagger-ui/oauth2-redirect.html', assetPackage)) })} %} {# json_encode(65) is for JSON_UNESCAPED_SLASHES|JSON_HEX_TAG to avoid JS XSS #} @@ -50,12 +50,12 @@
- +
{% if showWebby %} -
-
+
+
{% endif %}
@@ -82,12 +82,12 @@ {% block javascript %} {% if (reDocEnabled and not swaggerUiEnabled) or (reDocEnabled and 're_doc' == active_ui) %} - - + + {% else %} - - - + + + {% endif %} {% endblock %} diff --git a/src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php b/src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php index ca41097141e..1767e56f814 100644 --- a/src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php +++ b/src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiAction.php @@ -70,6 +70,7 @@ public function __invoke(Request $request) 'graphqlEnabled' => $this->swaggerUiContext->isGraphQlEnabled(), 'graphiQlEnabled' => $this->swaggerUiContext->isGraphiQlEnabled(), 'graphQlPlaygroundEnabled' => $this->swaggerUiContext->isGraphQlPlaygroundEnabled(), + 'assetPackage' => $this->swaggerUiContext->getAssetPackage(), ]; $swaggerData = [ diff --git a/src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiContext.php b/src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiContext.php index 9586ad3287f..29aaf205600 100644 --- a/src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiContext.php +++ b/src/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiContext.php @@ -21,8 +21,9 @@ final class SwaggerUiContext private $graphQlEnabled; private $graphiQlEnabled; private $graphQlPlaygroundEnabled; + private $assetPackage; - public function __construct(bool $swaggerUiEnabled = false, bool $showWebby = true, bool $reDocEnabled = false, bool $graphQlEnabled = false, bool $graphiQlEnabled = false, bool $graphQlPlaygroundEnabled = false) + public function __construct(bool $swaggerUiEnabled = false, bool $showWebby = true, bool $reDocEnabled = false, bool $graphQlEnabled = false, bool $graphiQlEnabled = false, bool $graphQlPlaygroundEnabled = false, $assetPackage = null) { $this->swaggerUiEnabled = $swaggerUiEnabled; $this->showWebby = $showWebby; @@ -30,6 +31,7 @@ public function __construct(bool $swaggerUiEnabled = false, bool $showWebby = tr $this->graphQlEnabled = $graphQlEnabled; $this->graphiQlEnabled = $graphiQlEnabled; $this->graphQlPlaygroundEnabled = $graphQlPlaygroundEnabled; + $this->assetPackage = $assetPackage; } public function isSwaggerUiEnabled(): bool @@ -61,4 +63,9 @@ public function isGraphQlPlaygroundEnabled(): bool { return $this->graphQlPlaygroundEnabled; } + + public function getAssetPackage(): ?string + { + return $this->assetPackage; + } } diff --git a/tests/Bridge/Symfony/Bundle/Action/SwaggerUiActionTest.php b/tests/Bridge/Symfony/Bundle/Action/SwaggerUiActionTest.php index 73ad02d47c2..2a5cfb7b1ac 100644 --- a/tests/Bridge/Symfony/Bundle/Action/SwaggerUiActionTest.php +++ b/tests/Bridge/Symfony/Bundle/Action/SwaggerUiActionTest.php @@ -174,6 +174,7 @@ public function testDoNotRunCurrentRequest(Request $request) 'graphqlEnabled' => false, 'graphiQlEnabled' => false, 'graphQlPlaygroundEnabled' => false, + 'assetPackage' => null, 'swagger_data' => [ 'url' => '/url', 'spec' => self::SPEC, diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php index 13198e2f1ea..15fa0b65102 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ApiPlatformExtensionTest.php @@ -872,6 +872,7 @@ private function getPartialContainerBuilderProphecy($configuration = null) 'api_platform.http_cache.vary' => ['Accept'], 'api_platform.http_cache.public' => null, 'api_platform.http_cache.invalidation.max_header_length' => 7500, + 'api_platform.asset_package' => null, 'api_platform.defaults' => ['attributes' => ['stateless' => true]], 'api_platform.enable_entrypoint' => true, 'api_platform.enable_docs' => true, @@ -1176,6 +1177,7 @@ private function getBaseContainerBuilderProphecy(array $doctrineIntegrationsToLo 'api_platform.resource_class_directories' => Argument::type('array'), 'api_platform.validator.serialize_payload_fields' => [], 'api_platform.elasticsearch.enabled' => false, + 'api_platform.asset_package' => null, 'api_platform.defaults' => ['attributes' => ['stateless' => true]], ]; diff --git a/tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php b/tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php index 64ea917210b..fd4c045d233 100644 --- a/tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php +++ b/tests/Bridge/Symfony/Bundle/DependencyInjection/ConfigurationTest.php @@ -206,6 +206,7 @@ private function runDefaultConfigTests(array $doctrineIntegrationsToLoad = ['orm ], 'allow_plain_identifiers' => false, 'resource_class_directories' => [], + 'asset_package' => null, ], $config); } diff --git a/tests/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiActionTest.php b/tests/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiActionTest.php index 1a231b883c7..1990b008196 100644 --- a/tests/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiActionTest.php +++ b/tests/Bridge/Symfony/Bundle/SwaggerUi/SwaggerUiActionTest.php @@ -169,6 +169,7 @@ public function testDoNotRunCurrentRequest(Request $request) 'graphqlEnabled' => false, 'graphiQlEnabled' => false, 'graphQlPlaygroundEnabled' => false, + 'assetPackage' => null, 'swagger_data' => [ 'url' => '/url', 'spec' => self::SPEC,