Camel case not default for Model validation #5590
Comments
It's true the keys added to Could you clarify why the line you added insufficient or perhaps an undue burden to adjust the system to match your preferences? |
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. |
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. |
Here you go. camel-case-issue |
@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. |
@johnathan-walker I'm closing this as a duplicate of the #4842 discussion. Had thought you were reporting a bug. |
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. |
FYI the JSON serializer doesn't special case |
Please see #4969 as well as discussion of the intentional distinction between code and data in #4842. As I said earlier, MVC treates |
@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 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. |
Have to agree this is really an issue. It shouldn't be hard to fix. |
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).
where it should actually be
The text was updated successfully, but these errors were encountered: