-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Creating correct model state keys when body model binding for missing properties or body. Also adds integration tests #2396
Conversation
@@ -13,5 +13,8 @@ | |||
<PropertyGroup> | |||
<SchemaVersion>2.0</SchemaVersion> | |||
</PropertyGroup> | |||
<ItemGroup> | |||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" /> | |||
</ItemGroup> |
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.
not sure why this shows up.
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 is fine. just means it is a test project. check it in separately, together w/ the rest of this PR, or whatever...
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.
In general just let VS do what it's going to do.
@harshgMSFT it isn't clear what the expected behaviour is in any of the cases you've outlined or why the current behaviour is incorrect. please describe the exact Asp.Net 4 and MVC 5 behaviours for these cases. separately what reported issues (if any) will this PR fix? |
|
||
namespace Microsoft.AspNet.Mvc.Core | ||
{ | ||
public class TestMvcOptions : IOptions<MvcOptions> |
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.
name of class and containing file must match
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.
Will fix.
@dougbu I have updated the comment to provide a better description. And no there is no issue for this one. |
return modelBindingResult.Model; | ||
} | ||
|
||
return null; |
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 is a good refactor. I had scoped out something similar thinking about building these tests.
8a10233
to
49b2eb9
Compare
@@ -51,8 +51,13 @@ public BodyModelBinder() | |||
{ | |||
var model = await formatter.ReadAsync(formatterContext); | |||
|
|||
// key is empty to ensure that the model name is not used as a prefix for validation. | |||
return new ModelBindingResult(model, key: string.Empty, isModelSet: true); | |||
var isTopLevelObject = bindingContext.ModelMetadata.ContainerType == null; |
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.
Didn't we conclude in another discussion that this kind of logic is incorrect, and we can only really know this by adding it back to the context?
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.
Yes, however that is a corner scenario and we should address this when we address that issue.
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.
@@ -63,6 +63,41 @@ public class DefaultControllerActionArgumentBinder : IControllerActionArgumentBi | |||
return actionArguments; | |||
} | |||
|
|||
public async Task<ModelBindingResult> BindModelAsync( |
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 class has a <summary/>
but no member-level doc comments. prefer if you add comments for new public
method but at least open a "task" issue to do the necessary.
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.
Will add doc comments for the public api surface for this class in a separate pr ( not holding this one for furhter review (as you have already signed off)). Not opening individual issues for adding doc comments, there are tons of places which needs fixing and this could be tracked by a singular issue.
with the perfect doc comment addition 😸 |
49b2eb9
to
ec21283
Compare
ec21283
to
53ef825
Compare
The model state keys for body bound models which are bound at property will use the entire model name with this change for example
Consider
Request body { "Zip" : 12345 }
In this case the error key would be "prefix.Address.Street" (assuming there is a prefix because of additional metadata/positioning for/of the Person model).
Request body { }
In this case the prefix gets ignored and the error key is Name.
Please note this is so that we are compatible with MVC 5.0
Request body { }
In both these cases (Action and Action2) the prefix gets ignored and the error key is Name.
This is a slight improvement from mvc, as in MVC the action parameter would be null.
The followup for this would be to fix #2416 -
This PR ignores the validation assuming that #2416 will address the issues and update the test.
NOTE: previous versions of mvc did not have property binding and hence there is no precedence in this case. For MVC and Web API it was possible to body bind an action parameter which used an empty prefix instead of a parameter name for adding errors to model state (In case of MVC if a custom prefix was provided, it failed binding from body i.e the parameter was null).