Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #1429
Closed
Open
Issue created Nov 13, 2018 by Administrator@rootContributor

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

Created by: advance512

Description

Quoting the initial post in https://github.com/swagger-api/swagger-codegen/issues/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: https://github.com/swagger-api/swagger-codegen/issues/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.

Assignee
Assign to
Time tracking