Skip to content

[java-micronaut] Generate visitor for subtypes with a discriminator#12192

Merged
wing328 merged 2 commits intoOpenAPITools:masterfrom
auke-:micronaut-visitable
Apr 25, 2022
Merged

[java-micronaut] Generate visitor for subtypes with a discriminator#12192
wing328 merged 2 commits intoOpenAPITools:masterfrom
auke-:micronaut-visitable

Conversation

@auke-
Copy link
Copy Markdown
Contributor

@auke- auke- commented Apr 21, 2022

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

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./mvnw clean package 
    ./bin/generate-samples.sh
    ./bin/utils/export_docs_generators.sh
    
    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*.
    For Windows users, please run the script in Git BASH.
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@andriy-dmytruk

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.
@andriy-dmytruk
Copy link
Copy Markdown
Contributor

@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 switch(...) default: ... . But I suppose it can be arguable, as this is not exactly following the pattern. What do you think?

@auke-
Copy link
Copy Markdown
Contributor Author

auke- commented Apr 22, 2022

I am wondering if it could make sense to provide a default visitor implementation.

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.

@andriy-dmytruk
Copy link
Copy Markdown
Contributor

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).

Makes sense. And if anyone would really need that, they can create a feature request.

@wing328 can you merge this PR?

@wing328 wing328 added this to the 6.0.0 milestone Apr 25, 2022
@wing328 wing328 merged commit 85170ee into OpenAPITools:master Apr 25, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants