Skip to content

Commit 4d120b8

Browse files
committed
[HttpClient] Preserve float in JsonMockResponse
1 parent a184a18 commit 4d120b8

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)