Skip to content

C# Enums defined as optional are not nullable [MyEnum (?) myenum] #6690

Description

@moreyeel
Description

I am generating C# files from a YAML file. I have an optional enum parameter which I decorate in the template NullValueHandling = NullValueHandling.Ignore meaning if null do not serialize. Like so:

[JsonProperty("days", NullValueHandling = NullValueHandling.Ignore)]
public WeekDays Days { get; set; }

The trouble is that unlike other C# nullable types (int?, bool?) the enum is not generated as nullable.
This means that the default value (sun) is always sent in my requests which is not the desired behavior. I cannot discern when the user chose not to configure a value at all and when the user configured sun in the aforementioned enum.

Swagger-codegen version

swagger-codegen version 2.2.3

Swagger declaration file content or url
swagger: '2.0'
info:
  version: '1'
  title: My title
host: 'localhost:10010'
basePath: /
schemes:
  - http
  - https
consumes:
  - application/json
produces:
  - application/json
definitions:
  WeekDays:
    type: string
    enum:
      - sun
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
  My_Class_With_Optional_Enum:
    properties:
      quarantine:
        type: boolean
      grayware:
        type: boolean
      days:
        $ref: '#/definitions/WeekDays'
      
Command line used for generation

java -jar -Dproject -Dmodels -DmodelTests=false -DmodelDocs=false -DdebugModels=true -DdebugOperations=true swagger-codegen-cli-2.2.3.jar generate -i my.yaml -l csharp -o outputDir -t templateDir --type-mappings Guid?=string

Steps to reproduce

Generate the C# files using swagger-codegen-cli-2.2.3.jar
Open the file MyClassWithOptionalEnum.cs
See public WeekDays Days { get; set; }
Expected" public WeekDays ? Days { get; set; }

Related issues/PRs
Suggest a fix/enhancement

C# treat enums as a nullable type

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions