swagger-api / swagger-api/swagger-codegen

[aspnetcore] string enums are invalid

Open
#7,947 1 comment 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

C# auto generated aspnetcore project contains invalid enums

Swagger-codegen version

2.3.1

Swagger declaration file content or url

Gist here

yaml code

  status:
	type: "string"
	description: "pet status in the store"
	enum:
	- "available"
	- "pending"
	- "sold"
Command line used for generation

editor.swagger.io

Steps to reproduce
  1. Take code from gist
  2. Paste into editor.swagger.io (http, not https)
  3. Generate an aspnetcore server
  4. Model generated looks like the following:
        public enum StatusEnum
        { 
            /// <summary>
            /// Enum AvailableEnum for available
            /// </summary>
            [EnumMember(Value = available)]
            AvailableEnum = 1,
            
            /// <summary>
            /// Enum PendingEnum for pending
            /// </summary>
            [EnumMember(Value = pending)]
            PendingEnum = 2,
            
            /// <summary>
            /// Enum SoldEnum for sold
            /// </summary>
            [EnumMember(Value = sold)]
            SoldEnum = 3
        }

This is invalid C#, the Value = x should be Value = "x".

Related issues/PRs

Possibly related

Suggest a fix/enhancement

Generated model should look like the following:

    public enum StatusEnum
    { 
        /// <summary>
        /// Enum AvailableEnum for available
        /// </summary>
        [EnumMember(Value = "available")]
        AvailableEnum = 1,
        
        /// <summary>
        /// Enum PendingEnum for pending
        /// </summary>
        [EnumMember(Value = "pending")]
        PendingEnum = 2,
        
        /// <summary>
        /// Enum SoldEnum for sold
        /// </summary>
        [EnumMember(Value = "sold")]
        SoldEnum = 3
    }

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 with the linked Swagger declaration or gist in editor.swagger.io and generate the aspnetcore server. Start by locating the template or generator path that emits the shown StatusEnum and EnumMember attributes. Done means the generated C# uses quoted enum values and the model is valid C#.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp
Domain
backend, tooling
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.