Skip to content

feat(symfony,laravel): hook to register Swagger UI plugins - #8891

Merged
soyuka merged 4 commits into
api-platform:mainfrom
ViPErCZ:feat/swagger-ui-plugins-hook
Dec 3, 2026
Merged

soyuka merged 4 commits into
api-platform:mainfrom
ViPErCZ:feat/swagger-ui-plugins-hook

Conversation

@ViPErCZ

@ViPErCZ ViPErCZ commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor
Q A
Branch? main
Tickets Follow-up of #8538, see also #8890
New feature? yes
Deprecations? no
License MIT

Second part of the split of #8538: a login-agnostic extension point for Swagger UI plugins.

Swagger UI plugins are functions and have to be passed when the UI is constructed, so they cannot go through swagger_ui_extra_configuration (JSON). Until now the only way to add one was to copy and override the whole init-swagger-ui.js.

Both init scripts (Symfony and Laravel) now append whatever is in window.apiPlatformSwaggerUiPlugins to the plugins list:

// my-plugin.js, loaded by the documentation page
(window.apiPlatformSwaggerUiPlugins ||= []).push(function MyPlugin(system) {
    return {
        wrapComponents: { /* ... */ },
    };
});

The script only has to run before the load event.

  • Symfony: override the javascript block of @ApiPlatform/SwaggerUi/index.html.twig.
  • Laravel: the swagger-ui view had no extension point (and the package does not publish its views), so this PR also adds two optional views included by the documentation page. Create
    them in your application to add your own tags:
  {{-- resources/views/vendor/api-platform/swagger-ui-javascript.blade.php --}}
  <script src="/js/my-plugin.js"></script>

swagger-ui-stylesheet works the same way for the <head>. Nothing changes when the views do not exist. I named them after the stylesheet and javascript blocks of the Symfony template; happy to switch to another approach (e.g. a configuration option) if you prefer.

I will send a documentation PR once this is merged.

Swagger UI plugins are functions that must be passed when the UI is
constructed, so they cannot go through the extra configuration, and the
only way to add one was to override the whole init-swagger-ui.js.

init-swagger-ui.js now appends the content of
window.apiPlatformSwaggerUiPlugins to the plugins list, so any script
loaded by the documentation page can register its own plugin.
The swagger-ui view has no extension point, so registering a Swagger UI
plugin required copying the whole view. It now includes two optional
views, "api-platform::swagger-ui-stylesheet" and
"api-platform::swagger-ui-javascript", that an application can create
in resources/views/vendor/api-platform.
@soyuka
soyuka merged commit d4afa58 into api-platform:main Dec 3, 2026
90 of 116 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants