Validation ignores ValidationNode.Model when the node represents a model property #2784
Description
We have an integration test that demonstrates this issue. See: FromBodyAndRequiredOnValueTypeProperty_EmptyBody_JsonFormatterAddsModelStateError
In reality, this is two bugs that wipe each other out. The validation node created by the body model binder, shouldn't be used/validated/created because there was no value set on the model. However, because we look at the model property and not at the validation node, we work around the bug in the validation or don't get an extra validation error.
The call to GetPropertyExplorer@182
ignores the value in the node.
We should consider a design where these validation nodes don't get created. The extra node that was created has no information that we don't already have in the system. We really only need validation nodes when model binding uses collections.
The model looks like this:
public class Person4
{
[Required]
[FromBody]
public int Address { get; set; }
}
In this testcase, no body is submitted in the request, so the Address
property has the default value of 0
. However the model validation node for Address
is created with a null
model value. Validation then ignores the value set on the validation node and uses the property value instead.