Improve request parsing with invalid json #2066
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Slim throws a runtime exception if json Request sends "semi valid" json code.
Here you expect json parsing to be an object, array or null:
https://github.com/slimphp/Slim/blob/3.x/Slim/Http/Request.php#L1015
If i send invalid json everything is fine, because the result ist null. If i send "semi valid" json like
"foobar"
the runtime exception gets triggered.
The Problem is that the php function json_decode (called here) decodes a little more than valid json as mentioned here:
http://php.net/manual/en/function.json-decode.php
So
"foobar"
gets decoded as string, which is not an object, array or null.I think ether you should accept "semi valid" json as result like php does or you should handle it like invalid json (i think better). But a RuntimeException looks totally wrong here.
I don't know how this should be fixed. The pull request just has tests for what i would expect.