Change MVC's use of System.Text.Json to case-insensitive #10723
Labels
area-mvc
Includes: MVC, Actions and Controllers, Localization, CORS, most templates
enhancement
This issue represents an ask for new feature or an enhancement to an existing one
Milestone
Edit: if you arrived here via twitter then Hi! Thanks for coming. I wrote the initial bug report last night pretty late so some adding some information and context.
History and context
MVC gets its existing JSON support through Newtonsoft.Json (thanks @JamesNK). Newtonsoft.Json provides options to configure canonicalization of names and case-sensitivity. Newtonsoft.Json's default settings are to leave your property names as-is and to compare property names case-insensitively.
MVC however configures Newtonsoft.Json to canonicalize property names as camelCase. The effect of this is:
MVC's choice of a different default than what Newtonsoft.Json users makes sense to us because MVC is for the web (web sites, APIs). If you're using JSON on the web, you're doing to so either:
In either of these cases, choosing camelCase is the least surprising default.
Enter System.Text.Json
We're adding a new serializer to dotnet/CoreFx - which is a good opportunity to question any and all assumptions about what we're doing. Using a new serializer as the default will mean breaking changes in behavior, so if there are things we want to change in this are it's our one chance.
I originally said we should use camelCase and be case-sensitive because it's the more correct default. JSON is fundamentally a case-sensitive format so it seems like the best default.
The problem
The project is that this breaks webapi client - which uses Newtonsoft.Json's default settings and preserves the case of your C# properties. In fact, for the immediate future, any .NET code that calls your APIs probably is using Newtonsoft.Json and probably is using its default settings (PascalCase).
So they send:
But we would only accept:
This gets worse because we also want to tolerate/ignore unmatched properties by default. The result is that things don't work and you don't know why.
The solution
The solution is opt-in to case-insensitivity. This gives us the same behavior as we had with Newtonsoft.Json.
I'm personally convinced that this is the right default because it's going to just work for most users, and it's also our current behavior. Again, this is a discussion about our default, so it's not about what's the most correct, it's about what's going to work well for most users.
If I were writing an API, I would consider changing this setting, which is easy to do.
/cc @anurse @BrennanConroy @pranavkm
The text was updated successfully, but these errors were encountered: