-
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
Inferring FromBody.AllowEmptyBehavior = Allow
based nullability information
#39754
Comments
Thanks for contacting us. We're moving this issue to the |
This looks great.
Super unrelated to this change, but could we update the check to use the feature instead of relying on
|
Actually, I was looking at exactly that because of this issue #29570. Thanks for mention that. |
I have been talking with @halter73 offline and one aspect of this change that we agreed is that having a mechanism to infer the EmptyBody based on optionality even when the cc: @pranavkm. |
After, design conversations the final the infer mechanism will allow empty body only when: modelMetadata.NullabilityState == NullabilityState.Nullable ||
modelMetadata.IsNullableValueType ||
modelMetadata.HasDefaultValue That means the following scenario will still not allowing empty body as default, unless the default is changed by the current options available: #nullable disable
public IActionResult Ref([FromBody] TestClass val) => Ok(val);
#nullable restore |
Current design
Today, MVC infer optionality for parameters/properties based on nullability or if they have default value for all Binding Sources, eg.:
Or
All examples above will allow the
null
value for the parametername
, however, when the bindingFrom Body
(inferred or not) the current default behavior isfail
when theContent-Length == 0
.Currently, are available 2 options to override this behavior and any of those available options are based on the
Nullability
information and need to be explicitly specified by the user.Globally:
Scoped:
Proposed Change
As part of the idea to bring
Minimal APIs
enhancements toMVC
the proposal is applying the same default behavior and allow empty input body (Content-Length = 0
) when the parameter/property is nullable or has the default value.The suggestion is to always allow empty input (
Content-Length = 0
) when a[FromBody]
parameter/property sets a default value or is nullable without allowing or requiring configuration.After this change the following scenarios will allow an empty input:
Usage Examples
If the users are affected by this change and they would like a different behavior, the simplest way to avoid it is to define the action parameter/property correctly based on the optionality, as the examples listed below.
Required parameter in a disabled nullable context
Before
After
Required parameter in an enabled nullable context
Before
After
The text was updated successfully, but these errors were encountered: