File tree Expand file tree Collapse file tree 3 files changed +37
-3
lines changed
src/Symfony/EventListener
Fixtures/TestBundle/ApiResource Expand file tree Collapse file tree 3 files changed +37
-3
lines changed Original file line number Diff line number Diff line change @@ -68,8 +68,9 @@ public function __construct(
68
68
protected function duplicateRequest (\Throwable $ exception , Request $ request ): Request
69
69
{
70
70
$ format = $ this ->getRequestFormat ($ request , $ this ->errorFormats , false );
71
- // Because ErrorFormatGuesser is buggy in some cases
72
- $ request ->setRequestFormat ($ format );
71
+ // Reset the request format as it may be that the original request format negotiation won't have the same result
72
+ // when an error occurs
73
+ $ request ->setRequestFormat (null );
73
74
$ apiOperation = $ this ->initializeOperation ($ request );
74
75
75
76
// TODO: add configuration flag to:
Original file line number Diff line number Diff line change
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 ;
15
+
16
+ use ApiPlatform \Metadata \Post ;
17
+
18
+ #[Post(inputFormats: ['xml ' => 'application/xml ' ], uriVariables: ['id ' ])]
19
+ class XmlWithJsonError
20
+ {
21
+ }
Original file line number Diff line number Diff line change 15
15
16
16
use ApiPlatform \State \ApiResource \Error ;
17
17
use ApiPlatform \Symfony \Bundle \Test \ApiTestCase ;
18
+ use ApiPlatform \Tests \Fixtures \TestBundle \ApiResource \XmlWithJsonError ;
18
19
use ApiPlatform \Tests \SetupClassResourcesTrait ;
19
20
use PHPUnit \Framework \Attributes \DataProvider ;
20
21
@@ -29,7 +30,7 @@ final class ErrorTest extends ApiTestCase
29
30
*/
30
31
public static function getResources (): array
31
32
{
32
- return [Error::class];
33
+ return [Error::class, XmlWithJsonError::class ];
33
34
}
34
35
35
36
#[DataProvider('formatsProvider ' )]
@@ -79,4 +80,15 @@ public static function formatsProvider(): array
79
80
],
80
81
];
81
82
}
83
+
84
+ public function testJsonError (): void
85
+ {
86
+ self ::createClient ()->request ('POST ' , '/xml_with_json_errors ' , [
87
+ 'headers ' => ['content-type ' => 'application/json ' ],
88
+ 'body ' => '<xml></xml> ' ,
89
+ ]);
90
+
91
+ $ this ->assertResponseStatusCodeSame (415 );
92
+ $ this ->assertJsonContains (['detail ' => 'The content-type "application/json" is not supported. Supported MIME types are "application/xml". ' ]);
93
+ }
82
94
}
You can’t perform that action at this time.
0 commit comments