Skip to content

Commit bf29e21

Browse files
committed
fix: json formatted resource should not get xml errors #7287
1 parent d1e6772 commit bf29e21

File tree

4 files changed

+76
-3
lines changed

4 files changed

+76
-3
lines changed

src/State/ApiResource/Error.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
routeName: '_api_errors',
3939
outputFormats: [
4040
'json' => ['application/problem+json', 'application/json'],
41-
'xml' => ['application/xml', 'text/xml'],
4241
],
4342
hideHydraOperation: true,
4443
normalizationContext: [
@@ -75,6 +74,21 @@
7574
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
7675
],
7776
),
77+
new Operation(
78+
errors: [],
79+
name: '_api_errors_xml',
80+
routeName: '_api_errors',
81+
outputFormats: [
82+
'xml' => ['application/xml', 'text/xml'],
83+
],
84+
hideHydraOperation: true,
85+
normalizationContext: [
86+
SchemaFactory::OPENAPI_DEFINITION_NAME => '',
87+
'groups' => ['jsonproblem'],
88+
'skip_null_values' => true,
89+
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
90+
],
91+
),
7892
new Operation(
7993
name: '_api_errors',
8094
hideHydraOperation: true,

src/Validator/Exception/ValidationException.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@
5151
name: '_api_validation_errors_problem',
5252
outputFormats: [
5353
'json' => ['application/problem+json'],
54-
'xml' => ['application/xml', 'text/xml'],
5554
],
5655
normalizationContext: [
5756
'groups' => ['json'],
@@ -79,6 +78,17 @@
7978
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
8079
]
8180
),
81+
new ErrorOperation(
82+
name: '_api_validation_errors_problem',
83+
outputFormats: [
84+
'xml' => ['application/xml', 'text/xml'],
85+
],
86+
normalizationContext: [
87+
'groups' => ['json'],
88+
'ignored_attributes' => ['trace', 'file', 'line', 'code', 'message', 'traceAsString', 'previous'],
89+
'skip_null_values' => true,
90+
]
91+
),
8292
],
8393
graphQlOperations: []
8494
)]
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the API Platform project.
5+
*
6+
* (c) Kévin Dunglas <dunglas@gmail.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7287;
15+
16+
use ApiPlatform\Metadata\ApiResource;
17+
use ApiPlatform\Metadata\GetCollection;
18+
use ApiPlatform\Metadata\NotExposed;
19+
20+
#[ApiResource(
21+
outputFormats: ['jsonld'],
22+
operations: [
23+
new GetCollection(provider: [self::class, 'provide']),
24+
new NotExposed(uriVariables: ['id']),
25+
]
26+
)]
27+
class OperationWithDefaultFormat
28+
{
29+
public function __construct(
30+
public string $id,
31+
) {
32+
}
33+
34+
public static function provide(): array
35+
{
36+
return [new self('1')];
37+
}
38+
}

tests/Functional/ErrorTest.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
use ApiPlatform\State\ApiResource\Error;
1717
use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
18+
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\Issue7287\OperationWithDefaultFormat;
1819
use ApiPlatform\Tests\Fixtures\TestBundle\ApiResource\XmlWithJsonError;
1920
use ApiPlatform\Tests\SetupClassResourcesTrait;
2021
use PHPUnit\Framework\Attributes\DataProvider;
@@ -30,7 +31,7 @@ final class ErrorTest extends ApiTestCase
3031
*/
3132
public static function getResources(): array
3233
{
33-
return [Error::class, XmlWithJsonError::class];
34+
return [Error::class, XmlWithJsonError::class, OperationWithDefaultFormat::class];
3435
}
3536

3637
#[DataProvider('formatsProvider')]
@@ -108,4 +109,14 @@ public function testXmlError(): void
108109
$this->assertResponseStatusCodeSame(404);
109110
$this->assertResponseHeaderSame('content-type', 'application/xml; charset=utf-8');
110111
}
112+
113+
public function testNotAcceptableXml(): void
114+
{
115+
self::createClient()->request('GET', '/operation_with_default_formats', [
116+
'headers' => ['accept' => 'text/xml'],
117+
]);
118+
119+
$this->assertJsonContains(['detail' => 'Requested format "text/xml" is not supported. Supported MIME types are "application/ld+json".']);
120+
$this->assertResponseStatusCodeSame(406);
121+
}
111122
}

0 commit comments

Comments
 (0)