OpenAPITools / OpenAPITools/openapi-generator

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

Open
#1,431 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

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

Reproduce the issue with the supplied OpenAPI definitions and Java generation command, then inspect the generated JavaScript ID model's constructFromObject function and the generator path that produced it. Done means a scalar integer reached through the ID $ref remains an integer instead of becoming an empty ID object, with a regression check covering the example.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, javascript, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.