OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.58k stars 6.52k forks source link

[csharp-netcore] Incorrect JsonConverter attribute added to polymorphic sub-classes #15302

Open barryf002 opened 1 year ago

barryf002 commented 1 year ago

We are observing an issue in relation to usage and implementation of JsonSubTypes apparatus which is included in the csharp-netcore generator (openapitools version 6.5.0).

When using this generator to create a polymorphic tree of objects (1 level deep) - we observe that the configured discriminator field is annotated on the base class and any derived type - even though the schema is configured correctly (as per OpenApi spec documentation [allOf section])

An example of the issue is shown below:

image

As can be seen the base class has the expected discriminator in place but the sub-type also denotes this again (via JsonConverter attribute annotation). This causes any deserialisation (and validation) via Json.Net to be defunct. This functionality we believe should be implemented correctly. As per JsonSubTypes documentation - the JsonConverter attribute is only required for annotation on the base class of the polymorphic hierarchy.

This issue can be reproduced by using this linked schema with version 6.5.0 of the openapi tools as:

openapi-generator-cli version-manager set 6.5.0
openapi-generator-cli generate -g csharp-netcore -i https://raw.githubusercontent.com/barryf002/openapi-models-validation/main/schema_manual_mod.yaml -o models_out --additional-properties=targetFramework=netstandard2.1

Linked schema: https://raw.githubusercontent.com/barryf002/openapi-models-validation/main/schema_manual_mod.yaml

Please open the following files to observe the issue

  1. models_out/src/Org.OpenAPITools/Model/NetworkService.cs - JsonConverter attribute on class annotation expected
  2. models_out/src/Org.OpenAPITools/Model/P2PNetworkService.cs - JsonConverter attribute on class annotation NOT expected
  3. models_out/src/Org.OpenAPITools/Model/CloudNetworkService.cs - - JsonConverter attribute on class annotation NOT expected

Proposed fix:

An investigation into the java code was conducted to attempt to resolve the issue in the short term. It was found that a small modification to this block of code during CodeGenModel creation did remove any sub-class annotation using JsonConverter attribute.

The deduced change (also depicted) is to remove the discriminator from any CodeGenModel created for a non-base class in this method (fromModel): https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/CSharpNetCoreClientCodegen.java#L375

As: image

Whether this fix is the permanent approach will be upto the admins of this project - and relevant tests will need to be included.

This issue is a matter of urgency for our project delivery and any assistance is greatly appreciated. I would like to add some observed moderators in a tag list to get this issue as much visibility as possible in the short term - thanks in advance.

Please review in due course - thanks again: @wing328 @mandrean @shibayan @MartinDelille @muttleyxd

MartinDelille commented 1 year ago

Some C++ technical committee members were tagged on this issue so I retag the C# missing one: @frankyjuang @Blackclaws @lucamazzanti