Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #3574
Closed
Open
Issue created Aug 07, 2019 by Administrator@rootContributor5 of 6 checklist items completed5/6 checklist items

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

Created by: 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 (closed), #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.

Assignee
Assign to
Time tracking