8000 bug #38122 [HttpClient] Fix Array to string conversion notice when pa… · symfony/symfony@9bb8084 · GitHub
[go: up one dir, main page]

Skip to content

Commit 9bb8084

Browse files
bug #38122 [HttpClient] Fix Array to string conversion notice when parsing JSON error body with non-scalar detail property (emarref)
This PR was squashed before being merged into the 4.4 branch. Discussion ---------- [HttpClient] Fix Array to string conversion notice when parsing JSON error body with non-scalar detail property | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | Fix #38118 | License | MIT | Doc PR | no An earlier commit added the ability to detect common API formats and extract a useful error message from the error response payload. To achieve this, if a `title` and `detail` property are found in the error response, the response is considered to be in a known format (RFC 7807) and these values are concatenated to form a helpful error message. However, when either `title` or `detail` property are present, but the document is not intended to follow the RFC semantics, if the properties are not scalar (e.g. if the detail property is an array of field validation violations) then the concatenation of the strings causes a notice to be raised. This pull request checks that the `title` and `detail` properties are scalar before attempting to concatenate them. If they are not, no enhanced error message is provided. Commits ------- 76fa884 [HttpClient] Fix Array to string conversion notice when parsing JSON error body with non-scalar detail property
2 parents 0ed20be + 76fa884 commit 9bb8084

File tree

2 files changed

+16
-11
lines changed

2 files changed

+16
-11
lines changed

src/Symfony/Component/HttpClient/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'] ?? '');

src/Symfony/Component/HttpClient/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)
0