-
Notifications
You must be signed in to change notification settings - Fork 11.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Http::fake does not preserve trailing zeros in JSON responses #54482
Comments
You'll have to fetch the content and parse it to JSON yourself, using the json_decode option to preserve zero fractions. I think that's something like: $response = Http::get('api.example.com/data');
$data = json_decode($response->body(), true, flags: JSON_PRESERVE_ZERO_FRACTION); |
Hi, Thanks for your response. I have an alternative suggestion: would it be possible to add an option directly to the Http facade to specify JSON decode flags? For example, something like: $response = Http::get('api.example.com/data');
$data = $response->json(flags: JSON_PRESERVE_ZERO_FRACTION); This would allow developers to preserve trailing zeros in decimals (e.g., 10.0) without having to manually decode the response with json_decode(). Is there any plan or possibility to introduce such a feature in the Http facade? It would greatly simplify cases where the exact numeric formatting is important. Thanks! |
Thank you for reporting this issue! As Laravel is an open source project, we rely on the community to help us diagnose and fix issues as it is not possible to research and fix every issue reported to us via GitHub. If possible, please make a pull request fixing the issue you have described, along with corresponding tests. All pull requests are promptly reviewed by the Laravel team. Thank you! |
I created this PR #54492 Edit: My PR was refused, so I'll use the solution above anyway, thank you. |
Laravel Version
11.39.1
PHP Version
8.4
Database Driver & Version
SQLite for macOS 15.3 on arm64 (Homebrew)
Description
When using Http::fake() in tests with fixture responses containing decimal numbers formatted with trailing zeros (e.g., 10.0), the numeric values are decoded without the trailing zero (resulting in 10). This causes tests that rely on exact formatting to fail. Although this behavior is a consequence of PHP’s json_decode() (which converts numbers to their numeric representation), it negatively impacts tests that expect the exact string representation as defined in the fixture.
Steps To Reproduce
Create a JSON Fixture:
tests/fixtures/sample.json
with the following content:10.0
with a trailing zero.Set Up the Test with
Http::fake()
:Trigger the HTTP Request:
Http::fake()
:Compare the Returned Data:
10.0
from the fixture is decoded as10
, causing a mismatch.The text was updated successfully, but these errors were encountered: