Reopen 7439 - ModelState json serialization should be camel cased
See original GitHub issueDescribe the bug
The issue 7439 was not completely addressed, the bot blocked the issue from commenting, but I think this main problem should be addressed:
As is, Net Core responds almost everything as camel case, except for model validations, which I think is odd:
{
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|706e0133-46db8fce16cb675d.",
"errors": {
"Email": [
"The Email field is not a valid e-mail address."
],
"Password": [
"The field Password must be a string or array type with a minimum length of '8'."
]
}
}
As you see, all the properties are camel case but the field names, a workaround is to use CustomProblemDetailsFactory from the comment in the same issue.
To Reproduce
Create a project and a controller with a model with validations.
Make sure the controller is annotated with [ApiController], so validation is handled automatically.
Example:
[Authorize]
[ApiController]
[Route("Api/Address/[controller]")]
public class ColonyController : Controller
{
[HttpPost]
[Route("[action]")]
public Task<int> Create([FromBody]ColonyToAddDto colonyToAddDto)
{
return Task.FromResult(0);
}
}
With the model
public class ColonyToAddDto
{
[Required]
[StringLength(300, MinimumLength = 3)]
public string Name { get; set; }
[Range(1, 99999)]
public int PostalCode { get; set; }
[Range(1, double.PositiveInfinity)]
public int MunicipalityId { get; set; }
}
Further technical details
- ASP.NET Core version 3.1
Issue Analytics
- State:
- Created 4 years ago
- Reactions:12
- Comments:17 (6 by maintainers)
Top Results From Across the Web
How to set Modelstate error keys to camel case?
How do I set the modelstate keys to camel case in WEB Api .net framework. I use JsonProperty attribute to set the property...
Read more >ASP.NET Core – Case Style Conversion - 兴杰
Reopen 7439 - ModelState json serialization should be camel cased. 又过了一年后, 大部分无知的人终于意识到这根本是一个bug.
Read more >Setting JSON Serialization Configuration At Runtime On A ...
We pass in our naming strategy options, saying that the default should be CamelCase, the header should be “json-naming-strategy”, for our ...
Read more >https://huggingface.co/dennishe97/bertoverflow-v2/...
diff --git a/tokenizer.json b/tokenizer.json new file mode 100644--- ... + "serialize": 6977, + "physical": 6978, + "encountered": 6979, + "##case": 6980, ...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

I do not agree with the labels applied to this issue, as @TanvirArjel says:
@javiercn @pranavkm can I implore that you reconsider the
affected-fewandseverity-nice-to-havelabels and re-apply it as a bug? This issue is still present in .NET 5 and clearly does not honor theDictionaryKeyPolicy = JsonNamingPolicy.CamelCasepolicy. I think it is safe to say the majority of users consuming an API from javascript expects camelcase properties, so not sure why you would think it only affects a few?See also the following two issues which has been closed by bot:
https://github.com/dotnet/aspnetcore/issues/7439 https://github.com/dotnet/runtime/issues/31849
How can the below “Email” and “Password” keys not be considered a bug?