OpenAPITools / OpenAPITools/openapi-generator
[csharp-netcore] Incorrect JsonConverter attribute added to polymorphic sub-classes
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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:

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
models_out/src/Org.OpenAPITools/Model/NetworkService.cs- JsonConverter attribute on class annotation expectedmodels_out/src/Org.OpenAPITools/Model/P2PNetworkService.cs- JsonConverter attribute on class annotation NOT expectedmodels_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:

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
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Run the provided openapi-generator-cli command with the linked schema, then inspect NetworkService.cs, P2PNetworkService.cs, and CloudNetworkService.cs. Read the fromModel block around line 375 of CSharpNetCoreClientCodegen.java and determine how the discriminator is assigned to non-base models. Done means only the base class has the JsonConverter attribute and relevant tests cover the generated output.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, java
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100