Skip to content

fix: error formats #7148

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2025
Merged
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
5 changes: 3 additions & 2 deletions src/Symfony/EventListener/ErrorListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,9 @@ public function __construct(
protected function duplicateRequest(\Throwable $exception, Request $request): Request
{
$format = $this->getRequestFormat($request, $this->errorFormats, false);
// Because ErrorFormatGuesser is buggy in some cases
$request->setRequestFormat($format);
// Reset the request format as it may be that the original request format negotiation won't have the same result
// when an error occurs
$request->setRequestFormat(null);
$apiOperation = $this->initializeOperation($request);

// TODO: add configuration flag to:
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Routing/ApiLoader.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,10 @@ public function load(mixed $data, ?string $type = null): RouteCollection
$path,
[
'_controller' => $controller ?? 'api_platform.action.placeholder',
'_format' => null,
'_stateless' => $operation->getStateless(),
'_api_resource_class' => $resourceClass,
'_api_operation_name' => $operationName,
] + ($operation->getDefaults() ?? []),
] + ($operation->getDefaults() ?? []) + ['_format' => null],
$operation->getRequirements() ?? [],
$operation->getOptions() ?? [],
$operation->getHost() ?? '',
Expand Down
21 changes: 21 additions & 0 deletions tests/Fixtures/TestBundle/ApiResource/XmlWithJsonError.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?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\Tests\Fixtures\TestBundle\ApiResource;

use ApiPlatform\Metadata\Post;

#[Post(inputFormats: ['xml' => 'application/xml'], uriVariables: ['id'])]
class XmlWithJsonError
{
}
14 changes: 13 additions & 1 deletion tests/Functional/ErrorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

use ApiPlatform\State\ApiResource\Error;
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\XmlWithJsonError;
use ApiPlatform\Tests\SetupClassResourcesTrait;
use PHPUnit\Framework\Attributes\DataProvider;

Expand All @@ -29,7 +30,7 @@ final class ErrorTest extends ApiTestCase
*/
public static function getResources(): array
{
return [Error::class];
return [Error::class, XmlWithJsonError::class];
}

#[DataProvider('formatsProvider')]
Expand Down Expand Up @@ -79,4 +80,15 @@ public static function formatsProvider(): array
],
];
}

public function testJsonError(): void
{
self::createClient()->request('POST', '/xml_with_json_errors', [
'headers' => ['content-type' => 'application/json'],
'body' => '<xml></xml>',
]);

$this->assertResponseStatusCodeSame(415);
$this->assertJsonContains(['detail' => 'The content-type "application/json" is not supported. Supported MIME types are "application/xml".']);
}
}
2 changes: 1 addition & 1 deletion tests/Symfony/Routing/ApiLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ private function getRoute(string $path, string $controller, ?bool $stateless, st
$path,
[
'_controller' => $controller,
'_format' => null,
'_format' => $extraDefaults['_format'] ?? null,
'_stateless' => $stateless,
'_api_resource_class' => $resourceClass,
'_api_operation_name' => $operationName,
Expand Down
Loading