[java-micronaut] Generate visitor for subtypes with a discriminator#12192
[java-micronaut] Generate visitor for subtypes with a discriminator#12192wing328 merged 2 commits intoOpenAPITools:masterfrom
Conversation
When types which extend a common type and are distinguished based on a discriminator are consumed they are often cast to their specific Java type which results in error prone boilerplate code. By generating a visitor for those kind of types the various subtypes can be consumed in a type safe manner.
|
@auke- Everything looks good to me. Nice pattern and implementation. Thanks. I am wondering if it could make sense to provide a default visitor implementation. Something like: public interface DefaultVisitor<R> extends Visitor<R> {
{{#discriminator.mappedModels}}
default R visit{{modelName}}({{modelName}} value) {
return visitDefault(value);
}
{{/discriminator.mappedModels}}
R visitDefault({{classname}} value);
}which could be used when someone needs functionality analogous to |
For me a default visitor implementation is an anti-pattern since it prevents the compiler from warning me when an implementation is incomplete (either with an initial implementation or when the visitor interface is changed). But since your proposal is an opt-in for the user, they don't have to use the DefaultVisitor and can choose if they want the fallback behavior or not. But i'm not sure if anyone would really want that. |
Makes sense. And if anyone would really need that, they can create a feature request. @wing328 can you merge this PR? |
When types which extend a common type and are distinguished based on a
discriminator are consumed they are often cast to their specific Java
type which results in error prone boilerplate code.
By generating a visitor for those kind of types the various subtypes can
be consumed in a type safe manner.
This change is based on the same implementation I made for the Kokuwa.io
version of the micronaut openapi plugin.
Note that there is also a pull request for the typescript-angular implementation.
PR checklist
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*.For Windows users, please run the script in Git BASH.
@andriy-dmytruk