swagger-api / swagger-api/swagger-codegen

C# enums rendered with either values or names, not both

Open
#5,837 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

Hi all,

We have some legacy code in our product where we have an enumeration starting at 1:

public enum AccountStateEnum
{
        Live = 1,
        Trial = 2,
        Internal = 3,
        Development = 4,
        Archived = 5,
        Disabled = 6
}

We would like swagger to generate the enum with strings, so we enable the SwaggerConfig setting 'DescribeAllEnumsAsStrings'.

This generates the enum like this:

        /// <summary>
        /// Gets or Sets AccountState
        /// </summary>
        [JsonConverter(typeof(StringEnumConverter))]
        public enum AccountStateEnum
        {
            
            /// <summary>
            /// Enum Live for "Live"
            /// </summary>
            [EnumMember(Value = "Live")]
            Live,
            
            /// <summary>
            /// Enum Trial for "Trial"
            /// </summary>
            [EnumMember(Value = "Trial")]
            Trial,
            
            /// <summary>
            /// Enum Internal for "Internal"
            /// </summary>
            [EnumMember(Value = "Internal")]
            Internal,
            
            /// <summary>
            /// Enum Development for "Development"
            /// </summary>
            [EnumMember(Value = "Development")]
            Development,
            
            /// <summary>
            /// Enum Archived for "Archived"
            /// </summary>
            [EnumMember(Value = "Archived")]
            Archived,
            
            /// <summary>
            /// Enum Disabled for "Disabled"
            /// </summary>
            [EnumMember(Value = "Disabled")]
            Disabled
        }

Which unfortunately doesn't work for us - as 'Live' needs to equal 1.

If we remove the setting from SwaggerConfig, the enum values are rendered like this:

        /// <summary>
        /// Gets or Sets AccountState
        /// </summary>
        [JsonConverter(typeof(StringEnumConverter))]
        public enum AccountStateEnum
        {
            
            /// <summary>
            /// Enum NUMBER_1 for 1
            /// </summary>
            [EnumMember(Value = "1")]
            NUMBER_1 = 1,
            
            /// <summary>
            /// Enum NUMBER_2 for 2
            /// </summary>
            [EnumMember(Value = "2")]
            NUMBER_2 = 2,
            
            /// <summary>
            /// Enum NUMBER_3 for 3
            /// </summary>
            [EnumMember(Value = "3")]
            NUMBER_3 = 3,
            
            /// <summary>
            /// Enum NUMBER_4 for 4
            /// </summary>
            [EnumMember(Value = "4")]
            NUMBER_4 = 4,
            
            /// <summary>
            /// Enum NUMBER_5 for 5
            /// </summary>
            [EnumMember(Value = "5")]
            NUMBER_5 = 5,
            
            /// <summary>
            /// Enum NUMBER_6 for 6
            /// </summary>
            [EnumMember(Value = "6")]
            NUMBER_6 = 6
        }

Which has the numbers assigned correctly, however the names are not being included!

Is there a way to get the best of both worlds?

Swagger-codegen version

2.3.0

Command line used for generation

Nothing special about the command line we use!

Steps to reproduce
  1. Create an enumeration with the values assigned starting at 1.
  2. Generate code with DescribeAllEnumsAsStrings enabled.
  3. Values will not be assigned to generated code.
  4. Disable DescribeAllEnumsAsStrings.
  5. Values will be assigned, however names are lost.

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

Reproduce the issue using the C# enum example and the listed Swagger-codegen 2.3.0 steps, comparing output with DescribeAllEnumsAsStrings enabled and disabled. Trace the C# enum generation templates and verify that the completed output preserves both symbolic names and explicit numeric assignments.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.