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

[JavaScript] Client produces a wrong object for a string enum type that is used with $ref #1429

Closed
advance512 opened this issue Nov 13, 2018 · 5 comments · Fixed by #1484
Closed

Comments

@advance512
Copy link

Description

Quoting the initial post in swagger-api/swagger-codegen#4819.

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

openapi-generator version

openapi-generator-cli-3.3.3-20181113.090410-41.jar

OpenAPI 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/openapi-generator-cli-3.3.3-20181113.090410-41.jar
generate -i test.yaml -l javascript -o output

Steps to reproduce

A reproduction is available at:
https://github.com/advance512/swagger-codegen-issue-4819/tree/openapi-generator-cli-3.3.3-20181113.090410-41.jar

Related issues/PRs

A lot more information available here:
swagger-api/swagger-codegen#4819

Suggest a fix/enhancement

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.

@advance512
Copy link
Author

@wing328
Is there a way to have this bug fixed with priority? A donation?

@advance512
Copy link
Author

@jfiala could you have a look at this?

@advance512
Copy link
Author

advance512 commented Nov 14, 2018

@advance512
Copy link
Author

@cartamundi-fdeo
Copy link

if type is undefined, this gives an exception:

image

I don't know if "type" should ever be undefined, but it is in my case.

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

Successfully merging a pull request may close this issue.

3 participants