OpenAPITools / OpenAPITools/openapi-generator
[BUG] The generated javascript client library is not handling "oneOf" properly.
Nobody has claimed this yet.
- 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
Patch
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
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
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