Skip to content

Commit 267f3c4

Browse files
committed
Merge branch '2.x' into 3.x
* 2.x: clear parsedBody "cache" when calling withBody()
2 parents ebb14cd + ccbb9d5 commit 267f3c4

2 files changed

Lines changed: 62 additions & 0 deletions

File tree

src/HttpMessage/ServerRequest.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use bdk\HttpMessage\Utility\ServerRequest as ServerRequestUtil;
1919
use InvalidArgumentException;
2020
use Psr\Http\Message\ServerRequestInterface;
21+
use Psr\Http\Message\StreamInterface;
2122
use Psr\Http\Message\UriInterface;
2223

2324
/**
@@ -75,6 +76,9 @@ class ServerRequest extends Request implements ServerRequestInterface
7576
/** @var null|array|object typically $_POST */
7677
private $parsedBody = null;
7778

79+
/** @var bool */
80+
private $parsedBodyExplicitlySet = false;
81+
7882
/**
7983
* Query (aka $_GET) params.
8084
*
@@ -141,6 +145,20 @@ public static function fromGlobals(array $parseStrOpts = array())
141145
return ServerRequestUtil::fromGlobals($parseStrOpts);
142146
}
143147

148+
/**
149+
* {@inheritDoc}
150+
*/
151+
#[\Override]
152+
public function withBody(StreamInterface $body): static
153+
{
154+
$new = parent::withBody($body);
155+
if ($new->parsedBodyExplicitlySet === false) {
156+
// ensure that getParsedBody() will re-parse body
157+
$new->parsedBody = null;
158+
}
159+
return $new;
160+
}
161+
144162
/**
145163
* Get $_SERVER values
146164
*
@@ -294,6 +312,7 @@ public function withParsedBody($data): static
294312
$this->assertParsedBody($data);
295313
$new = clone $this;
296314
$new->parsedBody = $data;
315+
$new->parsedBodyExplicitlySet = $data !== null;
297316
return $new;
298317
}
299318

tests/HttpMessage/ServerRequestTest.php

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
use bdk\HttpMessage\Message;
66
use bdk\HttpMessage\Request;
77
use bdk\HttpMessage\ServerRequest;
8+
use bdk\HttpMessage\Utility\ContentType;
89
use bdk\HttpMessage\Utility\ParseStr;
910
use ReflectionObject;
1011

@@ -159,6 +160,48 @@ public function testGetMethods()
159160
);
160161
}
161162

163+
public function testWithBodyClearsParsedBody()
164+
{
165+
$serverRequest = $this->createServerRequest('POST', '', array(
166+
'CONTENT_TYPE' => ContentType::JSON,
167+
))->withBody(
168+
$this->createStream(\json_encode(array('foo' => 'bar')))
169+
);
170+
171+
$this->assertSame(array('foo' => 'bar'), $serverRequest->getParsedBody());
172+
173+
$serverRequest = $serverRequest->withBody(
174+
$this->createStream(\json_encode(array('ding' => 'dong')))
175+
);
176+
177+
$this->assertSame(array('ding' => 'dong'), $serverRequest->getParsedBody());
178+
}
179+
180+
public function testWithBodyDoesNotClearParsedBody()
181+
{
182+
$serverRequest = $this->createServerRequest('POST', '', array(
183+
'CONTENT_TYPE' => ContentType::JSON,
184+
))->withBody(
185+
$this->createStream(\json_encode(array('foo' => 'bar')))
186+
)->withParsedBody(array(
187+
'snap' => 'crackle',
188+
));
189+
190+
// we explicitly set the parsed body - ignore the body
191+
$this->assertSame(array('snap' => 'crackle'), $serverRequest->getParsedBody());
192+
193+
// updating the body does nothing.... use the explicit parsed body
194+
$serverRequest = $serverRequest->withBody(
195+
$this->createStream(\json_encode(array('ding' => 'dong')))
196+
);
197+
$this->assertSame(array('snap' => 'crackle'), $serverRequest->getParsedBody());
198+
199+
// withParsedBody(null) -> we will attempt to parsed the body
200+
$serverRequest = $serverRequest->withParsedBody(null);
201+
$this->assertSame(array('ding' => 'dong'), $serverRequest->getParsedBody());
202+
}
203+
204+
162205
/**
163206
* @param string $contentType Request content type
164207
* @param string $body Request body

0 commit comments

Comments
 (0)