OpenAPI Generator Template Customization Example

The OpenAPI Generator is a wonderful bit of tech. It allow users to create an OpenAPI spec, and then generate client and server code from it in a variety of languages and platforms. I use it to generate DTOs for all my APIs, and to generate service interfaces to keep my client and server in sync. It also simplifies contract testing, if that’s your bag.

Contract testing is my bag, baby.

The generator is quite powerful out of the box, but it doesn’t do everything. Fortunately, it’s also extremely customizable, so if and when you find something that’s not supported out of the box, then you can make it work with minimal muss and fuss using template customization. Here’s how.

For example, the jaxrs-spec generator supports the JAX-RS 2.1 CompletionStage asynchronous style using <supportAsync>, but not the JAX-RS 2.0 AsyncResponse asynchronous style. However, Dropwizard only supports the JAX-RS 2.0 style. So we can’t use service contracts with asynchronous methods in Dropwizard out of the box with the Generator.

Template customization to the rescue!

Except, in our case, the need is actually concrete and practical.

I put together this OpenAPI Generator Maven plugin template customization example in GitHub to show off how to use template customization to solve this problem. So if you need to use the OpenAPI Generator to generate AsyncResponse-style server-side asynchronous processing service contracts, then this project will show you how. And if you need to do some other kind of customization, then this OpenAPI Generator template customization example should be a fine starting point.

Happy customizing!