-
-
Notifications
You must be signed in to change notification settings - Fork 536
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
Use native attributes for polymorphism in System.Text.Json #1739
Comments
My sample project: https://github.com/marinasundstrom/NullabilityTransformersPrototype/tree/main/WebApi The Microsoft Open API API doesn't handle inheritance that well, at the moment. |
Some background: I have been working to make the Open API stack in .NET 9 to work with NSwag client generator the way I expect it to. So I have been talking about its output, because I have tried to create a transformer for dealing with inheritance. Open API in .NET has polymorphism but it is not like in NSwag that deals with type hierarchies. Just assume that I have been hacking Open API specifications. I have found that if I, in the base schema/type, set Indeed, I checked, NSwag outputs that It is still a difference in how System.Text.Json deals with it. That makes me wonder whether |
Should now be possible with the new option: |
Thanks a lot for addressing this PolymorphicSerialization issue. For some reason the new setting is not recognized when used as a parameter in the .csproj file? We are on .Net 8 and use the following command in the .proj file: Error thrown: |
TL;DR;
When handling polymorphism in System.Text.Json, the generator should emit
JsonPolymorphic
andJsonDerivedType
.Background
I have been using System.Text.Json as a target library for my projects. And I have discovered an issue with the generated C# client with regard to inheritance.
https://github.com/RicoSuter/NJsonSchema/blob/9bf8f695b373410e8b51e1363270c08dda7b8127/src/NJsonSchema.CodeGeneration.CSharp/Templates/JsonInheritanceConverter.liquid
Some context: Both server and generated C# client use System.Text.Json.
Problem
In the client, the generated special inheritance converter and attributes don't work for me since the serialized JSON always contain a double set of the discriminator.
{"species":"Dog","foo":true,"species":"Dog"}
This is due to the converter adding that. And the serializer doesn't like it.
Proposal
I have experimented with manually changing the attributes to the build in ones:
JsonPolymorphic
andJsonDerivedType
So instead of this:
We should have an option to have this generated when using
System.Text.Json
:That would at least solve my problem.
The changes should be made here:
NJsonSchema/src/NJsonSchema.CodeGeneration.CSharp/Templates/Class.liquid
Line 8 in 9bf8f69
The text was updated successfully, but these errors were encountered: