Skip to content

Commit 0af2fe7

Browse files
committed
StandardServer: a bit more validation for parsed json PSR-7 request (related to #202)
1 parent 9d37f4c commit 0af2fe7

File tree

1 file changed

+9
-4
lines changed

1 file changed

+9
-4
lines changed

src/Server/Helper.php

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,16 +493,21 @@ public function parsePsrRequest(ServerRequestInterface $request)
493493
);
494494
}
495495

496+
// Try parsing ourselves if PSR-7 implementation doesn't parse JSON automatically
497+
if (is_array($bodyParams) && empty($bodyParams)) {
498+
$bodyParams = json_decode($request->getBody(), true);
499+
500+
if (json_last_error()) {
501+
throw new RequestError("Could not parse JSON: " . json_last_error_msg());
502+
}
503+
}
504+
496505
if (!is_array($bodyParams)) {
497506
throw new RequestError(
498507
"GraphQL Server expects JSON object or array, but got " .
499508
Utils::printSafeJson($bodyParams)
500509
);
501510
}
502-
503-
if (empty($bodyParams)) {
504-
$bodyParams = json_decode($request->getBody(), true);
505-
}
506511
} else {
507512
$bodyParams = $request->getParsedBody();
508513

0 commit comments

Comments
 (0)