swagger-api / swagger-api/swagger-codegen

Javascript client produces a wrong object for a string enum type that is used with $ref

Open
#4,819 39 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: JavaScript/Node.js Feature: Enum help wanted Issue: Bug
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

I got something like { '0': 'f', '1': 'i', '2': 's', '3': 'h' } for an enum type. It should be "fish".

Swagger-codegen version

2.2.2-SNAPSHOT (revision: bb81fc130aedd64a4d35232d5173b12ce758e967)

Swagger declaration file content or url
swagger: "2.0"
info:
  version: 1.0.0
schemes:
  - http
paths:
  /pet:
    post:
      parameters:
      responses:
        '405':
          description: Invalid input

definitions:
  EnumArrays:
    type: object
    properties:
      array_enum:
        type: array
        items:
          type: string
          enum:
            - fish
            - crab
      array_enum_ref:
        type: array
        items:
          $ref: '#/definitions/StringEnumObject'
  StringEnumObject:
    type: string
    enum:
      - fish
      - crab
Command line used for generation
$ java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -i test.yaml -l javascript -o output
Steps to reproduce
  1. Run codegen by the above command
  2. Cd into output directory, then run npm install
  3. Start a node interactive shell and type them:
$ node
> var SwaggerClient = require('./src')
> SwaggerClient.EnumArrays.constructFromObject({"array_enum": ["fish", "crab"]})
exports { array_enum: [ 'fish', 'crab' ] }
> SwaggerClient.EnumArrays.constructFromObject({"array_enum_ref": ["fish", "crab"]})
exports {
  array_enum_ref: 
   [ { '0': 'f', '1': 'i', '2': 's', '3': 'h' },
     { '0': 'c', '1': 'r', '2': 'a', '3': 'b' } ] }
Related issues

I did not find any known issues about it. However, there were a number of bugs related to enum types with Javascript client in the past.

#3639 [javascript client] constructFromObject calls undefined function constructFromObject on enums
#3416 [JavaScript] String Enum types produce invalid code
#2102 [JavaScript] Enums are not exported correctly

Suggest a Fix

I can fix the issue by modifying convertToType method defined in ApiClient.mustache like this:

--- a/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache
+++ b/modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache
@@ -470,7 +470,7 @@
         if (type === Object) {
           // generic object, return directly
           return data;
-        } else if (typeof type === 'function') {
+        } else if (typeof type.constructFromObject === 'function') {
           // for model type like: User
           return type.constructFromObject(data);
         } else if (Array.isArray(type)) {

I think ApiClient.convertToType() can always call type.constructFromObject() for types that have constructFromObject method. But I'm not sure about its side effects.

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 modules/swagger-codegen/src/main/resources/Javascript/ApiClient.mustache, especially the convertToType method, and reproduce the generated client with the supplied Java command and Swagger definition. Compare constructFromObject handling for the inline enum and the $ref enum. Done means the referenced string enum values remain strings, while the existing inline enum behavior still works.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.