Skip to content

openapi generator

Dave Moten edited this page May 9, 2023 · 1 revision

Mixed usage with openapi-generator

Let's look at the case where we want to generate the server side (spring-boot) with openapi-generator but use the Jackson annotated schema classes generated by openapi-codegen (which support more of the OpenAPI standard).

Unfortunately the import mappings configuration of openapi-generator-plugin 6.4.0 does not work well at all except for simple single class bandaids. I got cooperation between openapi-generator and openapi-codegen working by following this process:

  • generate with openapi-generator-plugin to packages my.company.api and my.company.model
  • use maven-antrun-plugin to
    • replace all references to my.company.model to my.company.alt.model in my/company/server/.*.java
    • delete *.java files in my/company/model
  • generate with openapi-codegen-plugin to base package my.company.alt (which creates my.company.alt.model classes)

Here are the plugins doing the above (example):

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.4.0</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/api.yml</inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>my.company.api</apiPackage>
                <modelPackage>my.company.model</modelPackage>
                <configOptions>
                    <delegatePattern>true</delegatePattern>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <artifactId>maven-antrun-plugin</artifactId>
    <version>3.1.0</version>
    <executions>
        <execution>
            <phase>generate-sources</phase>
            <configuration>
                <target>
                    <replace dir="${project.build.directory}/generated-sources/openapi/src/main/java" token="model.MsiGet200Response" value="path.MsiGet200Response" failOnNoReplacements="true">
                        <include name="**/api/*.java" />
                    </replace>
                    <replace dir="${project.build.directory}/generated-sources/openapi/src/main/java" token="my.company.model" value="my.company.alt.schema" failOnNoReplacements="true">
                        <include name="my/company/api/*.java" />
                        <include name="org/openapitools/**/*.java" />
                    </replace>
                    <delete>
                        <fileset dir="${project.build.directory}/generated-sources/openapi/src/main/java" includes="my/company/model/*.java" />
                    </delete>
                </target>
            </configuration>
            <goals>
                <goal>run</goal>
            </goals>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>com.github.davidmoten</groupId>
    <artifactId>openapi-codegen-maven-plugin</artifactId>
    <version>VERSION_HERE</version>
    <executions>
        <execution>
            <id>generate-more</id>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <basePackage>my.company.alt</basePackage>
                <outputDirectory>${project.build.directory}/generated-sources/openapi/src/main/java</outputDirectory>
                <sources>
                    <directory>${project.basedir}/src/main/resources</directory>
                    <includes>
                        <include>api.yml</include>
                    </includes>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>
<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>3.3.0</version>
    <executions>
        <execution>
            <id>add-source</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>add-source</goal>
            </goals>
            <configuration>
                <sources>
                    <source>${project.build.directory}/generated-sources/openapi/src/main/java</source>
                </sources>
            </configuration>
        </execution>
    </executions>
</plugin>

Clone this wiki locally