Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Maven OpenApi Specification (3.0) Codegen is generating unwanted test file, how to remove that?

I am using OpenApi Specification (3.0) for api definition and the openapi-generator-maven-plugin which generates files for me (api objects + endpoints).

Its however generating a test file in the build folder that I do not want. Its called 'OpenApiGeneratorApplicationTests'. It always blocks my compilation bc in the 'target' (=build) folder I do not have the right Spring Boot setup.

How can I avoid the generation of this test file?

This is my maven config:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>6.0.1</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/ApiDef.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>
    </executions>
</plugin>
like image 697
M Hot Avatar asked Oct 25 '25 10:10

M Hot


2 Answers

I've encountered today the same issue. So long story short:

By default the generator maven plugin for Spring will generate the 'invoker' - which by implementation is actually a @SpringBootApplication starter class.

@SpringBootApplication
@ComponentScan(basePackages = {"com.mypackage.invoker", "com.mypackage.api" , "org.openapitools.configuration"})
public class OpenApiGeneratorApplication {

    public static void main(String[] args) {
        SpringApplication.run(OpenApiGeneratorApplication.class, args);
    }

    @Bean
    public Module jsonNullableModule() {
        return new JsonNullableModule();
    }

}

and will also generate the associated test class OpenApiGeneratorApplicationTests

@SpringBootTest
class OpenApiGeneratorApplicationTests {

    @Test
    void contextLoads() {
    }

}

The problem here is that the src/test/com/mypackage/invoker/OpenApiGeneratorApplicationTests.java, and actually the whole package under test is marked as source, not as test-source and in my case this will result in compile time errors (since the test-scoped dependencies are not found).

I managed to get around it by configuring the maven plugin with

<configOptions>
    <interfaceOnly>true</interfaceOnly>
    ...
</configOptions>

which will skip the generation of the SpringBootApplication and the associated Tests.

I've also submitted a ticket to the OpenApi Generator project's github for marking the

like image 151
Marius Manastireanu Avatar answered Oct 27 '25 00:10

Marius Manastireanu


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>
like image 38
kinlhp Avatar answered Oct 27 '25 00:10

kinlhp



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!