Skip to content

Commit 7d5e7cf

Browse files
committed
Merge branch '2.x'
* 2.x: send RFC 7807 compatible content type header
2 parents eefa714 + a0426fe commit 7d5e7cf

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
@@ -35,7 +35,7 @@ public static function tearDownAfterClass()
3535
/**
3636
* @dataProvider serializeExceptionJsonUsingErrorRendererProvider
3737
*/
38-
public function testSerializeExceptionJsonUsingErrorRenderer(string $testCase, array $expectedJson)
38+
public function testSerializeExceptionJsonUsingErrorRenderer(string $testCase, array $expectedJson, string $expectedContentType)
3939
{
4040
if (!class_exists(SerializerErrorRenderer::class)) {
4141
$this->markTestSkipped();
@@ -46,6 +46,7 @@ public function testSerializeExceptionJsonUsingErrorRenderer(string $testCase, a
4646
$client = $this->createClient(['test_case' => $testCase, 'debug' => false]);
4747
$client->request('GET', '/serializer-error/exception.json');
4848

49+
$this->assertStringStartsWith($expectedContentType, $client->getResponse()->headers->get('Content-Type'));
4950
$this->assertEquals(json_encode($expectedJson), $client->getResponse()->getContent());
5051
}
5152

@@ -55,23 +56,23 @@ public function serializeExceptionJsonUsingErrorRendererProvider(): array
5556
['FlattenExceptionNormalizerLegacyFormat', [
5657
'code' => 500,
5758
'message' => 'Something bad happened.',
58-
]],
59+
], 'application/json'],
5960
['FlattenExceptionNormalizerRfc7807Format', [
6061
'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
6162
'title' => 'An error occurred',
6263
'status' => 500,
6364
'detail' => 'Something bad happened.',
64-
]],
65+
], 'application/problem+json'],
6566
['FlattenExceptionHandlerLegacyFormat', [
6667
'code' => 500,
6768
'message' => 'Something bad happened.',
68-
]],
69+
], 'application/json'],
6970
['FlattenExceptionHandlerRfc7807Format', [
7071
'type' => 'https://tools.ietf.org/html/rfc2616#section-10',
7172
'title' => 'An error occurred',
7273
'status' => 500,
7374
'detail' => 'Something bad happened.',
74-
]],
75+
], 'application/problem+json'],
7576
];
7677
}
7778

@@ -106,7 +107,7 @@ public function testSerializeUnknownExceptionJsonWithoutDebugUsingErrorRenderer(
106107
/**
107108
* @dataProvider serializeExceptionXmlUsingErrorRendererProvider
108109
*/
109-
public function testSerializeExceptionXmlUsingErrorRenderer(string $testCase, string $expectedContent)
110+
public function testSerializeExceptionXmlUsingErrorRenderer(string $testCase, string $expectedContent, string $expectedContentType)
110111
{
111112
if (!class_exists(SerializerErrorRenderer::class)) {
112113
$this->markTestSkipped();
@@ -117,6 +118,7 @@ public function testSerializeExceptionXmlUsingErrorRenderer(string $testCase, st
117118
$client = $this->createClient(['test_case' => $testCase, 'debug' => false]);
118119
$client->request('GET', '/serializer-error/exception.xml');
119120

121+
$this->assertStringStartsWith($expectedContentType, $client->getResponse()->headers->get('Content-Type'));
120122
$this->assertXmlStringEqualsXmlString($expectedContent, $client->getResponse()->getContent());
121123
}
122124

@@ -147,10 +149,10 @@ public function serializeExceptionXmlUsingErrorRendererProvider(): array
147149
XML;
148150

149151
return [
150-
['FlattenExceptionNormalizerLegacyFormat', $expectedLegacyContent],
151-
['FlattenExceptionNormalizerRfc7807Format', $expectedRfc7807Content],
152-
['FlattenExceptionHandlerLegacyFormat', $expectedLegacyJmsContent],
153-
['FlattenExceptionHandlerRfc7807Format', $expectedRfc7807Content],
152+
['FlattenExceptionNormalizerLegacyFormat', $expectedLegacyContent, 'text/xml'],
153+
['FlattenExceptionNormalizerRfc7807Format', $expectedRfc7807Content, 'application/problem+xml'],
154+
['FlattenExceptionHandlerLegacyFormat', $expectedLegacyJmsContent, 'text/xml'],
155+
['FlattenExceptionHandlerRfc7807Format', $expectedRfc7807Content, 'application/problem+xml'],
154156
];
155157
}
156158

0 commit comments

Comments
 (0)