diff --git a/core/configuration.md b/core/configuration.md index 5b2ff83a8db..720b255d841 100644 --- a/core/configuration.md +++ b/core/configuration.md @@ -555,23 +555,31 @@ return [ ], ], - 'swagger' => [ - // The active versions of OpenAPI to be exported or used in the swagger_ui. The first value is the default. - 'versions' => [2, 3], + 'swagger_ui' => [ + // Enable or disable Swagger UI. + 'enabled' => true, + + // The API keys Swagger UI can send, indexed by the name of the security scheme. + 'apiKeys' => [ + 'api' => [ + // The name of the header or query parameter containing the API key. + 'name' => 'Authorization', + + // Whether the API key should be a query parameter or a header. + 'type' => 'header', + ], + ], - // The swagger API keys. - 'api_keys' => [], + // If set to true, it persists authorization data and it would not be lost on browser close/refresh + 'persist_authorization' => true, - 'swagger_ui_extra_configuration' => [ + 'extra_configuration' => [ // Controls the default expansion setting for the operations and tags. It can be 'list' (expands only the tags), 'full' (expands the tags and operations) or 'none' (expands nothing). 'docExpansion' => 'list', // If set, enables filtering. The top bar will show an edit box that you can use to filter the tagged operations that are shown. 'filter' => false, - // If set to true, it persists authorization data and it would not be lost on browser close/refresh - 'persistAuthorization' => true, - // You can use any other configuration parameters too. ], ], diff --git a/core/openapi.md b/core/openapi.md index afe09488d8d..d2dd3727025 100644 --- a/core/openapi.md +++ b/core/openapi.md @@ -966,6 +966,76 @@ You may want to copy the [one shipped with API Platform](https://github.com/api-platform/core/blob/main/src/Laravel/resources/views/swagger-ui.blade.php) and customize it. +## Registering Swagger UI Plugins + +[Swagger UI plugins](https://swagger.io/docs/open-source-tools/swagger-ui/customization/plugin-api/) +are JavaScript functions that must be passed to Swagger UI when it is created, so they cannot be set +with the `swagger_ui_extra_configuration` option. Instead, API Platform passes every plugin found in +the `window.apiPlatformSwaggerUiPlugins` array, which means you don't have to override the script +that initializes Swagger UI. + +Push your plugin to this array from your own script: + +```javascript +// public/js/swagger-ui-plugin.js +(window.apiPlatformSwaggerUiPlugins ||= []).push(function NoticePlugin() { + return { + wrapComponents: { + // Renders a notice above the title and description of the API + InfoContainer: + (Original, { React }) => + (props) => + React.createElement( + "div", + null, + React.createElement("p", null, "This is the staging environment."), + React.createElement(Original, props), + ), + }, + }; +}); +``` + +Then load this script in the documentation page. It only has to run before the `load` event of the +page, so it can be added after the scripts of API Platform. + +For a complete example, see +[swagger-ui-login-plugin](https://github.com/ViPErCZ/swagger-ui-login-plugin), a community plugin +that logs in from Swagger UI and authorizes with the returned token. + +### Registering Swagger UI Plugins using Symfony + +Extend the Twig template of API Platform and add your script to the `javascript` block (use the +`stylesheet` block for stylesheets): + +```twig +{# templates/bundles/ApiPlatformBundle/SwaggerUi/index.html.twig #} +{% extends '@!ApiPlatform/SwaggerUi/index.html.twig' %} + +{% block javascript %} + {{ parent() }} + +{% endblock %} +``` + +### Registering Swagger UI Plugins using Laravel + +The documentation page includes two optional views. Create them in your application to add your own +scripts and stylesheets, there is no need to override the whole Blade template: + +```html +{{-- resources/views/vendor/api-platform/swagger-ui-javascript.blade.php --}} + +``` + +```html +{{-- resources/views/vendor/api-platform/swagger-ui-stylesheet.blade.php --}} + +``` + +Existing applications must re-publish the assets to get the `apiPlatformSwaggerUiPlugins` hook: +`php artisan vendor:publish --tag=api-platform-assets --force`. + ## Compatibility Layer with Amazon API Gateway [AWS API Gateway](https://aws.amazon.com/api-gateway/) supports OpenAPI partially, but it