Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Model enums are broken in generated C# client code #8027

Closed
JColl88 opened this issue Apr 16, 2018 · 4 comments
Closed

Model enums are broken in generated C# client code #8027

JColl88 opened this issue Apr 16, 2018 · 4 comments

Comments

@JColl88
Copy link

JColl88 commented Apr 16, 2018

Description

(This issue my be related to #7947.)

I have tried to generate a C# client solution using the codegen-cli 2.3.1.

The specification in question can be found at: https://api.apis.guru/v2/specs/amazonaws.com/AWSMigrationHub/2017-05-31/swagger.json

This functions fine with 2.2.3, and generates a solution that can be built in VS2013. However, using 2.3.1, the solution cannot be built as the C# code in one of the model classes (Status.cs) is invalid. A snippet of the class is shown below which illustrates the problem:

Status.cs:34 (generated using 2.2.3) - this works as expected:

        /// Enum NOTSTARTED for "NOT_STARTED"
        /// </summary>
        [EnumMember(Value = "NOT_STARTED")]
        NOTSTARTED,

Status.cs:34 (generated using 2.3.1) - this generates invalid C#:

        /// Enum NOTSTARTED for value: NOT_STARTED
        /// </summary>
        
        NOTSTARTED = NOT_STARTED,

If I generate using 2.3.1, but specify a folder containing the modelEnum.mustache template from 2.2.3, the following is generated, which is still invalid:

        /// Enum NOTSTARTED for NOT_STARTED
        /// </summary>
        [EnumMember(Value = NOT_STARTED)]
        NOTSTARTED,

(where the quotes around NOT_STARTED have not been planted.)

As of 16/04/18, the issue is still unfixed in the 2.4 snapshot.

Swagger-codegen version

2.3.1

Swagger declaration file content or url

https://api.apis.guru/v2/specs/amazonaws.com/AWSMigrationHub/2017-05-31/swagger.json

Command line used for generation

java -jar swagger-codegen-cli-2.3.1.jar generate -i aws_swagger.json -l csharp -DapiTests=false -DmodelTests=false

(where aws_swagger.json is a local copy of the version linked above.)

Steps to reproduce

Run the above command for the specification linked above (AWS Migration Hub).

Related issues/PRs

#7947

Suggest a fix/enhancement

In the spec, the Status enum is defined as follows:

      "enum": [
        "NOT_STARTED",
        "IN_PROGRESS",
        "FAILED",
        "COMPLETED"
      ],
      "type": "string"

By placing dummy text in isString tags in the modelEnum.mustache template (e.g. {{#isString}}This is a string{{/isString}}), it becomes apparent that the type tag for this variable is not being set correctly (to string), as the dummy text is not planted in the generated code.

@jages
Copy link
Contributor

jages commented Apr 27, 2018

Building 'swagger-codegen' locally, i have found a makeshift solution for this problem (that obviously requires further testing).
src/main/resources/csharp/enumClass.mustache @ line 14
replace
[EnumMember(Value = {{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isFloat}}"{{/isFloat}}{{#isDouble}}"{{/isDouble}}{{{value}}}{{#isLong}}"{{/isLong}}{{#isInteger}}"{{/isInteger}}{{#isDouble}}"{{/isDouble}}{{#isFloat}}"{{/isFloat}})]

with
[EnumMember(Value = "{{{value}}}")]

This works for the petstore/v2-demo, but needs to be looked into by someone with more experience. i don't think there is any DataType that cannot be masked with double quotes.

@coin8086
Copy link

coin8086 commented Jun 8, 2018

I also have this problem. Please fix is ASAP.

BTW, I found that the enum types are correct when download a generated client via the SwaggerHub(https://app.swaggerhub.com) online, but using 'swagger-codegen' 2.3.1 locally produces incorrect code. So the problem must be fixed officially already.

Please help.

spanglerco added a commit to spanglerco/swagger-codegen that referenced this issue Oct 1, 2018
Depending on HashMap iteration order, the isString flag added to string
enum values can be lost due to copying a reference to the
allowableValues map to properties that reference the enum. The result is
that the code fails to compile.

This should fix swagger-api#7656, fix swagger-api#7657 (regarded as a duplicate of 7656), and
fix swagger-api#8027 (which also appears to be the same issue).
spanglerco added a commit to spanglerco/swagger-codegen that referenced this issue Oct 2, 2018
Depending on HashMap iteration order, the isString flag added to string
enum values can be lost due to copying a reference to the
allowableValues map to properties that reference the enum. The result is
that the code fails to compile.

This should fix swagger-api#7656, fix swagger-api#7657 (regarded as a duplicate of 7656), and
fix swagger-api#8027 (which also appears to be the same issue).
@codersam
Copy link

codersam commented Dec 6, 2019

Same problem in version 3.0.14, installed from Homebrew.

@frantuma
Copy link
Member

should be fixed in #9936 (and swagger-api/swagger-codegen-generators#561 for 3.0.0)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants