Skip to content

Commit

Permalink
fix: swagger ui with route identifier (#6616)
Browse files Browse the repository at this point in the history
  • Loading branch information
soyuka authored Sep 17, 2024
1 parent 06a647a commit d74b2b5
Show file tree
Hide file tree
Showing 7 changed files with 14 additions and 2 deletions.
6 changes: 6 additions & 0 deletions features/openapi/docs.feature
Original file line number Diff line number Diff line change
Expand Up @@ -438,3 +438,9 @@ Feature: Documentation support
And I send a "GET" request to "/docs.jsonopenapi"
Then the response status code should be 200
And the response should be in JSON

Scenario: OpenAPI UI is enabled for docs endpoint
Given there are 1 dummy objects
Given I add "Accept" header equal to "text/html"
And I send a "GET" request to "/dummies/1.html"
Then the response status code should be 200
1 change: 1 addition & 0 deletions src/Laravel/Controller/ApiPlatformController.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ public function __invoke(Request $request): Response
}

$uriVariables = $this->getUriVariables($request, $operation);
$request->attributes->set('_api_uri_variables', $uriVariables);
// at some point we could introduce that back
// if ($this->uriVariablesConverter) {
// $context = ['operation' => $operation, 'uri_variables_map' => $uriVariablesMap];
Expand Down
1 change: 1 addition & 0 deletions src/Laravel/Exception/ErrorHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ public function register(): void
$dup->attributes->remove('exception');
// These are for swagger
$dup->attributes->set('_api_original_route', $request->attributes->get('_route'));
$dup->attributes->set('_api_original_uri_variables', $request->attributes->get('_api_uri_variables'));
$dup->attributes->set('_api_original_route_params', $request->attributes->get('_route_params'));
$dup->attributes->set('_api_requested_operation', $request->attributes->get('_api_requested_operation'));

Expand Down
3 changes: 2 additions & 1 deletion src/Laravel/State/SwaggerUiProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable
$status = 200;
$requestedOperation = $request?->attributes->get('_api_requested_operation') ?? null;
if ($request->isMethodSafe() && $requestedOperation && $requestedOperation->getName()) {
$swaggerData['id'] = $request->get('id');
// TODO: what if the parameter is named something else then `id`?
$swaggerData['id'] = ($request->attributes->get('_api_original_uri_variables') ?? [])['id'] ?? null;
$swaggerData['queryParameters'] = $request->query->all();

$swaggerData['shortName'] = $requestedOperation->getShortName();
Expand Down
3 changes: 2 additions & 1 deletion src/Symfony/Bundle/SwaggerUi/SwaggerUiProcessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,8 @@ public function process(mixed $openApi, Operation $operation, array $uriVariable
$status = 200;
$requestedOperation = $request?->attributes->get('_api_requested_operation') ?? null;
if ($request->isMethodSafe() && $requestedOperation && $requestedOperation->getName()) {
$swaggerData['id'] = $request->get('id');
// TODO: what if the parameter is named something else then `id`?
$swaggerData['id'] = ($request->attributes->get('_api_original_uri_variables') ?? [])['id'] ?? null;
$swaggerData['queryParameters'] = $request->query->all();

$swaggerData['shortName'] = $requestedOperation->getShortName();
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/Controller/MainController.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ public function __invoke(Request $request): Response
if (!$operation instanceof Error) {
try {
$uriVariables = $this->getOperationUriVariables($operation, $request->attributes->all(), $operation->getClass());
$request->attributes->set('_api_uri_variables', $uriVariables);
} catch (InvalidIdentifierException|InvalidUriVariableException $e) {
throw new NotFoundHttpException('Invalid uri variables.', $e);
}
Expand Down
1 change: 1 addition & 0 deletions src/Symfony/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ protected function duplicateRequest(\Throwable $exception, Request $request): Re
// These are for swagger
$dup->attributes->set('_api_original_route', $request->attributes->get('_route'));
$dup->attributes->set('_api_original_route_params', $request->attributes->get('_route_params'));
$dup->attributes->set('_api_original_uri_variables', $request->attributes->get('_api_uri_variables'));
$dup->attributes->set('_api_requested_operation', $request->attributes->get('_api_requested_operation'));
$dup->attributes->set('_api_platform_disable_listeners', true);

Expand Down

0 comments on commit d74b2b5

Please sign in to comment.