Skip to content

Commit ebbe68b

Browse files
authored
Merge pull request #20 from shouze/fix/request-content-edge-case
🐛 Fix request content check edge case
2 parents 6ede6f8 + 93a8e76 commit ebbe68b

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

src/JsonBodyListener.php

+13-18
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,11 @@ public function __construct(PayloadValidator $payloadValidator)
2121
public function onKernelRequest(GetResponseEvent $event)
2222
{
2323
$request = $event->getRequest();
24-
$method = $request->getMethod();
25-
26-
if (count($request->request->all())
27-
|| !in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE', 'LINK', 'UNLINK'])
28-
) {
24+
if (!in_array($request->getMethod(), ['POST', 'PUT', 'PATCH', 'DELETE', 'LINK', 'UNLINK'])) {
2925
return;
3026
}
3127

3228
$contentType = $request->headers->get('Content-Type');
33-
3429
$format = null === $contentType
3530
? $request->getRequestFormat()
3631
: $request->getFormat($contentType);
@@ -40,20 +35,20 @@ public function onKernelRequest(GetResponseEvent $event)
4035
}
4136

4237
$content = $request->getContent();
38+
if (empty($content)) {
39+
return;
40+
}
4341

44-
if (!empty($content)) {
45-
$data = @json_decode($content, true);
46-
47-
if (!is_array($data)) {
48-
throw new BadRequestHttpException('Invalid ' . $format . ' message received');
49-
}
50-
51-
$jsonSchema = $request->get('_jsonSchema');
52-
if (is_array($jsonSchema) && array_key_exists('request', $jsonSchema)) {
53-
$this->payloadValidator->validate($content, $jsonSchema['request']);
54-
}
42+
$data = @json_decode($content, true);
43+
if (!is_array($data)) {
44+
throw new BadRequestHttpException('Invalid ' . $format . ' message received');
45+
}
5546

56-
$request->request = new ParameterBag($data);
47+
$jsonSchema = $request->get('_jsonSchema');
48+
if (is_array($jsonSchema) && array_key_exists('request', $jsonSchema)) {
49+
$this->payloadValidator->validate($content, $jsonSchema['request']);
5750
}
51+
52+
$request->request = new ParameterBag($data);
5853
}
5954
}

0 commit comments

Comments
 (0)