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

Camel case not default for Model validation #5590

Closed
bryjshed opened this issue Dec 2, 2016 · 14 comments
Closed

Camel case not default for Model validation #5590

bryjshed opened this issue Dec 2, 2016 · 14 comments

Comments

@bryjshed
Copy link

bryjshed commented Dec 2, 2016

MVC serializes JSON with camel case by default. However, it doesn't seem true for Model validation and still requires. .AddJsonFormatters(j => j.ContractResolver = new CamelCasePropertyNamesContractResolver())

Otherwise, the validation errors come back in pascal case as seen below on BadRequest(ModelState).

{
  "FirstName": [
    "The FirstName field is required."
  ]
}

where it should actually be

{
  "firstName": [
    "The FirstName field is required."
  ]
}
@dougbu
Copy link
Member

dougbu commented Dec 3, 2016

It's true the keys added to ModelState are generally based on C# properties (usually Pascal-cased) and not the incoming data. But names are matched case-insensitively in model binding and validation and the ModelStateDictionary does case-insensitive lookups.

Could you clarify why the line you added insufficient or perhaps an undue burden to adjust the system to match your preferences?

@bryjshed
Copy link
Author

bryjshed commented Dec 6, 2016

Adding the line isn't an undue burden but we assumed since camel case was the default json formatter and this line wouldn't be needed. When any other object is serialized to json which has pascal-cased properties it correctly uses the default formatter which is camel case. However, except for the returned validation result errors json.

@dougbu
Copy link
Member

dougbu commented Dec 6, 2016

Please describe your scenario in more detail, including the validation errors when submitted JSON has unexpected key casing. A small repro project hosted in a public GitHub repo would be ideal.

@bryjshed
Copy link
Author

bryjshed commented Dec 6, 2016

Here you go. camel-case-issue

@dougbu
Copy link
Member

dougbu commented Dec 7, 2016

@bryjshed your tests show that the service is indeed configured with the expected output formatter configuration. However, asserting specific key casing doesn't demonstrate an end-to-end problem.

Are you using a client-side JSON parser that is case-sensitive? If yes, configure the service to match the exact keys your client expects.

@j7nw4r
Copy link

j7nw4r commented Dec 7, 2016

@dougbu I don't really think that the need for case-sensitivity is the issue. There is a work around. I think the issue is that this is unexpected behavior. As described here: #4842

@dougbu
Copy link
Member

dougbu commented Dec 7, 2016

@johnathan-walker I'm closing this as a duplicate of the #4842 discussion. Had thought you were reporting a bug.

@j7nw4r
Copy link

j7nw4r commented Dec 7, 2016

The expected behavior for serializing ModelState is camel case. The actual behavior is pascal. I think that is a bug. #4842 announces the functionality that we would expect.

@j7nw4r
Copy link

j7nw4r commented Dec 7, 2016

@dougbu

@dougbu
Copy link
Member

dougbu commented Dec 7, 2016

FYI the JSON serializer doesn't special case ModelState and it's handled like any other dictionary.

@dougbu dougbu closed this as completed Dec 7, 2016
@ajsebas
Copy link

ajsebas commented Dec 7, 2016

@dougbu This should really be a bug (since #4842 announces that the default casing in camel and in this case it is not) and fixed in upcoming releases, although there are workarounds as pointed out by @johnathan-walker and @bryjshed

@dougbu
Copy link
Member

dougbu commented Dec 7, 2016

Please see #4969 as well as discussion of the intentional distinction between code and data in #4842.

As I said earlier, MVC treates ModelState like any other dictionary. Feel free however to file a feature request for a special case in this area. Note it would be somewhat strange because ModelState keys come from both C# property names and users' submitted dictionary keys.

@nil4
Copy link

nil4 commented Dec 8, 2016

@dougbu C# property names are an internal implementation detail of the API code, and should not be exposed verbatim to the API clients. I would argue that the same justification used to default to camel case should be applied here.

In other words, as an API client, when I submit a payload containing key, and it is incorrect, I don't expect to receive validation errors referring to a different property Key. I didn't send that and I should not be expected to map it back to the key name I sent to make sense of it.

For Javascript apps running in a browser, Javascript property lookups are case sensitive. Doing case-insensitive lookups is not trivial nor very performant.

And it shouldn't be necessary either if this issue is fixed.

@MaikuMori
Copy link

Have to agree this is really an issue. It shouldn't be hard to fix.

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

6 participants