Skip to content

Commit 0861669

Browse files
committed
send RFC 7807 compatible content type header
1 parent 414b830 commit 0861669

File tree

3 files changed

+26
-10
lines changed

3 files changed

+26
-10
lines changed

Serializer/Normalizer/FlattenExceptionHandler.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,19 @@ public static function getSubscribingMethods(): array
4949

5050
public function serializeToJson(JsonSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context)
5151
{
52+
if ($this->rfc7807) {
53+
$exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+json']);
54+
}
55+
5256
return $visitor->visitArray($this->convertToArray($exception, $context), $type, $context);
5357
}
5458

5559
public function serializeToXml(XmlSerializationVisitor $visitor, FlattenException $exception, array $type, Context $context)
5660
{
61+
if ($this->rfc7807) {
62+
$exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+xml']);
63+
}
64+
5765
$rootName = $this->rfc7807 ? 'response' : 'result';
5866

5967
$data = $this->convertToArray($exception, $context);

Serializer/Normalizer/FlattenExceptionNormalizer.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ public function normalize($exception, $format = null, array $context = [])
5252
}
5353

5454
if ($this->rfc7807) {
55+
if ('json' === $format) {
56+
$exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+json']);
57+
} elseif ('xml' === $format) {
58+
$exception->setHeaders($exception->getHeaders() + ['Content-Type' => 'application/problem+xml']);
59+
}
60+
5561
return [
5662
'type' => $context['type'] ?? 'https://tools.ietf.org/html/rfc2616#section-10',
5763
'title' => $context['title'] ?? 'An error occurred',

Tests/Functional/SerializerErrorTest.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ public function serializeExceptionJsonProvider()
6060
/**
6161
* @dataProvider serializeExceptionJsonUsingErrorRendererProvider
6262
*/
63-
public function testSerializeExceptionJsonUsingErrorRenderer(string $testCase, array $expectedJson)
63+
public function testSerializeExceptionJsonUsingErrorRenderer(string $testCase, array $expectedJson, string $expectedContentType)
6464
{
6565
if (!class_exists(SerializerErrorRenderer::class)) {
6666
$this->markTestSkipped();
@@ -71,6 +71,7 @@ public function testSerializeExceptionJsonUsingErrorRenderer(string $testCase, a
7171
$client = $this->createClient(['test_case' => $testCase, 'debug' => false]);
7272
$client->request('GET', '/serializer-error/exception.json');
7373

74+
$this->assertStringStartsWith($expectedContentType, $client->getResponse()->headers->get('Content-Type'));
7475
$this->assertEquals(json_encode($expectedJson), $client->getResponse()->getContent());
7576
}
7677

@@ -80,23 +81,23 @@ public function serializeExceptionJsonUsingErrorRendererProvider(): array
8081
['FlattenExceptionNormalizerLegacyFormat', [
8182
'code' => 500,
8283
'message' => 'Something bad happened.',
83-
]],
84+
], 'application/json'],
8485
['FlattenExceptionNormalizerRfc7807Format', [
8586
'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
8687
'title' => 'An error occurred',
8788
'status' => 500,
8889
'detail' => 'Something bad happened.',
89-
]],
90+
], 'application/problem+json'],
9091
['FlattenExceptionHandlerLegacyFormat', [
9192
'code' => 500,
9293
'message' => 'Something bad happened.',
93-
]],
94+
], 'application/json'],
9495
['FlattenExceptionHandlerRfc7807Format', [
9596
'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
9697
'title' => 'An error occurred',
9798
'status' => 500,
9899
'detail' => 'Something bad happened.',
99-
]],
100+
], 'application/problem+json'],
100101
];
101102
}
102103

@@ -195,7 +196,7 @@ public function serializeExceptionXmlProvider()
195196
/**
196197
* @dataProvider serializeExceptionXmlUsingErrorRendererProvider
197198
*/
198-
public function testSerializeExceptionXmlUsingErrorRenderer(string $testCase, string $expectedContent)
199+
public function testSerializeExceptionXmlUsingErrorRenderer(string $testCase, string $expectedContent, string $expectedContentType)
199200
{
200201
if (!class_exists(SerializerErrorRenderer::class)) {
201202
$this->markTestSkipped();
@@ -206,6 +207,7 @@ public function testSerializeExceptionXmlUsingErrorRenderer(string $testCase, st
206207
$client = $this->createClient(['test_case' => $testCase, 'debug' => false]);
207208
$client->request('GET', '/serializer-error/exception.xml');
208209

210+
$this->assertStringStartsWith($expectedContentType, $client->getResponse()->headers->get('Content-Type'));
209211
$this->assertXmlStringEqualsXmlString($expectedContent, $client->getResponse()->getContent());
210212
}
211213

@@ -236,10 +238,10 @@ public function serializeExceptionXmlUsingErrorRendererProvider(): array
236238
XML;
237239

238240
return [
239-
['FlattenExceptionNormalizerLegacyFormat', $expectedLegacyContent],
240-
['FlattenExceptionNormalizerRfc7807Format', $expectedRfc7807Content],
241-
['FlattenExceptionHandlerLegacyFormat', $expectedLegacyJmsContent],
242-
['FlattenExceptionHandlerRfc7807Format', $expectedRfc7807Content],
241+
['FlattenExceptionNormalizerLegacyFormat', $expectedLegacyContent, 'text/xml'],
242+
['FlattenExceptionNormalizerRfc7807Format', $expectedRfc7807Content, 'application/problem+xml'],
243+
['FlattenExceptionHandlerLegacyFormat', $expectedLegacyJmsContent, 'text/xml'],
244+
['FlattenExceptionHandlerRfc7807Format', $expectedRfc7807Content, 'application/problem+xml'],
243245
];
244246
}
245247

0 commit comments

Comments
 (0)