Skip to content
This repository was archived by the owner on Dec 14, 2018. It is now read-only.
This repository was archived by the owner on Dec 14, 2018. It is now read-only.

Validation ignores ValidationNode.Model when the node represents a model property #2784

Closed
@rynowak

Description

@rynowak

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.

See: https://github.com/aspnet/Mvc/blob/dev/src/Microsoft.AspNet.Mvc.Core/ModelBinding/Validation/DefaultObjectValidator.cs#L182

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions