OpenAPITools / OpenAPITools/openapi-generator
[BUG][Javascript client] oneOf including array of string conditions always fails
Nobody has claimed this yet.
- 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
- Create OpenAPI file including oneOf, as per example above.
- Run the genreation shell script, see above.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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