Skip to content

Commit 831cc35

Browse files
committed
[HttpClient] allow arbitrary JSON values in requests
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.
1 parent 0ea2855 commit 831cc35

File tree

4 files changed

+6
-9
lines changed

4 files changed

+6
-9
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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ interface HttpClientInterface
3535
'body' => '', // array|string|resource|\Traversable|\Closure - the callback SHOULD yield a string
3636
// smaller than the amount requested as argument; the empty string signals EOF; when
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 - when 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 they are not
40+
// defined - typically "application/json"
4141
'user_data' => 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy