OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.49k stars 6.5k forks source link

[BUG] [Spring] Generated test classes are marked as sources instead of test #13677

Open mariusmanastireanu opened 1 year ago

mariusmanastireanu commented 1 year ago

Bug Report Checklist

Description

When generating interfaces (invoker) for spring generator the generated test resources are marked as Sources instead of Test Sources. This will break the compilation if test resources are marked as test scope. Also see my response on this question: https://stackoverflow.com/questions/73450027/java-maven-openapi-3-0-codegen-is-generating-unwanted-test-file-how-to-remove

Workaround: run with <interfaceOnly>true</interfaceOnly> (avoid test generation).

openapi-generator version

6.2.0, 6.0.1

OpenAPI declaration file content or url

N/A - anything is sufficient

openapi: 3.0.3
info:
  title: test
  version: "1.0"
servers:
  - url: http://localhost:8080/
    description: Local
tags:
  - name: Target
    description: Targets related REST endpoints.
paths:
  /target:
    get:
      tags:
        - Target
      summary: Retrieves all targets
      description: Returns all targets
      operationId: getTargets
      responses:
        '200':
          description: successful operation
                    <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>${openapi-generator.version}</version>
                <executions>
                    <execution>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <id>generate-files-from-openapi</id>
                        <configuration>
                            <inputSpec>${project.basedir}/src/main/resources/static/api.yaml</inputSpec>
                            <generatorName>spring</generatorName>
                            <library>spring-boot</library>
                            <configOptions>
<!--                                <interfaceOnly>true</interfaceOnly>-->
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Generation Details

spring

Steps to reproduce

Generate sources and check project strcture.

Related issues/PRs
Suggest a fix
nikimicallef commented 1 year ago

Same issue. The workaround is not great because it does not generate the clients which are nice/required.

sebastianblesgen commented 1 year ago

I am wondering:

sebastianblesgen commented 1 year ago

Still in 6.4.0

navaneeth-spotnana commented 1 year ago

@sebastianblesgen did you find any workarounds for this?

sebastianblesgen commented 1 year ago

@sebastianblesgen did you find any workarounds for this?

The only thing I can do is to use the suggested workaround using interfaceOnly 🤷🏻‍♂️

navaneeth-spotnana commented 1 year ago

I just now got rid of the file by adding it to the ignore list:

<ignoreFileOverride>${project.basedir}/.openapi-generator-ignore</ignoreFileOverride>

.openapi-generator-ignore content:

**/OpenApiGeneratorApplicationTests.java
tofi86 commented 7 months ago

Still present in v7.3.0 - but the workaround by @navaneeth-spotnana still works!

martinboue commented 5 months ago

Still present in v7.3.0 - but the workaround by @navaneeth-spotnana still works!

Same in v7.5.0, testOutput configuration seems to be ignored.

patwakeem commented 3 months ago

Still broke

MarcellHarmaci commented 1 week ago

Still present in v7.8.0 ...

@sebastianblesgen did you find any workarounds for this?

The only thing I can do is to use the suggested workaround using interfaceOnly 🤷🏻‍♂️

interfaceOnly is not a sufficient workaround when using <delegatePattern>true</delegatePattern>

Another workaround if the test are not needed is to add another plugin to delete the generated test folder. https://stackoverflow.com/a/74702278/12382368

<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>