[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. 🙇 |
macjohnny
left a comment
There was a problem hiding this comment.
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
oneOfschema without a discriminator might cause runtime errors.schema:
generated model code (partial):
In
BarFromJSONTyped, if the given json doesn't have theabcproperty,(json['abc'] as Array<any>).map(...)will fail.The same problem also occurs if
abcis a string with a date-time format.Solution
I modified the template to generate
FooFromJSONTypedlike the following:Because
instanceOfXXXXfunctions 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)