OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.49k stars 6.5k forks source link

[JavaScript (with promises)] Bug following $ref in object properties #1431

Open advance512 opened 5 years ago

advance512 commented 5 years ago
Description

Generated type file ID contains the following:

  /**
   * Constructs a <code>ID</code> from a plain JavaScript object, optionally creating a new instance.
   * Copies all relevant properties from <code>data</code> to <code>obj</code> if supplied or a new instance if not.
   * @param {Object} data The plain JavaScript object bearing properties of interest.
   * @param {module:model/ID} obj Optional instance to populate.
   * @return {module:model/ID} The populated <code>ID</code> instance.
   */
  exports.constructFromObject = function(data, obj) {
    if (data) {
      obj = obj || new exports();

    }
    return obj;
  }

data is an integer input. obj is null. Hence, the result is exports() instead of simply data.

openapi-generator version

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

OpenAPI declaration file content or url
definitions:

  # ===============================================================================
  # Fragments
  # ===============================================================================

  ID:
    description: An entity identifer
    type: integer
    format: int64
    readOnly: true

  # ===========================================================================
  # Users
  # ===========================================================================

  User:
    type: object
    required:
      - emailAddress
    properties:
      id:
        $ref: '#/definitions/ID'
      emailAddress:
        type: string
        format: email
        minLength: 6
        maxLength: 254
Command line used for generation

java -jar openapi-generator-cli-3.3.3-20181113.090410-41.jar generate -i ../source.yaml -l javascript --additional-properties usePromises=true -o ./javascript/

Steps to reproduce

Use the above definitions in any operation. You'll get a result object with an exports() objects instead of an ID integer.

Related issues/PRs

This is the same issue in swagger-codegen: https://github.com/swagger-api/swagger-codegen/issues/4973

Suggest a fix/enhancement

It tempts me to say:

  /**
   * Constructs a <code>ID</code> from a plain JavaScript object, optionally creating a new instance.
   * Copies all relevant properties from <code>data</code> to <code>obj</code> if supplied or a new instance if not.
   * @param {Object} data The plain JavaScript object bearing properties of interest.
   * @param {module:model/ID} obj Optional instance to populate.
   * @return {module:model/ID} The populated <code>ID</code> instance.
   */
  exports.constructFromObject = function(data, obj) {
    if (data) {
      obj = **data** || new exports();

    }
    return obj;
  }

but I actually think the issue is that the generator doesn't follow the $ref properly. Not certain

advance512 commented 5 years ago

This might be related: https://github.com/swagger-api/swagger-codegen/issues/7994

advance512 commented 5 years ago

I think this was solved in swagger-codegen. Have a look at the fix in: https://github.com/swagger-api/swagger-codegen/pull/7991

@jfiala could you have a look at this?