question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Reopen 7439 - ModelState json serialization should be camel cased

See original GitHub issue

Describe 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:closed
  • Created 4 years ago
  • Reactions:12
  • Comments:17 (6 by maintainers)

github_iconTop GitHub Comments

10reactions
antonioortizpolacommented, Oct 8, 2021

I do not agree with the labels applied to this issue, as @TanvirArjel says:

  • This is a bug, not an enhancement
  • This does not affect a few, it affects anyone with a SPA wanting to use validation with the JSON standard camel-case.
  • It is not a nice a to have, it is a bug that breaks apps and the docs, and needs time and research to apply workarounds for something really, really basic
10reactions
bcronjecommented, Mar 1, 2021

@javiercn @pranavkm can I implore that you reconsider the affected-few and severity-nice-to-have labels and re-apply it as a bug? This issue is still present in .NET 5 and clearly does not honor the DictionaryKeyPolicy = JsonNamingPolicy.CamelCase policy. 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?

{
  "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'."
    ]
  }
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found