Skip to content

Commit 22622fc

Browse files
emarrefnicolas-grekas
authored andcommitted
[HttpClient] Fix Array to string conversion notice when parsing JSON error body with non-scalar detail property
1 parent e4aa828 commit 22622fc

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

Exception/HttpExceptionTrait.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ public function __construct(ResponseInterface $response)
6060
// see http://www.hydra-cg.com/spec/latest/core/#description-of-http-status-codes-and-errors
6161
$separator = isset($body['hydra:title'], $body['hydra:description']) ? "\n\n" : '';
6262
$message = ($body['hydra:title'] ?? '').$separator.($body['hydra:description'] ?? '');
63-
} elseif (isset($body['title']) || isset($body['detail'])) {
63+
} elseif ((isset($body['title']) || isset($body['detail']))
64+
&& (is_scalar($body['title'] ?? '') && is_scalar($body['detail'] ?? ''))) {
6465
// see RFC 7807 and https://jsonapi.org/format/#error-objects
6566
$separator = isset($body['title'], $body['detail']) ? "\n\n" : '';
6667
$message = ($body['title'] ?? '').$separator.($body['detail'] ?? '');

Tests/Exception/HttpExceptionTraitTest.php

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,15 +22,24 @@ class HttpExceptionTraitTest extends TestCase
2222
{
2323
public function provideParseError(): iterable
2424
{
25-
yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}'];
26-
yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}'];
27-
yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}'];
25+
$errorWithoutMessage = 'HTTP/1.1 400 Bad Request returned for "http://example.com".';
26+
27+
$errorWithMessage = <<<ERROR
28+
An error occurred
29+
30+
Some details
31+
ERROR;
32+
33+
yield ['application/ld+json', '{"hydra:title": "An error occurred", "hydra:description": "Some details"}', $errorWithMessage];
34+
yield ['application/problem+json', '{"title": "An error occurred", "detail": "Some details"}', $errorWithMessage];
35+
yield ['application/vnd.api+json', '{"title": "An error occurred", "detail": "Some details"}', $errorWithMessage];
36+
yield ['application/json', '{"title": "An error occurred", "detail": {"field_name": ["Some details"]}}', $errorWithoutMessage];
2837
}
2938

3039
/**
3140
* @dataProvider provideParseError
3241
*/
33-
public function testParseError(string $mimeType, string $json): void
42+
public function testParseError(string $mimeType, string $json, string $expectedMessage): void
3443
{
3544
$response = $this->createMock(ResponseInterface::class);
3645
$response
@@ -47,12 +56,7 @@ public function testParseError(string $mimeType, string $json): void
4756

4857
$e = new TestException($response);
4958
$this->assertSame(400, $e->getCode());
50-
$this->assertSame(<<<ERROR
51-
An error occurred
52-
53-
Some details
54-
ERROR
55-
, $e->getMessage());
59+
$this->assertSame($expectedMessage, $e->getMessage());
5660
}
5761
}
5862

0 commit comments

Comments
 (0)