OpenAPITools / OpenAPITools/openapi-generator

[BUG][Javascript client] oneOf including array of string conditions always fails

Open
#16,379 0 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 have a few oneOf declaration. Array of strings never go through, so I have to bypass or edit the generated code that creates the objects. Here is a simplified example:

components:
  schemas:
    book:
      type: object
      properties:
        author:
          oneOf:
          - type: string
            example: John Doe
          - type: array
            example: ["Jane Doe", "John Doe"]
            items:
              type: string

The above generates the following code inside the BookAuthor.js class. The condition under "validate array of strings" will never succeed with an array of strings!

        try {
            // validate array data type
            if (!Array.isArray(instance)) {
                throw new Error("Invalid data type. Expecting array. Input: " + instance);
            }
            // validate array of string
            for (const item of instance) {
                if (!(typeof item === 'number' && item % 1 === 0)) {
                    throw new Error("Invalid array items. Must be string. Input: " + instance);
                }
            }
            this.actualInstance = instance;
            match++;
        } catch(err) {
            // json data failed to deserialize into [String]
            errorMessages.push("Failed to construct [String]: " + err)
        }

If the condition would read as if (!(typeof item === "string")) {, it would solve the issue.

openapi-generator version

7.0.0-SNAPSHOT

Generation Details
docker pull openapitools/openapi-generator-cli:latest
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli generate \
   -i /local/openapi.yaml \
   -g javascript \
   -o /local/
Steps to reproduce
  1. Create OpenAPI file including oneOf, as per example above.
  2. Run the genreation shell script, see above.
  3. Test or audit genrated conde in the class that resolves the oneOf definition.
Related issues/PRs

#10514
#13539

Suggest a fix

If the condition would read as if (!(typeof item === "string")) {, it would solve the issue.

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 simplified OpenAPI YAML and run the documented Docker command using the javascript generator. Inspect the generated BookAuthor.js oneOf validation, especially the array-of-string branch; done means generated clients accept arrays such as ["Jane Doe", "John Doe"] instead of rejecting them.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.