OpenAPITools / OpenAPITools/openapi-generator

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

Open
#3,574 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug Server: C-Sharp
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)?
  • 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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Run the provided Docker generation command with the supplied OpenAPI YAML to reproduce the nullable enum properties. Start by comparing the aspnetcore generator templates with the csharp-netcore output; done means generated enum declarations respect nullable and required for all four properties.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, openapi
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.