OpenAPITools / OpenAPITools/openapi-generator

[BUG][JAVASCRIPT] constructFromObject redundantly re-initializes fields belonging to the parent

Open
#17,543 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

In MyObject.js :

    static constructFromObject(data, obj) {
        if (data) {
            obj = obj || new MyModel();
            BaseModel.constructFromObject(data, obj);

            if (data.hasOwnProperty('myProperty')) {
                obj['myProperty'] = ApiClient.convertToType(data['myProperty'], 'String');
            }
            if (data.hasOwnProperty('baseProprty')) {
                obj['baseProprty'] = ApiClient.convertToType(data['baseProprty'], 'String');
            }
        }
        return obj;
    }

In BaseObject.js :

    static constructFromObject(data, obj) {
        if (data) {
            obj = obj || new BaseModel();

            if (data.hasOwnProperty('baseProperty')) {
                obj['baseProperty'] = ApiClient.convertToType(data['baseProperty'], 'String');
            }
        }
        return obj;
    }

baseProperty is initialized from the call to BaseModel.constructFromObject and then initialized again after the properties unique to the derived object.

openapi-generator version

7.2.0

OpenAPI declaration file content or url
components:
    schemas:
      BaseModel:
        type: object
        properties:
          baseProperty:
            type: string
      MyModel:
        allOf:
          - $ref: "#/components/schemas/BaseModel"
        type: object
        properties:
          myProperty:
            type: string

Generation Details

I used the Gradle plugin with the "javascript" generatorName

Steps to reproduce

Define a schema using allOf (without discriminators) and generate JavaScript models.

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 the generated MyObject.js and BaseObject.js examples and reproduce the issue using the provided allOf schema with the JavaScript generator and Gradle plugin. Trace how constructFromObject handles inherited properties, then regenerate the models to verify that parent fields are no longer initialized redundantly.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.