-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Conversation
@@ -48,7 +47,7 @@ public BodyModelBinder() | |||
return new ModelBindingResult(model: null, key: bindingContext.ModelName, isModelSet: false); | |||
} | |||
|
|||
object model = null; | |||
object model; |
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.
You can safely move this all of the way into the try
635bd34
to
6e21c2c
Compare
Updated with some dead code cleanup + new functional tests |
} | ||
catch (Exception ex) | ||
{ | ||
model = GetDefaultValueForType(bindingContext.ModelType); | ||
bindingContext.ModelState.AddModelError(bindingContext.ModelName, ex); | ||
|
||
// This model binder is the only handler for the Body binding source. | ||
// Always tell the model binding system to skip other model binders i.e. return non-null. | ||
return new ModelBindingResult(model: null, key: bindingContext.ModelName, isModelSet: false); |
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.
This was my bad. I think the first parameter here was supposed to be assigned the default value for type model calculated above?
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.
- For action parameters,
MethodInfo.Invoke
seems to take care of coercing null value types to default values for value types (this is verified by test). - For value type properties that are annotated with
[FromBody]
, we end up getting a null ref as part of the property setter. Tracking this via Value type controller properties with [From*] fail with NullRef exception when no value is available #2357.
That said, it seems like fixing it specifically for BodyModelBinder
is incorrect and we should have a single top level piece of code that addresses this, not individual binders.
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.
👍
@@ -127,6 +127,43 @@ public class InputFormatterTests | |||
} | |||
|
|||
[Theory] | |||
[InlineData("\"I'm a JSON string!\"")] | |||
[InlineData("true")] | |||
public async Task JsonInputFormatter_ReturnsDefaultValue_ForValueTypes(string input) |
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.
How about an empty string content? that should be treated as a null value and a model state error should be added by JsonInputformatter
⌚ on one test case. |
Follow up work item to #2269
6e21c2c
to
43e24b2
Compare
Updated. |
Follow up work item to #2269