Skip to content

Commit 6842be2

Browse files
committed
fix body
1 parent 50d97af commit 6842be2

3 files changed

Lines changed: 81 additions & 17 deletions

File tree

src/Controller.php

Lines changed: 24 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
use Chevere\Parameter\Interfaces\ParameterInterface;
3030
use Chevere\Parameter\Interfaces\ParametersAccessInterface;
3131
use Chevere\Parameter\Interfaces\TypedInterface;
32+
use Chevere\Parameter\Interfaces\UnionParameterInterface;
3233
use LogicException;
3334
use PhpParser\Builder\Param;
3435
use Psr\Http\Message\ResponseInterface;
@@ -40,6 +41,7 @@
4041
use function Chevere\Parameter\arrayp;
4142
use function Chevere\Parameter\arrayString;
4243
use function Chevere\Parameter\mixed;
44+
use function Chevere\Parameter\parameters;
4345
use function Chevere\Parameter\typed;
4446

4547
abstract class Controller extends BaseController implements ControllerInterface
@@ -123,26 +125,31 @@ final public function withServerRequest(ServerRequestInterface $serverRequest):
123125
$parsedBody = (array) $parsedBody;
124126
}
125127
$new->_body = $parsedBody ?? null;
126-
if (! $new->_body && $serverRequest->getHeaderLine('Content-Type') === 'application/json') {
127-
$streamed = $new->_bodyStream->__toString();
128-
$new->_body = json_decode($streamed, true);
129-
if ($new->_body === null && $streamed !== '') {
130-
$new->_body = $streamed;
128+
if ($serverRequest->getHeaderLine('Content-Type') === 'application/json') {
129+
if (! $new->_body) {
130+
$streamed = $new->_bodyStream->__toString();
131+
$new->_body = json_decode($streamed, true);
132+
if ($new->_body === null && $streamed !== '') {
133+
$new->_body = $streamed;
134+
}
131135
}
132-
}
133-
if ($new->_body === null) {
134-
$new->_body = $streamed ?? $new->_bodyStream->__toString();
136+
} elseif ($new->_body === null) {
137+
$new->_body = $new->_bodyStream->__toString();
135138
}
136139
$acceptBody = $new::acceptBody();
137-
$acceptBody = $acceptBody instanceof ParametersAccessInterface
138-
? $acceptBody->parameters()
139-
: arrayp();
140-
$new->_bodyParsed = arguments(
141-
$acceptBody,
142-
is_array($new->_body)
143-
? $new->_body
144-
: ($parsedBody ?? [])
145-
);
140+
$new->_bodyParsed = arguments(parameters(), []);
141+
if ($acceptBody instanceof UnionParameterInterface) {
142+
$acceptBody->__invoke($new->_body);
143+
} else {
144+
if ($acceptBody instanceof ParametersAccessInterface) {
145+
$new->_bodyParsed = arguments(
146+
$acceptBody,
147+
is_array($new->_body)
148+
? $new->_body
149+
: ($parsedBody ?? [])
150+
);
151+
}
152+
}
146153
} catch (Throwable $e) {
147154
throw new ControllerException(
148155
'[HTTP body] ' . $e->getMessage(),

tests/ControllerTest.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Chevere\Http\Status;
2121
use Chevere\Parameter\Interfaces\ArrayStringParameterInterface;
2222
use Chevere\Tests\src\AcceptBodyController;
23+
use Chevere\Tests\src\AcceptBodyUnionController;
2324
use Chevere\Tests\src\AcceptController;
2425
use Chevere\Tests\src\AcceptHeadersController;
2526
use Chevere\Tests\src\AcceptOptionalController;
@@ -31,6 +32,7 @@
3132
use Error;
3233
use Nyholm\Psr7\Response;
3334
use Nyholm\Psr7\ServerRequest;
35+
use Nyholm\Psr7\Stream;
3436
use Nyholm\Psr7\UploadedFile;
3537
use OutOfBoundsException;
3638
use PHPUnit\Framework\TestCase;
@@ -231,6 +233,25 @@ public function testAcceptBody(): void
231233
);
232234
}
233235

236+
public function testAcceptBodyUnion(): void
237+
{
238+
$serverRequest = new ServerRequest('GET', '/', [
239+
'Content-Type' => 'application/json',
240+
]);
241+
$controller = new AcceptBodyUnionController();
242+
$controller->withServerRequest(
243+
$serverRequest
244+
);
245+
$streamString = (new Stream(fopen('php://temp', 'r+')));
246+
$streamString->write(json_encode('123'));
247+
$with = $controller->withServerRequest(
248+
$serverRequest
249+
->withBody($streamString)
250+
);
251+
$this->assertCount(0, $with->bodyParsed()->parameters());
252+
$this->assertSame('123', $with->body()->string());
253+
}
254+
234255
public function testAcceptQueryBodyFiles(): void
235256
{
236257
$serverRequest = new ServerRequest('GET', '/');
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
<?php
2+
3+
/*
4+
* This file is part of Chevere.
5+
*
6+
* (c) Rodolfo Berrios <rodolfo@chevere.org>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Chevere\Tests\src;
15+
16+
use Chevere\Http\Attributes\Request;
17+
use Chevere\Http\Controller;
18+
use Chevere\Http\Header;
19+
use Chevere\Parameter\Interfaces\UnionParameterInterface;
20+
use function Chevere\Parameter\string;
21+
use function Chevere\Parameter\unionNull;
22+
23+
#[Request(
24+
new Header('Content-Type', 'application/json')
25+
)]
26+
final class AcceptBodyUnionController extends Controller
27+
{
28+
public function __invoke(): void
29+
{
30+
}
31+
32+
public static function acceptBody(): UnionParameterInterface
33+
{
34+
return unionNull(string());
35+
}
36+
}

0 commit comments

Comments
 (0)