OpenAPITools / OpenAPITools/openapi-generator
[BUG] [CSharp] Discriminator mapping for polymorphie is ignored
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?
- 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
I tested polymoprphie with with the charp generator default not library=RestSharp. In my open api description i have a discriminator mapping which is not applied correctly in the Deserializer of the BaseType
Type: ValueTypeBase
openapi-generator version
v7.17.0
OpenAPI declaration file content or url
Generation Details
config.yaml
additionalProperties:
targetFramework: net9.0
packageName: Rexroth.InduFlow.ApiClient
docker run --rm -v C:\100_Devel\induflow-example\:/local openapitools/openapi-generator-cli:v7.17.0 generate -i /local/code-generation/openapi.json -g csharp -o /local/lib/Rexroth.InduFlow.ApiClient -c /local/code-generation/config.yaml
Output
Steps to reproduce
Generate as described and see the Model/ValueTypeBase.cs
public override ValueTypeBase Read(ref Utf8JsonReader utf8JsonReader, Type typeToConvert, JsonSerializerOptions jsonSerializerOptions)
{
int currentDepth = utf8JsonReader.CurrentDepth;
if (utf8JsonReader.TokenType != JsonTokenType.StartObject && utf8JsonReader.TokenType != JsonTokenType.StartArray)
throw new JsonException();
JsonTokenType startingTokenType = utf8JsonReader.TokenType;
Option<string?> kind = default;
string? discriminator = ClientUtils.GetDiscriminator(utf8JsonReader, "kind");
if (discriminator != null && discriminator.Equals("BooleanValue"))
return JsonSerializer.Deserialize<BooleanValue>(ref utf8JsonReader, jsonSerializerOptions) ?? throw new JsonException("The result was an unexpected value.");
if (discriminator != null && discriminator.Equals("NumberValue"))
return JsonSerializer.Deserialize<NumberValue>(ref utf8JsonReader, jsonSerializerOptions) ?? throw new JsonException("The result was an unexpected value.");
if (discriminator != null && discriminator.Equals("PhysicalValue"))
return JsonSerializer.Deserialize<PhysicalValue>(ref utf8JsonReader, jsonSerializerOptions) ?? throw new JsonException("The result was an unexpected value.");
if (discriminator != null && discriminator.Equals("TextValue"))
return JsonSerializer.Deserialize<TextValue>(ref utf8JsonReader, jsonSerializerOptions) ?? throw new JsonException("The result was an unexpected value.");
while (utf8JsonReader.Read())
{
if (startingTokenType == JsonTokenType.StartObject && utf8JsonReader.TokenType == JsonTokenType.EndObject && currentDepth == utf8JsonReader.CurrentDepth)
break;
if (startingTokenType == JsonTokenType.StartArray && utf8JsonReader.TokenType == JsonTokenType.EndArray && currentDepth == utf8JsonReader.CurrentDepth)
break;
if (utf8JsonReader.TokenType == JsonTokenType.PropertyName && currentDepth == utf8JsonReader.CurrentDepth - 1)
{
string? localVarJsonPropertyName = utf8JsonReader.GetString();
utf8JsonReader.Read();
switch (localVarJsonPropertyName)
{
case "kind":
kind = new Option<string?>(utf8JsonReader.GetString()!);
break;
default:
break;
}
}
}
if (!kind.IsSet)
throw new ArgumentException("Property is required for class ValueTypeBase.", nameof(kind));
if (kind.IsSet && kind.Value == null)
throw new ArgumentNullException(nameof(kind), "Property is not nullable for class ValueTypeBase.");
return new ValueTypeBase();
}
discriminator check should be on "bool", "text", "physical" and "number"
Related issues/PRs
Suggest a fix
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 by regenerating the client with openapi.json, config.yaml, and the provided Docker command, then inspect Model/ValueTypeBase.cs. Compare the generated discriminator checks with the mapping in the OpenAPI declaration. Done means the deserializer selects the BooleanValue, NumberValue, PhysicalValue, and TextValue types for the specified discriminator values.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- csharp, docker, openapi
- Domain
- devtools, tooling
- Issue type
- Bug
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100