8000 feature #34216 [HttpClient] allow arbitrary JSON values in requests (… · symfony/symfony@6779c33 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6779c33

Browse files
feature #34216 [HttpClient] allow arbitrary JSON values in requests (pschultz)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] allow arbitrary JSON values in requests | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | no | New feature? | no <!-- please update src/**/CHANGELOG.md files --> | Deprecations? | no <!-- please update UPGRADE-*.md and src/**/CHANGELOG.md files --> | Tickets | n/a | License | MIT | Doc PR | n/a Allow arbitrary values in the "json" request option. Previously values were limitated to arrays and objects of type JsonSerializable. This doesn't account for scalar values and classes with public properties (which don't need to implement JsonSerializable), [all of which are perfectly acceptable arguments to json_encode](https://www.php.net/manual/en/function.json-encode.php): > value > The value being encoded. Can be any type except a resource. It seems silly to expect users to add classes implementing JsonSerializable to represent, say, scalar values or an empty object. I'm not sure if you consider removed exceptional cases a BC break or not. I'm assuming "no" for now. Commits ------- e98731a [HttpClient] allow arbitrary JSON values in requests
2 parents 9a20437 + e98731a commit 6779c33

File tree

4 files changed

+7
-10
lines changed

4 files changed

+7
-10
lines changed

src/Symfony/Component/HttpClient/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ CHANGELOG
1515
* added `TraceableHttpClient`, `HttpClientDataCollector` and `HttpClientPass` to integrate with the web profiler
1616
* allow enabling buffering conditionally with a Closure
1717
* allow option "buffer" to be a stream resource
18+
* allow arbitrary values for the "json" option
1819

1920
4.3.0
2021
-----

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -332,18 +332,14 @@ private static function normalizePeerFingerprint($fingerprint): array
332332
}
333333

334334
/**
335-
* @param array|\JsonSerializable $value
335+
* @param mixed $value
336336
*
337337
* @throws InvalidArgumentException When the value cannot be json-encoded
338338
*/
339339
private static function jsonEncode($value, int $flags = null, int $maxDepth = 512): string
340340
{
341341
$flags = $flags ?? (JSON_HEX_TAG | JSON_HEX_APOS | JSON_HEX_AMP | JSON_HEX_QUOT | JSON_PRESERVE_ZERO_FRACTION);
342342

343-
if (!\is_array($value) && !$value instanceof \JsonSerializable) {
344-
throw new InvalidArgumentException(sprintf('Option "json" must be array or JsonSerializable, %s given.', \is_object($value) ? \get_class($value) : \gettype($value)));
345-
}
346-
347343
try {
348344
$value = json_encode($value, $flags | (\PHP_VERSION_ID >= 70300 ? \JSON_THROW_ON_ERROR : 0), $maxDepth);
349345
} catch (\JsonException $e) {

src/Symfony/Component/HttpClient/HttpOptions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function setBody($body)
8686
}
8787

8888
/**
89-
* @param array|\JsonSerializable $json
89+
* @param mixed $json
9090
*
9191
* @return $this
9292
*/

src/Symfony/Contracts/HttpClient/HttpClientInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ interface HttpClientInterface
3333
'query' => [], // string[] - associative array of query string values to merge with the request's URL
3434
'headers' => [], // iterable|string[]|string[][] - headers names provided as keys or as part of values
3535
'body' => '', // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string
36-
// smaller than the amount requested as argument; the empty string signals EOF; when
36+
// smaller than the amount requested as argument; the empty string signals EOF; if
3737
// an array is passed, it is meant as a form payload of field names and values
38-
'json' => null, // array|\JsonSerializable - when set, implementations MUST set the "body" option to
39-
// the JSON-encoded value and set the "content-type" headers to a JSON-compatible
40-
// value if they are not defined - typically "application/json"
38+
'json' => null, // mixed - if set, implementations MUST set the "body" option to the JSON-encoded
39+
// value and set the "content-type" header to a JSON-compatible value if it is not
40+
// explicitly defined in the headers option - typically "application/json"
4141
'user_data< 443C /span>' => null, // mixed - any extra data to attach to the request (scalar, callable, object...) that
4242
// MUST be available via $response->getInfo('user_data') - not used internally
4343
'max_redirects' => 20, // int - the maximum number of redirects to follow; a value lower than or equal to 0

0 commit comments

Comments
 (0)
0