Skip to content

Commit 0523300

Browse files
committed
bug symfony#54146 [HttpClient] Preserve float in JsonMockResponse (Jibbarth)
This PR was merged into the 6.4 branch. Discussion ---------- [HttpClient] Preserve float in JsonMockResponse | Q | A | ------------- | --- | Branch? | 6.4 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Issues | Fix #... <!-- prefix each issue number with "Fix #", no need to create an issue if none exists, explain below instead --> | License | MIT Hello, I am mocking an API Response that can send float in values : ``` { "values": 1.0 } ``` Converting this to a php array and giving it to JsonMockResponse return me an int after, because the missing json_encode flag to keep zero fraction, and leds to issue when I want to deserialize it into a float property. In my mind it's a bugfix, but if it's considered as a new feature, I can target 7.1. Commits ------- 4d120b8 [HttpClient] Preserve float in JsonMockResponse
2 parents f5b5e14 + 4d120b8 commit 0523300

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

src/Symfony/Component/HttpClient/Response/JsonMockResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class JsonMockResponse extends MockResponse
2121
public function __construct(mixed $body = [], array $info = [])
2222
{
2323
try {
24-
$json = json_encode($body, \JSON_THROW_ON_ERROR);
24+
$json = json_encode($body, \JSON_THROW_ON_ERROR | \JSON_PRESERVE_ZERO_FRACTION);
2525
} catch (\JsonException $e) {
2626
throw new InvalidArgumentException('JSON encoding failed: '.$e->getMessage(), $e->getCode(), $e);
2727
}

src/Symfony/Component/HttpClient/Tests/Response/JsonMockResponseTest.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,22 @@ public function testJsonEncodeString()
5959
$this->assertSame('application/json', $response->getHeaders()['content-type'][0]);
6060
}
6161

62+
public function testJsonEncodeFloat()
63+
{
64+
$client = new MockHttpClient(new JsonMockResponse([
65+
'foo' => 1.23,
66+
'ccc' => 1.0,
67+
'baz' => 10.,
68+
]));
69+
$response = $client->request('GET', 'https://symfony.com');
70+
71+
$this->assertSame([
72+
'foo' => 1.23,
73+
'ccc' => 1.,
74+
'baz' => 10.,
75+
], $response->toArray());
76+
}
77+
6278
/**
6379
* @dataProvider responseHeadersProvider
6480
*/

0 commit comments

Comments
 (0)