Search code examples
spring-bootmavenopenapiopenapi-generator

Spring Boot: OpenApi Generator Plugin creates unwanted test file


I use OpenApi 3.0 and the maven plugin openapi-generator-maven-plugin to generate my api + objects.

This is my maven config:

                    <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/BookingService.yaml</inputSpec>
                        <generatorName>spring</generatorName>
                        <modelPackage>${clientPackage}.model</modelPackage>
                        <invokerPackage>${clientPackage}.invoker</invokerPackage>
                        <apiPackage>${clientPackage}.api</apiPackage>
                        <generateApis>true</generateApis>
                        <generateApiTests>false</generateApiTests>
                        <generateModelTests>false</generateModelTests>
                        <configOptions>
                            <delegatePattern>true</delegatePattern>
                        </configOptions>
                    </configuration>
                </execution>

It works as expected, however its also generating tests that I do not want. As you can see in my config i disabled the tests for Api tests + Model tests..

enter image description here

The compilation of these tests fail bc it "Cannot resolve symbol 'SpringBootTest'" in the build target folder...

These tests do not have any sense, how can I disable them?


Solution

  • Note this is a workaround. It would be better to get a property to not generate this testcase, but for now this seems to work...

    Add this maven plugin to your pom.xml

    <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-antrun-plugin</artifactId>
        <executions>
            <execution>
                <phase>process-sources</phase>
                <goals>
                    <goal>run</goal>
                </goals>
                <configuration>
                    <target>
                        <!-- remove the unwanted generated testcases by the spring generator of openapi -->
                        <delete dir="${project.build.directory}/generated-sources/openapi/src/test" />
                    </target>
                </configuration>
            </execution>
        </executions>
    </plugin>
    

    It will delete that entire test package after the sources have been generated in the process-sources phase