Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jmlord opened this issue Aug 22, 2023 · 0 comments

Comments

@jmlord
Copy link

jmlord commented Aug 22, 2023

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.

@jmlord jmlord changed the title [BUG][Javascript client] OneOf including array of string conditions always fails [BUG][Javascript client] oneOf including array of string conditions always fails Aug 22, 2023
@jmlord jmlord changed the title [BUG][Javascript client] oneOf including array of string conditions always fails [BUG][Javascript client] array of string conditions always fails Aug 24, 2023
@jmlord jmlord changed the title [BUG][Javascript client] array of string conditions always fails [BUG][Javascript client] oneOf including array of string conditions always fails Aug 24, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant