Revisiting Selective Code Generation from OpenAPI Specifications

I have now moved fully to using the openapi-generator for generating code from OpenAPI specifications, instead of the “official” swagger-codegen. This fork, by some of the original maintainers, has a much more active community & development pace over the original project. My PR to improve tag support in swagger-codegen is still sitting with no activity more than 6 months on.

Using tags as part of generation has been supported for all JAX-RS based generators as of version 5 of openapi-generator. I am using this through through the maven plugin, which correctly integrates with maven to add generated sources to the sources paths. We use this with the generate goal linked to the maven lifecycle so code generation is performed in every build of the project, so we can be sure that the code in the produced artefact reflects the specification in the repository.

The maven plugin has very detailed documentation with all the configuration options available for it, but going into this it is worth noting that some of the terminology is different between the two forks of the plugin. This is reflected in the documentation. Individual generators are documented here.

With the maven plugin, to generate an API endpoint file containing all the endpoints tagged with “Pet” we could configure the plugin like this:

    <!-- RELEASE_VERSION -->
    <!-- /RELEASE_VERSION -->
            <!-- Our specification comes from a dependency --> 
                <!-- specify the OpenAPI spec -->

                <!-- target to generate jaxrs spec code -->
                <!-- restrict the APIs to generate -->

                <!-- pass any necessary config options -->

                    <!-- Generate API files based on tags -->

As with swagger-codegen, tags as they are used in the maven plugin configuration have all non alphanumeric characters removed.

The project also contains an example repository showing how a multi-module maven project could be created to produce artefacts for a specification as well as separate modules for client and server generated artefacts.