-
Notifications
You must be signed in to change notification settings - Fork 4.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Jsonserializer and record types with "primary" constructor #38539
Comments
@steveharter can you have a look at how the serializer handles records? We should add some tests to cover this new language feature. |
This is not working because
To address without a workaround, I believe we need to relax the deserializer to allow an exact-match on constructor parameter name to property name (in addition to the existing camel-case to Pascal-case match on property name). @layomia do you agree? |
@steveharter yes, that is a good approach to support this new language feature. cc @pranavkm |
OK, I'm working on getting a PR out today. |
@steveharter would it make sense to use |
@pranavkm the matching in question is between the property's name and the corresponding constructor parameter's name, where we currently expect the names to conform to C#'s naming guidelines e.g. a property named We see now with record types that the ctor parameter's name will be an exact match with the property's name, so we will make the change to check for an exact match as well. None of these semantics relate to the incoming JSON, so I don't believe there's any reason for the matching to be influenced by |
Yes as @layomia explains there is an indirection between the JSON property name and ctor parameter. The indirection is that the ctor parameter name must bind to a CLR property name through a naming convention (must be exact-match or match when property is camel-cased). This means, for example, you can have a |
Thanks @steveharter \ @layomia. It would be great to make sure this behavior is well documented particularly because the behavior does not conform to how Json.Net works. |
Json.Net does support the binding between parameter name and property allowing a completely different JSON name for example. However Json.Net doesn't require that binding (like STJ does) so this should be documented. |
@steveharter do you know if this change made it to p8? |
@pranavkm, it did - find "38f6ebc" here https://github.com/dotnet/runtime/commits/release/5.0-preview8. |
- appear to have fixes for all of the following: - #3835 -> dotnet/runtime#3229, dotnet/runtime#3315 - #8387 - #11813 - #12097 - #14022 - #24695 - dotnet/aspnetcore-internal#1363 -> #6393 -> #4763 - dotnet/corefx#36024 -> dotnet/runtime#28959 -> dotnet/runtime#29337 - dotnet/runtime#38126 -> #24513 - dotnet/runtime#38539 - dotnet/sdk#12831
- appear to have fixes for all of the following: - #3835 -> dotnet/runtime#3229, dotnet/runtime#3315 - #8387 - #11813 - #12097 - #14022 - #24695 - dotnet/aspnetcore-internal#1363 -> #6393 -> #4763 - dotnet/corefx#36024 -> dotnet/runtime#28959 -> dotnet/runtime#29337 - dotnet/runtime#38126 -> #24513 - dotnet/runtime#38539 - dotnet/sdk#12831
- appear to have fixes for all of the following: - #3835 -> dotnet/runtime#3229, dotnet/runtime#3315 - #8387 - #11813 - #12097 - #14022 - #24695 - dotnet/aspnetcore-internal#1363 -> #6393 -> #4763 - dotnet/corefx#36024 -> dotnet/runtime#28959 -> dotnet/runtime#29337 - dotnet/runtime#38126 -> #24513 - dotnet/runtime#38539 - dotnet/sdk#12831
- appear to have fixes for all of the following: - #3835 -> dotnet/runtime#3229, dotnet/runtime#3315 - #8387 - #11813 - #12097 - #14022 - #24695 - dotnet/aspnetcore-internal#1363 -> #6393 -> #4763 - dotnet/corefx#36024 -> dotnet/runtime#28959 -> dotnet/runtime#29337 - dotnet/runtime#38126 -> #24513 - dotnet/runtime#38539 - dotnet/sdk#12831
I've been playing with the new C# 9.0 features to evaluate how they integrate with ASP.NET Core. As part of that, I've been checking records and I found that the deserializer throws if I try to deserialize a record defined as follows:
The exception thrown is:
This forces people to use the longwinded syntax for records when writing APIs, which is something we want to avoid. The closest I got is to do, which is far more verbose:
The text was updated successfully, but these errors were encountered: