Skip to content

Commit

Permalink
fix(symfony): swagger ui should use base url (#5918)
Browse files Browse the repository at this point in the history
fixes #5904
  • Loading branch information
soyuka authored Oct 23, 2023
1 parent ae090c7 commit 3d0dfc1
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/SwaggerUi/SwaggerUiProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,6 @@ class: OpenApi::class,
// save our operation
$request->attributes->set('_api_operation', $swaggerUiOperation);

return $this->openApiFactory->__invoke($context);
return $this->openApiFactory->__invoke(['base_url' => $request->getBaseUrl() ?: '/']);
}
}
42 changes: 42 additions & 0 deletions tests/Symfony/Bundle/SwaggerUi/SwaggerUiProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <[email protected]>
*
* 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\Tests\Symfony\Bundle\SwaggerUi;

use ApiPlatform\Documentation\Documentation;
use ApiPlatform\Metadata\Get;
use ApiPlatform\OpenApi\Factory\OpenApiFactoryInterface;
use ApiPlatform\OpenApi\Model\Info;
use ApiPlatform\OpenApi\Model\Paths;
use ApiPlatform\OpenApi\OpenApi;
use ApiPlatform\State\ProviderInterface;
use ApiPlatform\Symfony\Bundle\SwaggerUi\SwaggerUiProvider;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;

class SwaggerUiProviderTest extends TestCase
{
public function testProvideWithBaseUrl(): void
{
$openapiFactory = $this->createMock(OpenApiFactoryInterface::class);
$request = $this->createStub(Request::class);
$request->attributes = new ParameterBag();
$request->method('getRequestFormat')->willReturn('html');
$request->method('getBaseUrl')->willReturn('test');
$decorated = $this->createStub(ProviderInterface::class);
$provider = new SwaggerUiProvider($decorated, $openapiFactory);
$openapiFactory->expects($this->once())->method('__invoke')->with(['base_url' => 'test'])->willReturn(new OpenApi(new Info('test', '1'), [], new Paths()));
$provider->provide(new Get(class: Documentation::class), [], ['request' => $request]);
}
}

0 comments on commit 3d0dfc1

Please sign in to comment.