Skip to content

[BUG][aspnetcore] bug enum becomes nullable when required or nullable=false #3574

@ingarpedersen

Description

@ingarpedersen

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

When generating aspnetcore code enums always becomes nullable in the model file.
The definition for the enum class is correct but declaration does not take nullable/required into account.

The resulting cs file for the openapi below is (have removed enum definitions etc):

[DataMember(Name="TestEnum1", EmitDefaultValue=false)]
public TestEnum1Enum? TestEnum1 { get; set; }

[DataMember(Name="TestEnum2", EmitDefaultValue=false)]
public TestEnum2Enum? TestEnum2 { get; set; }

[Required]
[DataMember(Name="TestEnum3", EmitDefaultValue=false)]
public TestEnum3Enum? TestEnum3 { get; set; }

[Required]
[DataMember(Name="TestEnum4", EmitDefaultValue=false)]
public TestEnum4Enum? TestEnum4 { get; set; }

I was expecting (assuming nullable is default true):

[DataMember(Name="TestEnum1", EmitDefaultValue=false)]
public TestEnum1Enum? TestEnum1 { get; set; }

[DataMember(Name="TestEnum2", EmitDefaultValue=false)]
public TestEnum2Enum TestEnum2 { get; set; }

[Required]
[DataMember(Name="TestEnum3", EmitDefaultValue=false)]
public TestEnum3Enum TestEnum3 { get; set; }

[Required]
[DataMember(Name="TestEnum4", EmitDefaultValue=false)]
public TestEnum4Enum TestEnum4 { get; set; }

If I generate with the csharp-netcore generator enum3 and 4 are as expected, but enum2 is still nullable in the generated code.

openapi-generator version

4.1.0-SNAPSHOT

OpenAPI declaration file content or url
Item:
      type: object
      properties:
        TestEnum1:
          type: string
          enum: [t1val1, t1val2]
        TestEnum2:
          nullable: false
          type: string
          enum: [t1val1, t1val2]
        TestEnum3:
          type: string
          enum: [t1val1, t1val2]
        TestEnum4:
          nullable: false
          type: string
          enum: [t1val1, t1val2]
      required:
        - TestEnum3
        - TestEnum4
Command line used for generation

docker run --rm -v //OpenAPITest:/tmp openapitools/openapi-generator-cli generate -i /tmp/openapi.yaml -g aspnetcore -o /tmp/Test

Steps to reproduce

Running the docker command above with enum definition as described above will generate described code

Related issues/PRs

#3528, #2269

Suggest a fix

It would seem the template for aspnetcore does not handle enums correctly, but I do not know the generator code/templates well enough to pinpoint the problem.

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