Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

4336: Return 400 error on invalid JSON sent to the datastore API #4341

Merged
merged 3 commits into from
Jan 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions modules/datastore/src/Controller/AbstractQueryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,9 +327,14 @@ public static function getJson(Request $request) {
*/
public static function fixTypes($json, $schema) {
$data = json_decode($json);
$validator = new Validator();
$validator->coerce($data, json_decode($schema));
return json_encode($data, JSON_PRETTY_PRINT);

if ($data !== NULL) {
$validator = new Validator();
$validator->coerce($data, json_decode($schema));
return json_encode($data, JSON_PRETTY_PRINT);
}

throw new \InvalidArgumentException("Invalid JSON");
}

/**
Expand Down
3 changes: 3 additions & 0 deletions modules/datastore/tests/data/query/invalidJson.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"foo": bar,
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testGetNormalizer() {
}

/**
* Make sure we get what we expect with a post
* Make sure we get what we expect with a POST
*/
public function testPostNormalizer() {
$sampleJson = $this->getSampleJson();
Expand All @@ -36,7 +36,7 @@ public function testPostNormalizer() {
}

/**
* Make sure we get what we expect with a patch
* Make sure we get what we expect with a PATCH
*/
public function testPatchNormalizer() {
$sampleJson = $this->getSampleJson();
Expand All @@ -48,7 +48,7 @@ public function testPatchNormalizer() {
}

/**
* Make sure we get what we expect with a delete
* Make sure we get what we expect with a DELETE
*/
public function testDeleteNormalizer() {
$this->expectExceptionMessage("Only POST, PUT, PATCH and GET requests can be normalized");
Expand All @@ -59,7 +59,7 @@ public function testDeleteNormalizer() {
}

/**
* Make sure we get what we expect with a put
* Make sure we get what we expect with a PUT
*/
public function testPutNormalizer() {
$sampleJson = $this->getSampleJson();
Expand All @@ -70,10 +70,26 @@ public function testPutNormalizer() {
$this->assertEquals($requestJson, $sampleJson);
}

/**
* Make sure we get what we expect with invalid JSON.
*/
public function testInvalidJson() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid JSON');
$sampleJson = $this->getBadJson();
$schema = $this->getSampleSchema();
$request = Request::create("http://example.com", "POST", [], [], [], [], $sampleJson);
AbstractQueryController::getPayloadJson($request, $schema);
}

private function getSampleJson() {
return file_get_contents(__DIR__ . "/../../../data/query.json");
}

private function getBadJson() {
return file_get_contents(__DIR__ . "/../../../data/query/invalidJson.json");
}

private function getSampleSchema() {
return file_get_contents(__DIR__ . "/../../../data/querySchema.json");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -339,7 +339,19 @@ public function testStreamedBadSchema() {
}

/**
* Create a mock object for the main container passed to the controller.
* Make sure we get what we expect with invalid JSON.
*/
public function testInvalidJson() {
$this->expectException(\InvalidArgumentException::class);
$this->expectExceptionMessage('Invalid JSON');
$sampleJson = $this->getBadJson();
$schema = $this->getSampleSchema();
$request = $this->mockRequest($sampleJson);
QueryDownloadController::getPayloadJson($request, $schema);
}

/**
* Create a mock chain for the main container passed to the controller.
*
* @param int $rowLimit
* The row limit for a query.
Expand Down Expand Up @@ -493,4 +505,12 @@ protected function getBuffer($buffer) {
$this->buffer .= $buffer;
}

private function getBadJson() {
return file_get_contents(__DIR__ . "/../../../data/query/invalidJson.json");
}

private function getSampleSchema() {
return file_get_contents(__DIR__ . "/../../../data/querySchema.json");
}

}