8000 minor #45566 [HttpClient] Handle requests with null body (jderusse) · symfony/symfony@b8bf937 · GitHub
[go: up one dir, main page]

Skip to content

Commit b8bf937

Browse files
minor #45566 [HttpClient] Handle requests with null body (jderusse)
This PR was merged into the 4.4 branch. Discussion ---------- [HttpClient] Handle requests with null body | Q | A | ------------- | --- | Branch? | 4.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | / | License | MIT | Doc PR | / since #45527 passing null to the `body` parameters leads to an exception (which [breaks async-aws](https://github.com/async-aws/aws/blob/09723ddca29b8d1d522426f81dd422373de1785f/src/Core/src/AbstractApi.php#L157)) > Argument #2 ($body) must be of type Closure, null given, called in /home/runner/work/aws/aws/vendor/symfony/http-client/CurlHttpClient.php on line 221 In curl client: `null` is not a string and `self::readRequestBody` expects a closure. https://github.com/symfony/symfony/blob/08fa74a16c84895575e305b2a7ee3a03e371f79b/src/Symfony/Component/HttpClient/CurlHttpClient.php#L214-L221 In NativeClient, `getBodyAsString` will fail to return `null` because of the `string` return type. Before #45527 null was converted to `""` thanks to the defaultOptions, but this is not the case anymore. In many places, we check if the body is `!== ""` but rarely check if the body is null, this PR restores the original behaviors for the `body` parameters and converts nulls to `""`. Commits ------- 39aec09 [HttpClient] Handle requests with null body
2 parents 65e6faa + 39aec09 commit b8bf937

File tree

2 files changed

+35
-3
lines changed

2 files changed

+35
-3
lines changed

src/Symfony/Component/HttpClient/HttpClientTrait.php

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
trait HttpClientTrait
2424
{
2525
private static $CHUNK_SIZE = 16372;
26+
private static $emptyDefaults;
2627

2728
/**
2829
* Validates and normalizes method, URL and options, and merges them with defaults.
@@ -40,6 +41,16 @@ private static function prepareRequest(?string $method, ?string $url, array $opt
4041
}
4142
}
4243

44+
if (null === self::$emptyDefaults) {
45+
self::$emptyDefaults = [];
46+
47+
foreach ($defaultOptions as $k => $v) {
48+
if (null !== $v) {
49+
self::$emptyDefaults[$k] = $v;
50+
}
51+
}
52+
}
53+
4354
$options = self::mergeDefaultOptions($options, $defaultOptions, $allowExtraOptions);
4455

4556
$buffer = $options['buffer'] ?? true;
@@ -189,6 +200,16 @@ private static function mergeDefaultOptions(array $options, array $defaultOption
189200

190201
$options += $defaultOptions;
191202

203+
if (null === self::$emptyDefaults) {
204+
self::$emptyDefaults = [];
205+
}
206+
207+
foreach (self::$emptyDefaults as $k => $v) {
208+
if (!isset($options[$k])) {
209+
$options[$k] = $v;
210+
}
211+
}
212+
192213
if (isset($defaultOptions['extra'])) {
193214
$options['extra'] += $defaultOptions['extra'];
194215
}
@@ -221,9 +242,9 @@ private static function mergeDefaultOptions(array $options, array $defaultOption
221242

222243
$alternatives = [];
223244

224-
foreach ($defaultOptions as $key => $v) {
225-
if (levenshtein($name, $key) <= \strlen($name) / 3 || str_contains($key, $name)) {
226-
$alternatives[] = $key;
245+
foreach ($defaultOptions as $k => $v) {
246+
if (levenshtein($name, $k) <= \strlen($name) / 3 || str_contains($k, $name)) {
247+
$alternatives[] = $k;
227248
}
228249
}
229250

src/Symfony/Component/HttpClient/Tests/CurlHttpClientTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,17 @@ public function testHandleIsReinitOnReset()
148148
self::assertNotSame($initialShareId, $clientState->share);
149149
}
150150

151+
public function testNullBody()
152+
{
153+
$httpClient = $this->getHttpClient(__FUNCTION__);
154+
155+
$httpClient->request('POST', 'http://localhost:8057/post', [
156+
'body' => null,
157+
]);
158+
159+
$this->expectNotToPerformAssertions();
160+
}
161+
151162
public function testProcessAfterReset()
152163
{
153164
$client = $this->getHttpClient(__FUNCTION__);

0 commit comments

Comments
 (0)
0