Skip to content

Commit 0a91a48

Browse files
committed
validateContentType
1 parent 50cccf6 commit 0a91a48

File tree

2 files changed

+15
-8
lines changed

2 files changed

+15
-8
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public function handleRequest(ServerRequestInterface $request): bool
6666
$this->setSent();
6767

6868
// Check content type.
69-
if (!$this->requestService->contentTypeMatches($request)) {
69+
if (!$this->requestService->validateContentType($request)) {
7070
$this->addErrorMessage('Content type does not match.');
7171

7272
return false;

src/WebServCo/JSONAPI/Service/JSONAPIRequestService.php

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Psr\Http\Message\ServerRequestInterface;
88
use UnexpectedValueException;
99
use WebServCo\Data\Contract\Extraction\DataExtractionContainerInterface;
10+
use WebServCo\Http\Contract\Message\Request\RequestBodyServiceInterface;
1011
use WebServCo\Http\Contract\Message\Request\RequestHeaderServiceInterface;
1112
use WebServCo\JSONAPI\Contract\Document\JSONAPIInterface;
1213
use WebServCo\JSONAPI\Contract\Service\JSONAPIRequestServiceInterface;
@@ -20,17 +21,11 @@ final class JSONAPIRequestService implements JSONAPIRequestServiceInterface
2021
{
2122
public function __construct(
2223
private readonly DataExtractionContainerInterface $dataExtractionContainer,
24+
private readonly RequestBodyServiceInterface $requestBodyService,
2325
private readonly RequestHeaderServiceInterface $requestHeaderService,
2426
) {
2527
}
2628

27-
public function contentTypeMatches(ServerRequestInterface $request): bool
28-
{
29-
$contentTypeHeaderValue = $this->requestHeaderService->getHeaderValue('Content-Type', $request);
30-
31-
return $contentTypeHeaderValue === JSONAPIInterface::MEDIA_TYPE;
32-
}
33-
3429
/**
3530
* @inheritDoc
3631
*/
@@ -55,6 +50,18 @@ public function getRequestBodyAsArray(ServerRequestInterface $request): array
5550
return $array;
5651
}
5752

53+
public function validateContentType(ServerRequestInterface $request): bool
54+
{
55+
if (!$this->requestBodyService->canHaveRequestBody($request)) {
56+
// No request body, nothing to check.
57+
return true;
58+
}
59+
60+
$contentTypeHeaderValue = $this->requestHeaderService->getHeaderValue('Content-Type', $request);
61+
62+
return $contentTypeHeaderValue === JSONAPIInterface::MEDIA_TYPE;
63+
}
64+
5865
/**
5966
* @inheritDoc
6067
*/

0 commit comments

Comments
 (0)