Skip to content

[BUG] [CSHARP] Wrong code generated for discriminator property when enum is used #19691

@McMlok

Description

@McMlok
Description

I have class inheritance in schema definition and using enum in descriminator property. When generating code with csharp a generichost library it generates wrong code in base class and its JSON converter.

[JsonIgnore]
[global::System.ComponentModel.EditorBrowsable(global::System.ComponentModel.EditorBrowsableState.Never)]
public ObjectTypeEnum ObjectType { get; } = "TestDescendants"; //This line does not compile

and

public void WriteProperties(Utf8JsonWriter writer, TestDescendants testDescendants, JsonSerializerOptions jsonSerializerOptions)
{
    if (testDescendants.AlternativeName == null)
        throw new ArgumentNullException(nameof(testDescendants.AlternativeName), "Property is required for class TestDescendants.");

    writer.WriteString("alternativeName", testDescendants.AlternativeName);

    writer.WriteString("objectType", ); //This line does not compile
}
openapi-generator version

openapi-codegen-cli-7.8.0.jar

OpenAPI declaration file content or url

sample-schema

Generation Details

java.exe -jar .\openapi-generator-cli.jar generate -i '.\enum-discriminator.yaml' -g csharp -o .\test\ --additional-properties='library=generichost,targetFramework=net8.0,excludeTests=true,nullableReferenceTypes=true,useOneOfDiscriminatorLookup=true,packageName=Test.Client'

Steps to reproduce

Generate code with command above and then open solution in VS and build or use dotnet build command. Compilation error appears.

Suggest a fix

The objectType property in TestDescendants does not have isNew flag set to true and wrong template branch is used in modelGeneric.mustache on line 173 and also in JsonConverter.mustache on line 442. Remove {{#isNew}} check for enum properties.

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