OpenAPITools / OpenAPITools/openapi-generator

[Bug] JavaScript client generator should serialize/deserialize polymorphic types

Open
#7,407 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: JavaScript/Node.js Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Bug Report Checklist
  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • Have you tested with the latest master to confirm the issue still exists?
  • [x ] Have you searched for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

The generated ApiClient.js does not add the discriminator value to the output when it serializes a model. This prevents the server from processing requests where the concrete type is not known ahead of time.

Conversely, the generated ApiClient.js does not make use of the discriminator value to construct the correct concrete type when it deserializes a response.

openapi-generator version

4.3.2

OpenAPI declaration file content or url
components:
  schemas:
    Node:
      type: 'object'
      properties:
        items:
          type: 'array'
          items:
            $ref: '#/components/schemas/NodeItem'
    NodeItem:
      type: 'object'
      discriminator:
        propertyName: 'type'
        mapping:
          ConcreteItem: '#/components/schemas/ConcreteItem'   
        properties:
          id:
            type: 'string'
    ConcreteItem:
      allOf:
        - $ref: '#/components/schemas/NodeItem'
        - type: 'object'
          properties:
            prop1:
              type: 'string'
Generation Details
Steps to reproduce
Related issues/PRs
Suggest a fix

The way I currently work around this is by editing the generated ApiClient.js and patching both callApi and convertToType.

Ideally, the generator would generate a ModelMapper, plug that directly into superagent, and allow the end user to replace it if they would like.

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 provided OpenAPI YAML and the generated ApiClient.js, focusing on the callApi and convertToType paths mentioned in the report. Reproduce generation with the Node/ConcreteItem discriminator example; done means serialization includes the discriminator value and deserialization constructs the mapped concrete type.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.