Skip to content

[BUG] [CSharp] Discriminator mapping for polymorphie is ignored #22284

@pfeufferdaniel

Description

@pfeufferdaniel

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

openapi.json

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

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions