Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[HttpClient] Fix json encode flags usage in copy-as-curl generation #46670

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

Conversation

welcoMattic
Copy link
Member

@welcoMattic welcoMattic commented Jun 14, 2022

Q A
Branch? 6.1
Bug fix? yes
New feature? no
Deprecations? no
Tickets
License MIT
Doc PR

The json_encode($json, \JSON_PRETTY_PRINT) call to generate the cURL version of a HTTP Request in HttpClientDataCollector didn't use the self::jsonEncode method available.

The main issue with that is the usage of \JSON_PRETTY_PRINT, which is overwrites all other flags in self::jsonEncode method, especially \JSON_PRESERVE_ZERO_FRACTION.

So I have added a test for it:

1) Symfony\Component\HttpClient\Tests\DataCollector\HttpClientDataCollectorTest::testItGeneratesCurlCommandsAsExpected with data set "POST with json" (array('POST', 'http://localhost:8057/js
on', array(array(array('baz', array(1.1, 1.0))))), 'curl \\n  --compressed \\n  -...n}%1$s')
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
         "bar": "baz",\n
         "qux": [\n
             1.1,\n
-            1.0\n
+            1\n
         ]\n
     }\n
 }''

After the changes, this test pass ✅

I've added test for all other flags, to be sure that they are used. It could lead to less readable JSON (with hex values) displayed in Profiler, but it's more accurate to debug.

EDIT: removing \JSON_PRETTY_PRINT solve the problem too, and we don't need to pretty-print the JSON in curl command, it allows to run the exact same request.

@welcoMattic welcoMattic force-pushed the fix/http-client-data-collector-json-encode branch from f1392ae to 1210828 Compare June 14, 2022 15:53
@welcoMattic welcoMattic force-pushed the fix/http-client-data-collector-json-encode branch from 1210828 to bb0ddf1 Compare June 16, 2022 08:41
@nicolas-grekas
Copy link
Member

Thank you @welcoMattic.

@nicolas-grekas nicolas-grekas merged commit b78725a into symfony:6.1 Jun 19, 2022
@fabpot fabpot mentioned this pull request Jun 26, 2022
@welcoMattic welcoMattic deleted the fix/http-client-data-collector-json-encode branch July 6, 2022 09:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants