OpenAPITools / OpenAPITools/openapi-generator

The polymorphism in C# with Newtonsoft.Json is unable to deserialize child classes due to the JsonConverter annotation present in the child class.

Open
#17,990 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

"Adding [JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")] to the child C# class compels Newtonsoft to not look at the subtypes within the child class. To do it properly, one should move the line [JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")] in {{#mappedModels.length}}.
like this ?
{{#mappedModels.length}}
[JsonConverter(typeof(JsonSubtypes), "{{{discriminatorName}}}")]
{{/mappedModels.length}}

https://github.com/OpenAPITools/openapi-generator/blame/275ed9296dea4a1a894cc5b3402b5b8f3a61e7d7/modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache#L13

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The referenced template is modules/openapi-generator/src/main/resources/csharp/modelGeneric.mustache near line 13; inspect how JsonConverter is emitted for child and mapped models. Regenerate a C# client from a polymorphic OpenAPI model and verify Newtonsoft.Json can deserialize child classes without the child annotation blocking subtype lookup.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.