limoncello-php / app

Quick start JSON API application

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Bad argument type in Exception/JsonApiHandler to construct a JsonApiResponse

medbenhenda opened this issue · comments

Class : App\Exceptions\JsonApiHandler
Method: private function handle
Variable: $corsHeaders
the current code is:

$corsHeaders = $container->has(CorsStorageInterface::class) === true ?
$container->get(CorsStorageInterface::class)->getHeaders() : [];

but in some case the result of $container->get(CorsStorageInterface::class)->getHeaders() is null.

As result

$corsHeaders = null

Then we construct a JsonApiResponse with the thirs vaariable $corsHeaders which is null , but the onstructor requires that the third argument should be an array

$response = new JsonApiResponse($content, $httpCode, $corsHeaders);

The solution:

if ($corsHeaders === null) {
    $corsHeaders = array();
}
 $response    = new JsonApiResponse($content, $httpCode, $corsHeaders);

Thanks for found bug. Interface CorsStorageInterface has method for checking headers but it practically does the same thing as your proposal.
I've added fix and some tests to cover handle method.