Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JsonConverter Deserializing Polymorphic List and ModelState Error Context #4506

Closed
michaelvannevel opened this issue Dec 7, 2018 · 3 comments
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates question
Milestone

Comments

@michaelvannevel
Copy link

michaelvannevel commented Dec 7, 2018

Description

When using a Polymorphic JsonConverter on a list of objects in a request to an Asp.Net Core Web app ModelStateValidation for properties of the objects in that list lose their context. For example if i have an object Person and that object has a property Friends which is a List, if I set the Gender enum to an invalid enum value when using JsonConverter i get a response like:

{
    "gender": [
        "The input was not valid."
    ]
}
but if you take the JsonConverter off of the model you get:
{
    "friends[0].gender": [
        "The input was not valid."
    ]
}

I tried to place JsonConverters in the AddJsonOptions extension method on IServiceCollection, or directly on the model with the same result, not that I expected it to change.

To Reproduce

  1. Using this version of ASP.NET Core 2.1 or 2.2
  2. Open the solution contained inside of the Stuffies.zip file.
  3. Hit Debug
  4. Make a request via postman or whatever tool you like and post to https://localhost:5001/api/values.
    using the following json:
{
	"name":"Bob Jones",
	"gender":"Male",
	"friends":[
		{
			"name":"Steven",
			"gender":"cat"
		}
	]
}
  1. You'll see that the context of the response exists in the response: friends[0].gender
  2. Now go into the ValuesController and uncomment the Attributes on the Person class
  3. Debug again and do the same Post
  4. See the differences in the previously mentioned responses between when you add or remove the Attributes from the Person class. Now the response will have : gender without the context.

Expected behavior

I expected the response to have the context on the ModelState error.

Screenshots/Sample Project

If applicable, add screenshots to help explain your problem.
stuffies
Stuffies.zip

@Eilon Eilon added the area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates label Dec 13, 2018
@mkArtakMSFT
Copy link
Member

Thanks for contacting us, @michaelvannevel.
@dougbu, can you please look into this? Thanks.

@mkArtakMSFT
Copy link
Member

Hi. It looks like this is a question about how to use ASP.NET Core. While we do our best to look through all the issues filed here, to get a faster response we suggest posting your questions to StackOverflow using the asp.net-core-mvc tag.

@ghost
Copy link

ghost commented Dec 7, 2019

Thank you for contacting us. Due to no activity on this issue we're closing it in an effort to keep our backlog clean. If you believe there is a concern related to the ASP.NET Core framework, which hasn't been addressed yet, please file a new issue.

@ghost ghost closed this as completed Dec 7, 2019
@ghost ghost locked as resolved and limited conversation to collaborators Dec 7, 2019
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-mvc Includes: MVC, Actions and Controllers, Localization, CORS, most templates question
Projects
None yet
Development

No branches or pull requests

5 participants