Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b78725a

Browse filesBrowse files
bug #46670 [HttpClient] Fix json encode flags usage in copy-as-curl generation (welcoMattic)
This PR was merged into the 6.1 branch. Discussion ---------- [HttpClient] Fix json encode flags usage in copy-as-curl generation | Q | A | ------------- | --- | Branch? | 6.1 | Bug fix? | yes | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | 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. Commits ------- bb0ddf1 Fix HttpClientTrait::jsonEncode flags usage
2 parents f0247e0 + bb0ddf1 commit b78725a
Copy full SHA for b78725a

File tree

2 files changed

+5
-7
lines changed
Filter options

2 files changed

+5
-7
lines changed

‎src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/DataCollector/HttpClientDataCollector.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ private function getCurlCommand(array $trace): ?string
194194
$dataArg = [];
195195

196196
if ($json = $trace['options']['json'] ?? null) {
197-
$dataArg[] = '--data '.escapeshellarg(json_encode($json, \JSON_PRETTY_PRINT));
197+
$dataArg[] = '--data '.escapeshellarg(self::jsonEncode($json));
198198
} elseif ($body = $trace['options']['body'] ?? null) {
199199
if (\is_string($body)) {
200200
try {

‎src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/HttpClient/Tests/DataCollector/HttpClientDataCollectorTest.php
+4-6Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ public function __toString(): string
307307
'json' => [
308308
'foo' => [
309309
'bar' => 'baz',
310+
'qux' => [1.10, 1.0],
311+
'fred' => ['<foo>',"'bar'",'"baz"','&blong&'],
310312
],
311313
],
312314
],
@@ -317,14 +319,10 @@ public function __toString(): string
317319
--url %1$shttp://localhost:8057/json%1$s \\
318320
--header %1$sContent-Type: application/json%1$s \\
319321
--header %1$sAccept: */*%1$s \\
320-
--header %1$sContent-Length: 21%1$s \\
322+
--header %1$sContent-Length: 120%1$s \\
321323
--header %1$sAccept-Encoding: gzip%1$s \\
322324
--header %1$sUser-Agent: Symfony HttpClient/Native%1$s \\
323-
--data %1$s{
324-
"foo": {
325-
"bar": "baz"
326-
}
327-
}%1$s',
325+
--data %1$s{"foo":{"bar":"baz","qux":[1.1,1.0],"fred":["\u003Cfoo\u003E","\u0027bar\u0027","\u0022baz\u0022","\u0026blong\u0026"]}}%1$s',
328326
];
329327
}
330328
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.