Skip to content

Commit e42b97d

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 8d7f4dd commit e42b97d

File tree

3 files changed

+3
-6
lines changed

3 files changed

+3
-6
lines changed

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
-----

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) {

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
*/

0 commit comments

Comments
 (0)