slimphp / Slim-Http

A set of PSR-7 object decorators providing useful convenience methods
MIT License
151 stars 46 forks source link

Support RFC7158 JSON body boolean #167

Closed esetnik closed 3 years ago

esetnik commented 3 years ago

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.

https://github.com/slimphp/Slim-Http/blob/90c44f2014e9072be52e2107c78d71ac35f8103e/src/ServerRequest.php#L56-L63

l0gicgate commented 3 years ago

That is correct, however if you look here: https://github.com/slimphp/Slim-Http/blob/90c44f2014e9072be52e2107c78d71ac35f8103e/src/ServerRequest.php#L196

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();