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

Model state has spurious entries when binding to a collection where element type has no validators. #2446

Closed
harshgMSFT opened this issue Apr 24, 2015 · 5 comments

Comments

@harshgMSFT
Copy link
Contributor

Consider the following scenario

public Action([FromHeader(Name = "Header")]string[] headers)
{
}

Input
Header: a, b, c
In this case there should be the following keys in the model state
Header[0]
Header[1]
Header[2]

but there a spurious entry for
Header as well which should not be there.
This would be the case even for simple scenarios like so

public Action(string[] parameter)
{
}

Query string is
parameter[0]=foo

in this case there would be two entries
parameter[0]
and
parameter

@Eilon Eilon added this to the 6.0.0-beta5 milestone Apr 24, 2015
@Eilon
Copy link
Member

Eilon commented Apr 24, 2015

The model state entries should represent the structure of the posted data as opposed to how they were bound.

@Eilon
Copy link
Member

Eilon commented Apr 24, 2015

Also need to check what exactly happens today in MVC5 / MVC6 for the scenarios listed above, plus this one:

public ActionResult Foo(Person[] people)
{
}

public class Person : IValidatableObject
{
}

@harshgMSFT
Copy link
Contributor Author

MVC 5.0

For

public ActionResult Foo(Person[] people)
{
}

public class Person : IValidatableObject
{
           public string Name { get; set; }
           public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
            {
                if (Name ==  "c")
                {
                    yield return new ValidationResult("c is not allowed.");
                }

                yield break;
            }
}

if there are three people bound
with query string as ?people[0].Name=a&people[1].Name=b&people[2].Name=c
there would be FOUR entries
people[0].Name,people[1].Name,people[2].Name,people[2] - this one has an error and no value.

For the other scenario listed in the bug, MVC 5.0 does not add the key with parameter name. (adds only what is bound).

harshgMSFT added a commit that referenced this issue May 13, 2015
The assumption is ModelState should have entries if
1. An error is explicitly added by a model binder.
2. There is validation error reported while validating the model.
3. There is value bound by the model binder.

With this change there should be no extra entry other than for the cases mentioned above.

Also enabling the integration test cases.
@dougbu
Copy link
Member

dougbu commented May 13, 2015

@harshgMSFT could you please clarify the MVC 5 / 6 differences in these scenarios?

harshgMSFT added a commit that referenced this issue May 14, 2015
The assumption is ModelState should have entries if
1. An error is explicitly added by a model binder.
2. There is validation error reported while validating the model.
3. There is value bound by the model binder.

With this change there should be no extra entry other than for the cases mentioned above.

Also enabling the integration test cases.
harshgMSFT added a commit that referenced this issue May 14, 2015
The assumption is ModelState should have entries if
1. An error is explicitly added by a model binder.
2. There is validation error reported while validating the model.
3. There is value bound by the model binder.

With this change there should be no extra entry other than for the cases mentioned above.

Also enabling the integration test cases.
harshgMSFT added a commit that referenced this issue May 15, 2015
The assumption is ModelState should have entries if
1. An error is explicitly added by a model binder.
2. There is validation error reported while validating the model.
3. There is value bound by the model binder.

With this change there should be no extra entry other than for the cases mentioned above.

Also enabling the integration test cases.
harshgMSFT added a commit that referenced this issue May 15, 2015
The assumption is ModelState should have entries if
1. An error is explicitly added by a model binder.
2. There is validation error reported while validating the model.
3. There is value bound by the model binder.

With this change there should be no extra entry other than for the cases mentioned above.

Also enabling the integration test cases.
@rynowak
Copy link
Member

rynowak commented May 18, 2015

88ac4b9

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants