fix(laravel): expose ReDoc/Scalar via UI switcher in docs view#7942
Open
abderrahimghazali wants to merge 1 commit intoapi-platform:mainfrom
Open
fix(laravel): expose ReDoc/Scalar via UI switcher in docs view#7942abderrahimghazali wants to merge 1 commit intoapi-platform:mainfrom
abderrahimghazali wants to merge 1 commit intoapi-platform:mainfrom
Conversation
The Laravel docs page only rendered Swagger UI even when ReDoc and Scalar were enabled, since the Blade template never surfaced the `?ui=redoc` / `?ui=scalar` switch present in the Symfony Twig template. Expose the enabled flags to the view, render a header nav switcher in parity with Symfony, and fix the `originalRoute` lookup which relied on the Symfony-only `_route` request attribute and was always null under Laravel. Closes api-platform#7682
b2a14a6 to
ebb78c2
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In the Laravel package, the
/api/docspage only ever rendered Swagger UI even whenredoc.enabledandscalar.enabledweretrueinconfig/api-platform.php. The Symfony bundle ships a UI switcher in its Twig template; the Laravel Blade template never surfaced one, so ReDoc and Scalar were effectively undiscoverable through the UI even though the wiring (config flags, processor params,?ui=query handling) was already in place.While digging in I also noticed that
originalRoute/originalRouteParamsinSwaggerUiProcessorwere alwaysnullunder Laravel: the lookup relied on the Symfony-only_routerequest attribute. Anyroute($originalRoute, …)call from the view would have failed withRoute [] not defined.Fix
SwaggerUiProcessor: read the active route name via Laravel's$request->route()?->getName()(with the_api_original_routeAPI Platform attribute still taking precedence), and passswaggerUiEnabled/redocEnabled/scalarEnabledinto the view so the template can decide what to render.swagger-ui.blade.php: render a header<nav class=\"api-platform-ui-switcher\">whenever more than one UI is enabled, mark the active link witharia-current=\"page\", and tag#swagger-uiwith the active UI as a class so the layout can compensate for the fixed 70px header in ReDoc/Scalar (Swagger UI already pads itself via.info).style.css: subtle segmented-control styling for the switcher;padding-top: 70pxon#swagger-ui.redoc/#swagger-ui.scalarso their content is not hidden behind the fixed header.Behavior is opt-in to multiple UIs: if only one UI is enabled the switcher is not rendered.
Tests
src/Laravel/Tests/DocsTest.phpcovers the default Swagger UI render plus?ui=redocand?ui=scalar, asserting the switcher markup, the active marker (aria-current=\"page\") and the right init script per UI. A separateDocsSingleUiTestoverrides the workbench config to enable only Swagger UI and asserts the switcher is not rendered.Screenshots
Tested locally against a fresh Laravel skeleton with
api-platform/laravelinstalled via path repository.Swagger UI (default)
ReDoc (
?ui=redoc)Scalar (
?ui=scalar)