slimphp / Slim-Http

A set of PSR-7 object decorators providing useful convenience methods

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Support RFC7158 JSON body boolean

esetnik opened this issue · comments

According to https://datatracker.ietf.org/doc/html/rfc7158 it is possible to pass non-object and non-array types as json bodies. e.g. a json body containing only a primitive boolean value such as:

true

And this is supported by php's json_decode function which returns a bool given an input like:

$result = json_decode('true');

But ServerRequest requires an array to be returned by json_decode for the json media type.

$this->registerMediaTypeParser('application/json', function ($input) {
$result = json_decode($input, true);
if (!is_array($result)) {
return null;
}
return $result;

That is correct, however if you look here:

'Request body media type parser return value must be an array, an object, or null'

The reason is that the PSR-7 getParsedBody() method can only return array|object|null :

/**
 * Retrieve any parameters provided in the request body.
 *
 * If the request Content-Type is either application/x-www-form-urlencoded
 * or multipart/form-data, and the request method is POST, this method MUST
 * return the contents of $_POST.
 *
 * Otherwise, this method may return any results of deserializing
 * the request body content; as parsing returns structured content, the
 * potential types MUST be arrays or objects only. A null value indicates
 * the absence of body content.
 *
 * @return null|array|object The deserialized body parameters, if any.
 *     These will typically be an array or object.
 */
public function getParsedBody();