-
-
Notifications
You must be signed in to change notification settings - Fork 6.8k
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[typescript-fetch] Improve code generation for oneOf
cases without discriminator
#18702
[typescript-fetch] Improve code generation for oneOf
cases without discriminator
#18702
Conversation
Dear technical committee members of TypeScript, please check this pull request if you have time. 🙇 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
thanks for your contribution!
An edge case that wouldnt work is if the models dont contain required fields or they are the same, but i guess then its hard to distinguish without a discriminator anyway
@odiak I just updated our openapi-generator to
And indeed, if the input JSON is not an instance of any of the variants, this function returns Don't you have this exact same problem? |
@sir4ur0n thanks for reporting that. do you want to try to fix this? |
I don't really know how to fix that:
I think this PR was right to use I also don't feel very confident in attempting to fix because I am not great at Typescript 😅 |
@sir4ur0n I also realized same problem last month. I have workaround for it but I forgot to submit pull request. Sorry! My solution is just adding |
@odiak no worries 👍 Would you mind opening a PR with your workaround please? 🙏 |
@sir4ur0n Sure! |
@odiak did you get a chance to work on that? 🙏 |
I'll do it tonight! |
Problem
Previously, generated code from the
oneOf
schema without a discriminator might cause runtime errors.schema:
generated model code (partial):
In
BarFromJSONTyped
, if the given json doesn't have theabc
property,(json['abc'] as Array<any>).map(...)
will fail.The same problem also occurs if
abc
is a string with a date-time format.Solution
I modified the template to generate
FooFromJSONTyped
like the following:Because
instanceOfXXXX
functions check for the existence of required keys, it makes the generated code safer.How to check
PR checklist
Commit all changed files.
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*
.IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed.
master
(upcoming 7.1.0 minor release - breaking changes with fallbacks),8.0.x
(breaking changes without fallbacks)