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

[5.6] Handle more JSON errors gracefully when JSON_PARTIAL_OUTPUT_ON_ERROR is set #23410

Merged
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
21 changes: 18 additions & 3 deletions src/Illuminate/Http/JsonResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,24 @@ public function setData($data = [])
*/
protected function hasValidJson($jsonError)
{
return $jsonError === JSON_ERROR_NONE ||
($jsonError === JSON_ERROR_UNSUPPORTED_TYPE &&
$this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR));
// No error is obviously fine
if ($jsonError === JSON_ERROR_NONE) {
return true;
}

// If the JSON_PARTIAL_OUTPUT_ON_ERROR option is set, some additional errors are fine
// (see https://secure.php.net/manual/en/json.constants.php)
if ($this->hasEncodingOption(JSON_PARTIAL_OUTPUT_ON_ERROR)) {
$acceptableErrors = [
JSON_ERROR_RECURSION,
JSON_ERROR_INF_OR_NAN,
JSON_ERROR_UNSUPPORTED_TYPE,
];

return \in_array($jsonError, $acceptableErrors);
}

return false;
}

/**
Expand Down
45 changes: 36 additions & 9 deletions tests/Http/HttpJsonResponseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,49 @@ public function testSetAndRetrieveStatusCode()
}

/**
* @param mixed $data
*
* @expectedException \InvalidArgumentException
* @expectedExceptionMessage Type is not supported
*
* @dataProvider jsonErrorDataProvider
*/
public function testJsonErrorResource()
public function testInvalidArgumentExceptionOnJsonError($data)
{
$resource = tmpfile();
$response = new \Illuminate\Http\JsonResponse(['resource' => $resource]);
new \Illuminate\Http\JsonResponse(['data' => $data]);
}

public function testJsonErrorResourceWithPartialOutputOnError()
/**
* @param mixed $data
*
* @dataProvider jsonErrorDataProvider
*/
public function testGracefullyHandledSomeJsonErrorsWithPartialOutputOnError($data)
{
new \Illuminate\Http\JsonResponse(['data' => $data], 200, [], JSON_PARTIAL_OUTPUT_ON_ERROR);
}

/**
* @return array
*/
public function jsonErrorDataProvider()
{
// Resources can't be encoded
$resource = tmpfile();
$response = new \Illuminate\Http\JsonResponse(['resource' => $resource], 200, [], JSON_PARTIAL_OUTPUT_ON_ERROR);
$data = $response->getData();
$this->assertInstanceOf('stdClass', $data);
$this->assertNull($data->resource);

// Recursion can't be encoded
$recursiveObject = new \stdClass();
$objectB = new \stdClass();
$recursiveObject->b = $objectB;
$objectB->a = $recursiveObject;

// NAN or INF can't be encoded
$nan = NAN;

return [
[$resource],
[$recursiveObject],
[$nan],
];
}
}

Expand Down