-
Notifications
You must be signed in to change notification settings - Fork 10k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JSON "null" request body rejected as an empty request body #40415
Comments
@madelson we are changing the EmptyBody Behavior inference for nullable or parameters with default value in .NET 7 Preview 3 #39754, and both use cases (empty body and JSON null) will be treated as the same (invoke the action with null model). Can you share why you are accepting JSON null and not empty bodies? |
To be clear, we aren't forced into this nor are we very strongly opinionated regarding it. In general, it feels strange to me not to differentiate between a valid JSON payload consisting of the On the other hand, if the |
@madelson I understand you point, however, since you have an action with a nullable parameter or a parameter with a default value, this parameter is actually optional, that means even if the caller mistakes and failed to send the request body your API is accepting it because the parameter is not even required. That is what happens if you have a nullable/default value parameter As I mentioned, your scenario is fixed in .NET 7 Preview 3 where both will be accepted, however, if you have a non-nullable type /or without default value the request will be rejected with the message that you mentioned "A non-empty request body is required" that I agree that could not be the best for the JSON "null" since the body is not empty but that is how MVC works right now. |
Hi @madelson. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time. |
A parameter with a default value should definitely be treated as optional. I think it's less obvious that nullable parameters are automatically optional (certainly they are not optional in C#, for example).
A difference here is that query strings do not have a native encoding for null whereas JSON does so it is possible to differentiate between a value that is null and a value that is missing in JSON. In the HTTP world generally, does it make sense/is it conventional to send an empty request body (which cannot be parsed as JSON) with a content type header that implies JSON encoding? I'm honestly not sure.
Moreso than the above points I would consider this a bug that ought to be fixed. Had the message been correct it would have saved me saved me some debugging time. Do you agree? |
@madelson I agree that the message is not obvious and should not be the same as Also, it will not be, unfortunately, a high priority right now but if you have time to contribute will be a pleasure helping you to make it works. |
Thanks for contacting us. We're moving this issue to the |
Sounds reasonable. Thanks @brunolins16 ! |
@madelson have You started working on this, or is it up for grabs still? |
@senbar go for it. I haven't picked it up. |
Technically they don't have a native encoding at all. One convention for representing The former is not supported entirely as such by ASP.NET, afaik. (Well; at least it's consistently inconsistent with the rest of the world 🤷♂️ ) |
What's the error message I should put in? Is something like "Null is not permitted in request body" fine? |
Thanks for contacting us. We're moving this issue to the |
Is there an existing issue for this?
Describe the bug
I have an action method which accepts a posted
null
value for the body:However, when I post a JSON null to this endpoint I get a 400 response complaining that the body is empty.
Expected Behavior
Since the body is not empty and JSON-deserializes to the model parameter type just fine, I expect it to invoke the action with a null model.
If it can't do that, I at least expect an error message that indicates nulls are not permitted rather than one claiming the body is empty (although if the body were actually empty instead of valid JSON I'd probably want this request to be rejected).
Steps To Reproduce
Call the above endpoint with:
Exceptions (if any)
No response
.NET Version
6.0.100
Anything else?
The workaround I found is to set
[FromBody(EmptyBodyBehavior = EmptyBodyBehavior.Allow)]
, but I don't really want to have to do this because I'd like to reject empty bodies and accept non-empty bodies which are JSON null.The text was updated successfully, but these errors were encountered: