-
Notifications
You must be signed in to change notification settings - Fork 18
Don't throw exception on empty JSON body #29
Conversation
`2.0.3` introduced an issue where a request along the lines of `PATCH /foo` with `Content-Type: application/json` but no actual request body (since there may not actually be any information to provide) would result in an exception. The previous behavior was that the request would be allowed and the Parsed Body would be NULL.
@xtreamwayz @weierophinney can I get some feedback on this before merging please? |
Path is used for partial updating a partial resource. Doesn't that mean that there should always be data? If there is no data, it can't update a resource and should be considered an invalid request. |
Not necessarily. The request could be something like: Sort of like: That's how this issue arose, actually. The request does modify the entity, so |
I wouldn't require The question is would this be considered a valid endpoint? To me it wouldn't. I would write it as you did: |
RFC 7159 states:
The wording is a bit unfortunate as it implies that NULL and booleans are OK, but it does seem to insist that Which makes something like this perfectly valid:
(which looks horrendous imo with that Prior to Would really like your input on this as well @weierophinney :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The only change I'd make is s/!empty/! empty/
; that'll eventually be caught with the updated coding standard, however.
In terms of your arguments, I tend to agree with your assertion that an empty body should result in null
for the parsed body, and not an exception. Body content for any HTTP method is always considered optional, regardless of the content type provided during the request.
I might suggest one optimization: perhaps skip parsing if the $rawBody
is empty to begin with?
@weierophinney I'm inclined to leave the parsing as-is for two reasons:
I'm fine with it if you don't think the concerns are warranted, though, but in the meantime I'll merge this since you already approved it. |
2.0.3
introduced an issue where a request along the lines ofPATCH /foo
withContent-Type: application/json
but no actual request body (since there may notactually be any information to provide) would result in an exception. The
previous behavior was that the request would be allowed and the Parsed Body
would be NULL.
@weierophinney this pertains to what we discussed briefly via e-mail a while back.
Other possible alternatives would be to either require the user to use
text/plain
(or something else) as the content type when not needing to provide a request body in order for theJsonStrategy
to not be executed (which would be a bit of a hassle if 100% of all other requests wereapplication/json
) OR to require the user to provide a valid empty JSON structure ({}
,'""'
, etc...), which would also be a bit of a bother, since the intent seems clear ("do this thing which doesn't require any further information to be provided as input from me").I opted for
NULL
as the resulting parsed body rather than an empty array or empty object type sinceNULL
was the end result prior to2.0.3
, and I figure that's one fewer vector for BC breakage.