Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions src/Laravel/State/SwaggerUiProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable
'formats' => $this->formats,
'title' => $openApi->getInfo()->getTitle(),
'description' => $openApi->getInfo()->getDescription(),
'originalRoute' => $request->attributes->get('_api_original_route', $request->attributes->get('_route')),
'originalRouteParams' => $request->attributes->get('_api_original_route_params', $request->attributes->get('_route_params', [])),
'originalRoute' => $request->attributes->get('_api_original_route') ?? $request->route()?->getName(),
'originalRouteParams' => $request->attributes->get('_api_original_route_params') ?? $request->route()?->parameters() ?? [],
];

$swaggerData = [
Expand Down Expand Up @@ -99,7 +99,13 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable

$swaggerData['scalarExtraConfiguration'] = $this->scalarExtraConfiguration;

return new Response(view('api-platform::swagger-ui', $swaggerContext + ['swagger_data' => $swaggerData, 'ui' => $this->getUi()]), 200);
return new Response(view('api-platform::swagger-ui', $swaggerContext + [
'swagger_data' => $swaggerData,
'ui' => $this->getUi(),
'swaggerUiEnabled' => $this->swaggerEnabled,
'redocEnabled' => $this->redocEnabled,
'scalarEnabled' => $this->scalarEnabled,
]), 200);
}

/**
Expand Down
44 changes: 44 additions & 0 deletions src/Laravel/Tests/DocsSingleUiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Laravel\Tests;

use ApiPlatform\Laravel\Test\ApiTestAssertionsTrait;
use Illuminate\Config\Repository;
use Orchestra\Testbench\Concerns\WithWorkbench;
use Orchestra\Testbench\TestCase;

class DocsSingleUiTest extends TestCase
{
use ApiTestAssertionsTrait;
use WithWorkbench;

protected function defineEnvironment($app): void
{
tap($app['config'], static function (Repository $config): void {
$config->set('api-platform.swagger_ui.enabled', true);
$config->set('api-platform.redoc.enabled', false);
$config->set('api-platform.scalar.enabled', false);
});
}

public function testHtmlDocsHasNoUiSwitcherWhenOnlyOneUiEnabled(): void
{
$res = $this->get('/api/docs', headers: ['accept' => 'text/html']);
$res->assertOk();
$content = $res->getContent();

$this->assertStringContainsString('init-swagger-ui.js', $content);
$this->assertStringNotContainsString('class="api-platform-ui-switcher"', $content);
}
}
37 changes: 37 additions & 0 deletions src/Laravel/Tests/DocsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,41 @@ public function testJsonLdAccept(): void
$this->assertArrayHasKey('@context', $res->json());
$this->assertSame('application/ld+json; charset=utf-8', $res->headers->get('content-type'));
}

public function testHtmlDocsRendersSwaggerByDefaultWithUiSwitcher(): void
{
$res = $this->get('/api/docs', headers: ['accept' => 'text/html']);
$res->assertOk();
$content = $res->getContent();

$this->assertStringContainsString('class="api-platform-ui-switcher"', $content);
$this->assertStringContainsString('init-swagger-ui.js', $content);
$this->assertStringContainsString('ui=swagger', $content);
$this->assertStringContainsString('ui=redoc', $content);
$this->assertStringContainsString('ui=scalar', $content);
$this->assertMatchesRegularExpression('#<a[^>]*ui=swagger[^>]*aria-current="page"#', $content);
}

public function testHtmlDocsRendersRedocWhenRequested(): void
{
$res = $this->get('/api/docs?ui=redoc', headers: ['accept' => 'text/html']);
$res->assertOk();
$content = $res->getContent();

$this->assertStringContainsString('redoc.standalone.js', $content);
$this->assertStringContainsString('init-redoc-ui.js', $content);
$this->assertStringContainsString('class="api-platform-ui-switcher"', $content);
$this->assertMatchesRegularExpression('#<a[^>]*ui=redoc[^>]*aria-current="page"#', $content);
}

public function testHtmlDocsRendersScalarWhenRequested(): void
{
$res = $this->get('/api/docs?ui=scalar', headers: ['accept' => 'text/html']);
$res->assertOk();
$content = $res->getContent();

$this->assertStringContainsString('init-scalar-ui.js', $content);
$this->assertStringContainsString('class="api-platform-ui-switcher"', $content);
$this->assertMatchesRegularExpression('#<a[^>]*ui=scalar[^>]*aria-current="page"#', $content);
}
}
43 changes: 43 additions & 0 deletions src/Laravel/public/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ html {
box-sizing: border-box;
overflow: -moz-scrollbars-vertical;
overflow-y: scroll;
scroll-padding-top: 70px;
}

*,
Expand Down Expand Up @@ -41,6 +42,48 @@ header #logo img {
background-color: rgba(40, 134, 144, .4)
}

header .api-platform-ui-switcher {
position: fixed;
top: 35px;
left: 50%;
z-index: 102;
transform: translate(-50%, -50%);
display: inline-flex;
padding: 4px;
gap: 2px;
background: rgba(0, 0, 0, .18);
border-radius: 6px;
font-family: 'Open Sans', sans-serif;
font-size: 13px;
}

header .api-platform-ui-switcher a {
color: rgba(255, 255, 255, .8);
text-decoration: none;
padding: 5px 14px;
border-radius: 4px;
font-weight: 500;
line-height: 1.4;
transition: background-color .15s ease, color .15s ease;
}

header .api-platform-ui-switcher a:hover {
color: #fff;
background: rgba(255, 255, 255, .12);
}

header .api-platform-ui-switcher a[aria-current="page"] {
color: #288690;
background: #fff;
cursor: default;
pointer-events: none;
}

#swagger-ui.api-platform.redoc,
#swagger-ui.api-platform.scalar {
padding-top: 70px;
}

.svg-icons {
position:absolute;width:0;height:0
}
Expand Down
15 changes: 14 additions & 1 deletion src/Laravel/resources/views/swagger-ui.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,19 @@
<body>
<header>
<a id="logo" href="https://api-platform.com/"><img src="/vendor/api-platform/logo-header.svg" alt="API Platform"></a>
@if ((int) $swaggerUiEnabled + (int) $redocEnabled + (int) $scalarEnabled > 1)
<nav class="api-platform-ui-switcher" aria-label="Documentation UI">
@if ($swaggerUiEnabled)
<a href="{{ route($originalRoute, array_merge($originalRouteParams, ['ui' => 'swagger'])) }}"@if ($ui === 'swagger') aria-current="page"@endif>Swagger UI</a>
@endif
@if ($redocEnabled)
<a href="{{ route($originalRoute, array_merge($originalRouteParams, ['ui' => 'redoc'])) }}"@if ($ui === 'redoc') aria-current="page"@endif>ReDoc</a>
@endif
@if ($scalarEnabled)
<a href="{{ route($originalRoute, array_merge($originalRouteParams, ['ui' => 'scalar'])) }}"@if ($ui === 'scalar') aria-current="page"@endif>Scalar</a>
@endif
</nav>
@endif
</header>
@if (config('api-platform.show_webby', true))
<div class="web calm"><img src="/vendor/api-platform/web.png"></div>
Expand Down Expand Up @@ -212,7 +225,7 @@
</svg>
@endif

<div id="swagger-ui" class="api-platform"></div>
<div id="swagger-ui" class="api-platform {{ $ui }}"></div>
@if ($ui === 'scalar')
<script src="https://cdn.jsdelivr.net/npm/@scalar/api-reference"></script>
<script src="/vendor/api-platform/init-scalar-ui.js"></script>
Expand Down
Loading