Spring Boot: OpenApi Generator Plugin creates unwanted test file
Asked Answered
H

3

7

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?

Henderson answered 23/8, 2022 at 15:18 Comment(1)
Without changing the code I have not found a way to do this without switching to interface only generation which I do not want. I have a workaround in my IDE to set the generated test folder to "Excluded" which makes it work but I also would like to just have the option not to generate the test caseStonechat
S
5

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

Stonechat answered 6/12, 2022 at 12:10 Comment(0)
S
1

We can skip the generation by enabling <interfaceOnly>true</interfaceOnly> but delegatePattern will not consider.

Here is the source code from openapi-generator... Source Code

if (!interfaceOnly) {
        if (SPRING_BOOT.equals(library)) {
            if (useSwaggerUI && selectedDocumentationProviderRequiresSwaggerUiBootstrap()) {
                supportingFiles.add(new SupportingFile("swagger-ui.mustache", "src/main/resources/static", "swagger-ui.html"));
            }
            // rename template to SpringBootApplication.mustache
            supportingFiles.add(new SupportingFile("openapi2SpringBoot.mustache",
                    (sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
                    "OpenApiGeneratorApplication.java"));
            supportingFiles.add(new SupportingFile("SpringBootTest.mustache",
                (testFolder + File.separator + basePackage).replace(".", java.io.File.separator),
                "OpenApiGeneratorApplicationTests.java"));
            supportingFiles.add(new SupportingFile("RFC3339DateFormat.mustache",
                    (sourceFolder + File.separator + basePackage).replace(".", java.io.File.separator),
                    "RFC3339DateFormat.java"));
        }
....

You can always exclude the generated test package in Intellij IDE for local compilation issue.

Speechless answered 7/10, 2022 at 6:42 Comment(0)
K
0

An detailed response can be seen here.

But, briefly example is:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.kinlhp</groupId>
    <artifactId>moname-build</artifactId>
    <version>${revision}</version>
    <packaging>pom</packaging>
    <name>${project.artifactId}</name>
    <description>Project to money me with no name [build].</description>
    <url>https://moname.kinlhp.com</url>
    <properties>
        <main.basedir>${basedir}</main.basedir>
        <maven.compiler.release>${java.version}</maven.compiler.release>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
        <openapi-generator.base-package>com.kinlhp.moname.commons.api.oas</openapi-generator.base-package>
        <openapi-generator.input-spec>${project.basedir}/src/main/resources/openapi/specification.yaml</openapi-generator.input-spec>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>${project.build.sourceEncoding}</project.reporting.outputEncoding>
        <!-- dependency versions -->
        <revision>1.0.0.BUILD-SNAPSHOT</revision>
        <!-- # third party # -->
        <java.version>17</java.version>
        <openapi-generator-maven-plugin.version>6.6.0</openapi-generator-maven-plugin.version>
    </properties>
    <build>
        <pluginManagement>
            <plugins>
                <!-- # third party # -->
                <!-- openapitools -->
                <plugin>
                    <groupId>org.openapitools</groupId>
                    <artifactId>openapi-generator-maven-plugin</artifactId>
                    <version>${openapi-generator-maven-plugin.version}</version>
                    <executions>
                        <execution>
                            <id>openapi-generator</id>
                            <phase>generate-sources</phase>
                            <goals>
                                <goal>generate</goal>
                            </goals>
                            <configuration>
                                <!-- README#openapi-generator-spring-metadata -->
                                <generatorName>spring</generatorName>
                                <!-- README#openapi-generator-spring-config-options -->
                                <apiPackage>${openapi-generator.base-package}.endpoint</apiPackage>
                                <artifactId>moname-commons-api-oas</artifactId>
                                <artifactVersion>${revision}</artifactVersion>
                                <generateApiTests>false</generateApiTests>
                                <generateModelTests>false</generateModelTests>
                                <groupId>${project.groupId}</groupId>
                                <ignoreFileOverride>.openapi-generator-ignore</ignoreFileOverride>
                                <inputSpec>${openapi-generator.input-spec}</inputSpec>
                                <invokerPackage>${openapi-generator.base-package}.invoker</invokerPackage>
                                <modelNameSuffix>DTO</modelNameSuffix>
                                <modelPackage>${openapi-generator.base-package}.payload</modelPackage>
                                <configOptions>
                                    <!-- README#openapi-generator-spring-metadata -->
                                    <generatorLanguage>Java</generatorLanguage>
                                    <generatorType>SERVER</generatorType>
                                    <!-- README#openapi-generator-spring-config-options -->
                                    <artifactDescription>Project to money me with no name [commons-api-oas].</artifactDescription>
                                    <artifactUrl>${project.url}</artifactUrl>
                                    <basePackage>${openapi-generator.base-package}</basePackage>
                                    <bigDecimalAsString>true</bigDecimalAsString>
                                    <booleanGetterPrefix>is</booleanGetterPrefix>
                                    <configPackage>${openapi-generator.base-package}.configuration</configPackage>
                                    <delegatePattern>true</delegatePattern>
                                    <developerEmail>[email protected]</developerEmail>
                                    <developerName>Luis Henrique Pereira</developerName>
                                    <developerOrganization>${project.organization.name}</developerOrganization>
                                    <developerOrganizationUrl>${project.organization.url}</developerOrganizationUrl>
                                    <licenseName>MIT License</licenseName>
                                    <licenseUrl>https://www.opensource.org/licenses/mit-license.php</licenseUrl>
                                    <parentArtifactId>moname-commons</parentArtifactId>
                                    <parentGroupId>${project.parent.groupId}</parentGroupId>
                                    <parentVersion>${revision}</parentVersion>
                                    <performBeanValidation>true</performBeanValidation>
                                    <scmConnection>${project.scm.connection}</scmConnection>
                                    <scmDeveloperConnection>${project.scm.developerConnection}</scmDeveloperConnection>
                                    <scmUrl>${project.scm.url}</scmUrl>
                                    <serializableModel>true</serializableModel>
                                    <title>Project to money me with no name [commons-api-oas].</title>
                                    <unhandledException>true</unhandledException>
                                    <useSpringBoot3>true</useSpringBoot3>
                                    <useSpringController>true</useSpringController>
                                </configOptions>
                            </configuration>
                        </execution>
                    </executions>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>
Kingery answered 1/6, 2023 at 1:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.