OpenAPITools / OpenAPITools/openapi-generator
[BUG][Csharp] Client serialization of oneOf repeats discriminator
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator (example)?
- Have you tested with the latest master to confirm the issue still exists?
- Have you searched for related issues/PRs?
- What's the actual output vs expected output?
- [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
In the csharp client generator, generichost library, the JsonConverter for a class generated from a oneOf with discriminator serializes incorrectly. It will write all properties from the "inner" class plus the discriminator from the oneOf class, thus writing the discriminator property twice.
Spec and generated code here: Oneof.zip
Test that shows the error:
[Fact]
public void OneOfTwoSerializationTest()
{
var options = new JsonSerializerOptions();
options.Converters.Add(new OneOfTwoJsonConverter());
options.Converters.Add(new T1JsonConverter());
options.Converters.Add(new T2JsonConverter());
var oneOfTwo = new OneOfTwo(new T2("t2", 2), "t2");
string json = JsonSerializer.Serialize<OneOfTwo>(oneOfTwo, options);
Assert.DoesNotMatch("type.*type", json);
}
The serialized json in the above:
{"$type":"t2","val":2,"$type":"t2"}
openapi-generator version
7.0.0
OpenAPI declaration file content or url
(See attached zip)
Generation Details
openapi-generator-cli generate -i Oneof.json -g csharp -o OneOfClient --package-name OneOfClient "--additional-properties=library=generichost"
Steps to reproduce
Run the test above.
Related issues/PRs
None found
Suggest a fix
The Write method of the generated OneOfTwoJsonConverter should be a lot simpler (and no WriteProperties method need be generated):
public override void Write(Utf8JsonWriter writer, OneOfTwo oneOfTwo, JsonSerializerOptions jsonSerializerOptions)
{
if (oneOfTwo.T1 != null) {
JsonSerializer.Serialize<T1>(writer, oneOfTwo.T1, jsonSerializerOptions);
return;
}
if (oneOfTwo.T2 != null) {
JsonSerializer.Serialize<T2>(writer, oneOfTwo.T2, jsonSerializerOptions);
return;
}
throw new NotSupportedException();
}
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
Start with the supplied Oneof.zip and generation command, then run the OneOfTwoSerializationTest against the generated C# generichost client. Inspect OneOfTwoJsonConverter.Write and compare the generated JSON with the expected single discriminator; done means the repeated discriminator is absent and the test passes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp
- Domain
- tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 55/100