OpenAPITools / OpenAPITools/openapi-generator

[BUG] The generated javascript client library is not handling "oneOf" properly.

Open
#18,125 14 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

I've been using the openapi generation for years. I tend to generate python and javascript client libraries.

The problem I found occurs when you have a property that uses "oneOf", and the list of types includes "type: object".

I've patched the generated code, with success. The patch is quite simple. Hence I hope that this can be quickly resolved.

The generated javascript code does a check on type. It properly checks numbers, strings, arrays, booleans, however it fails to check on the object type and hence increments match. When I patched this the problem that I saw went away.

Bug
image

Patch
image

openapi-generator version

7.3.0

OpenAPI declaration file content or url

Just recently we added a tweak to the API spec as follows:

Spec:
      required:
        - name
        - value
      type: object
      properties:
        name:
          type: string
          example: zone
        value:
          oneOf:
            - type: string
            - type: number
            - type: integer
            - type: boolean
            - type: array
              items:
                type: object
            - type: object
          example: Critical
      xml:
        name: spec
Generation Details
npx ./node_modules/\@openapitools/openapi-generator-cli generate -i interface_spec.yaml  -g javascript -o test_lib_javascript
Suggest a fix
    try {
      if (!(typeof instance === "object")) {
        throw new Error(
          "Invalid value. Must be object. Input: " + JSON.stringify(instance)
        );
      }
      this.actualInstance = instance;
      match++;
    } catch (err) {
      // json data failed to deserialize into Object
      errorMessages.push("Failed to construct Object: " + err);
    }

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 by generating a JavaScript client with the supplied OpenAPI declaration and generation command, then inspect the generated oneOf handling for the object branch. Confirm that an object value is accepted without incorrectly incrementing the match count, and verify the generated client behavior against the reported case.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.