Skip to content

[JavaScript] String Enum types produce invalid code #3416

@xyin96

Description

@xyin96
Description

Enum types produce invalid JavaScript when dealing with String constants

Swagger-codegen version

Latest (2.2.0)

Swagger declaration file content or url

Given a swagger definition for an Enum type:

"deviceType": {
          "enum": [
            "ANDROID",
            "UNKNOWN",
            "IOS"
          ],
          "type": "string"
        },

The generated JS client looks like this

exports.DeviceTypeEnum = {
    /**
     * value: ANDROID
     * @const
     */
    "ANDROID": ANDROID,


    /**
     * value: UNKNOWN
     * @const
     */
    "UNKNOWN": UNKNOWN,


    /**
     * value: IOS
     * @const
     */
    "IOS": IOS

  };

Which is invalid javascript, since the constants IOS, UNKNOWN, etc. are undefined.

Proposed fix

We've worked around this by adding in double quotes to the templates:

exports.{{datatypeWithEnum}} = {
  {{#allowableValues}}
  {{#enumVars}}
{{#emitJSDoc}}
    /**
     * value: {{{value}}}
     * @const
     */
{{/emitJSDoc}}
    "{{name}}": "{{{value}}}"{{^-last}},
    {{/-last}}
  {{/enumVars}}
  {{/allowableValues}}
  };
Command line used for generation
swagger-codegen generate -i $SWAGGER_URL \
    -l javascript \
    -o clients/javascript/ \
    --additional-properties usePromises=true,projectName=test-project 

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions