Skip to content

Commit e7976a8

Browse files
committed
ACP2E-3467: [Cloud] 500 response to empty Graphql response on 2.4.7
1 parent 196ab50 commit e7976a8

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed

app/code/Magento/GraphQl/Controller/GraphQl.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Magento\Framework\App\ResponseInterface;
2020
use Magento\Framework\Controller\Result\JsonFactory;
2121
use Magento\Framework\GraphQl\Exception\ExceptionFormatter;
22+
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
2223
use Magento\Framework\GraphQl\Query\Fields as QueryFields;
2324
use Magento\Framework\GraphQl\Query\QueryParser;
2425
use Magento\Framework\GraphQl\Query\QueryProcessor;
@@ -208,7 +209,7 @@ public function dispatch(RequestInterface $request): ResponseInterface
208209
);
209210
$statusCode = 200;
210211
}
211-
} catch (SyntaxError $error) {
212+
} catch (SyntaxError|GraphQlInputException $error) {
212213
$result = [
213214
'errors' => [FormattedError::createFromException($error)],
214215
];
@@ -245,7 +246,7 @@ private function getDataFromRequest(RequestInterface $request): array
245246
{
246247
/** @var Http $request */
247248
if ($request->isPost()) {
248-
$data = $this->jsonSerializer->unserialize($request->getContent());
249+
$data = $request->getContent() ? $this->jsonSerializer->unserialize($request->getContent()) : [];
249250
} elseif ($request->isGet()) {
250251
$data = $request->getParams();
251252
$data['variables'] = isset($data['variables']) ?

dev/tests/integration/testsuite/Magento/GraphQl/Controller/GraphQlControllerTest.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ public function testDispatchWithOptions(): void
273273
self::assertEmpty($response->getContent());
274274
}
275275

276-
public function testDispatchWithoutQuery(): void
276+
public function testDispatchWithGetWithoutQuery(): void
277277
{
278278
$this->request->setPathInfo('/graphql');
279279
$this->request->setMethod('GET');
@@ -282,4 +282,32 @@ public function testDispatchWithoutQuery(): void
282282
$output = $this->jsonSerializer->unserialize($response->getContent());
283283
self::assertNotEmpty($output['errors']);
284284
}
285+
286+
public function testDispatchWithPostAndWrongContentType(): void
287+
{
288+
$query = <<<QUERY
289+
{
290+
products(filter: {sku: {eq: "simple1"}}) {
291+
items {
292+
id
293+
name
294+
sku
295+
}
296+
}
297+
}
298+
QUERY;
299+
$postData = [
300+
'query' => $query,
301+
'variables' => null,
302+
'operationName' => null
303+
];
304+
305+
$this->request->setPathInfo('/graphql');
306+
$this->request->setMethod('POST');
307+
$this->request->setContent(json_encode($postData));
308+
$response = $this->graphql->dispatch($this->request);
309+
self::assertEquals(400, $response->getStatusCode());
310+
$output = $this->jsonSerializer->unserialize($response->getContent());
311+
self::assertNotEmpty($output['errors']);
312+
}
285313
}

0 commit comments

Comments
 (0)