Skip to content

Commit ec77bad

Browse files
committed
Handle empty request body.
1 parent 0a91a48 commit ec77bad

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/WebServCo/JSONAPI/Service/Handler/JSONAPIItemHandler.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,15 @@ public function handleRequest(ServerRequestInterface $request): bool
8989
*/
9090
private function handleFormProcessing(array $requestBodyAsArray): bool
9191
{
92+
if ($requestBodyAsArray === []) {
93+
/**
94+
* Request body is an empty array if request should not have a body.
95+
*
96+
* @see JSONAPIRequestService.getRequestBodyAsArray
97+
*/
98+
return true;
99+
}
100+
92101
/**
93102
* Start from local fields and iterate,
94103
* because id is stored in the actual formField (string key),
@@ -120,6 +129,15 @@ private function handleFormProcessing(array $requestBodyAsArray): bool
120129
*/
121130
private function handleVersionMatchCheck(array $requestBodyAsArray): bool
122131
{
132+
if ($requestBodyAsArray === []) {
133+
/**
134+
* Request body is an empty array if request should not have a body.
135+
*
136+
* @see JSONAPIRequestService.getRequestBodyAsArray
137+
*/
138+
return true;
139+
}
140+
123141
try {
124142
if (!$this->requestService->versionMatches($requestBodyAsArray, 1.1)) {
125143
$this->addErrorMessage('JSONAPI version does not match.');

src/WebServCo/JSONAPI/Service/JSONAPIRequestService.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,11 @@ public function getRequestBodyAsArray(ServerRequestInterface $request): array
3434
$requestBody = $request->getBody()->getContents();
3535

3636
if ($requestBody === '') {
37+
if (!$this->requestBodyService->canHaveRequestBody($request)) {
38+
// Correct that there is no request body.
39+
return [];
40+
}
41+
3742
// Possible situation: the body contents were read elsewhere and the stream was not rewinded.
3843
throw new UnexpectedValueException('Request body is empty.');
3944
}

0 commit comments

Comments
 (0)