swagger-api / swagger-api/swagger-codegen

[CSharp] Spaces in enum generates uncompilable code

Open
#9,357 0 comments 2 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

If there is an enum, with a value of ' ' (SPACE), then the resulting C# code is invalid and doesn't build

The invalid generated code looks like

/// <summary>
/// Enum  for value:  
/// </summary>
[EnumMember(Value = " ")]
 = 1,
Swagger-codegen version

2.4.5-SNAPSHOT

Swagger declaration file content or url
      SomeType:
        type: string
        description: |
          Some Type
          > Possible values include:
            * Y - Yes
            * N - No
            * Space - Not Supplied
        enum:
          - Y
          - N
          - ' '
Command line used for generation

java -jar .\swagger-codegen-cli.jar generate -i .\MySample.yaml -l csharp

Steps to reproduce

Define an enum with a value of ' '

Related issues/PRs

I looked through issues, but didn't spot this being previously reported

Suggest a fix/enhancement

io.swagger.codegen.languages.CSharpClientCodegen#toEnumVarName

    @Override
    public String toEnumVarName(String value, String datatype) {
        if (value.length() == 0) {
            return "Empty";
        }

        // New code added to return a constant that isn't whitespace
        if (value.trim().length() == 0)
        {
            return "SPACE";
        }
...

This then produces code that can be compiled, example

/// <summary>
/// Enum SPACE for value:  
/// </summary>
[EnumMember(Value = " ")]
SPACE = 1,

I've focused on C# here and that is causing me a problem, but wonder about other languages, for instance io.swagger.codegen.DefaultCodegen#toEnumVarName looks to have similar code

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 at io.swagger.codegen.languages.CSharpClientCodegen#toEnumVarName and reproduce generation with the YAML enum containing ' ' using the csharp generator. Check the generated enum and any related DefaultCodegen behavior; done means the generated C# code has a valid identifier for the whitespace value and compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
csharp, java
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
55/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.