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

InputFormatter errors reported by JsonContractResolver should use the entire model prefix as the model state key. #2416

Closed
harshgMSFT opened this issue Apr 20, 2015 · 0 comments

Comments

@harshgMSFT
Copy link
Contributor

Consider the following two examples :

Example 1

   public class Person
   {
        [FromBody]
        [Required]
        public Address Address { get; set; }
   }

   public class Address
   {
         public int Zip { get; set; }
   }

Request body ""
In this case the error key would be "" and not "prefix.Address" (assuming there is a prefix because of additional metadata/positioning for/of the Person model).
Also the model state dictionary incorrectly has two keys
Key 1 : ""
Key 2 : Address -> This gets added because the validator looks for a key Address and not "" to determine if validation has been done for property Address.
In this case there should have been only two keys Address and it should be invalid.

Example 2

   public class Person
   {
        [FromBody]
        public Address Address { get; set; }
   }

   public class Address
   {
        public string Street { get; set; }

        [Required]
         public int Zip { get; set; }
   }

Request body { "Street" : "Some street" }
In this case the error key would be "Street" and not "prefix.Address.Street" (assuming there is a prefix because of additional metadata/positioning for/of the Person model).
Also the model state dictionary incorrectly has three keys
Key 1 : ""
Key 2 : Street
Key 3: Zip -> This gets added as a valid entry because the validator looks for a key Zip and not "" to determine if validation has been done for property Zip.
In this case there should have been only two keys Address.Street and Address.Zip and the second one should be invalid.

The fix for this we would have to make the model state keys consistent with the rest of the model binding system. In all cases it should add the full prefixed version as the error key.

The fix to this might require #2330 to be fixed.

harshgMSFT added a commit that referenced this issue Apr 22, 2015
…y will use the entire model name with this change for example

Consider

public class Person
{
    [FromBody]
    public Address Address { get; set; }
}

public class Address
{
   [Required]
   public string Street { get; set; }

   public int Zip { get; set; }
}

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).

public class Person
{
       [Required]
       public string Name { get; set; }
}

public void Action([FromBody]Person p)
{
}
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

public class Person
{
       [Required]
       public string Name { get; set; }
}

public void Action([FromBody][ModelBinder(Name = "prefix")] Person p)
{
}

public void Action2([FromBody][Bind(Name = "prefix")] Person p)
{
}
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).
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant