swagger-api / swagger-api/swagger-codegen

Csharp Client not generating valid enums

Open
#5,352 5 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Client: C-Sharp Feature: Enum help wanted Issue: Usage/Installation
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

CSharp client generation does not generate valid c# for enums with int values. eg
enum Sex {
None=0,
Male=1,
Female=2
}

After running generate command with the below config

{ "packageName":"{ProjectName}"}

The resultant code:
[JsonConverter(typeof(StringEnumConverter))]
public enum Sex
{

    /// <summary>
    /// Enum NUMBER_0 for 0
    /// </summary>
    [EnumMember(Value = 0)]
    NUMBER_0,
    
    /// <summary>
    /// Enum NUMBER_1 for 1
    /// </summary>
    [EnumMember(Value = 1)]
    NUMBER_1,
    
    /// <summary>
    /// Enum NUMBER_2 for 2
    /// </summary>
    [EnumMember(Value = 2)]
    NUMBER_2,
    
    /// <summary>
    /// Enum NUMBER_3 for 3
    /// </summary>
    [EnumMember(Value = 3)]
    NUMBER_3
}
Swagger-codegen version

2.2.3

Swagger declaration file content or url
Command line used for generation

java -jar swagger-codegen-cli.jar generate -i %JSON_CONFIG% -l csharp -c "%APP_FOLDER%code-gen-config.json" -o "%APP_FOLDER%API"

Steps to reproduce
Related issues
Suggest a Fix

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

Start with the reported java -jar swagger-codegen-cli.jar generate command and its C# output; the issue does not name a source file or test. Check how the integer-valued enum input is represented and compare it with the generated enum. Done means the generated C# is valid for that case; the thread has had no comment for years.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.