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.79k stars 6.57k forks source link

[BUG] Java/Spring: @RequestMapping is unconditionally added to controller with property expression #13552

Open uweschaefer opened 2 years ago

uweschaefer commented 2 years ago
Description

When generating Java/Spring, since 6.1.0 a @RequestMapping annotation is added to every controller type, that contains a property with a supposed default value.

See {{=<% %>=}} @RequestMapping("${openapi.<%title%>.base-path:<%>defaultBasePath%>}") <%={{ }}=%>

The annotation contains a property expression, that is not resolvable from spring-hateoas, which breaks any hateoas application by creating broken self links like 'http://localhost:8080/${openapi.myProjectName.base-path}/', as the property is not resolved.

In our case

@Generated(value = "org.openapitools.codegen.languages.SpringCodegen", date = "2022-09-29T19:10:21.734133+02:00[Europe/Berlin]")
@Validated
@Tag(name = "default", description = "the default API")
@RequestMapping("${openapi.myProject.base-path:}")
public interface DefaultApi {
...

(Also, we assume the > char in >defaultBasePath is a typo?)

OpenAPI declaration file content or url

any API will do as the annotation is added unconditionally

Generation Details

using maven plugin with the following config:

<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>${open-api-plugin.version}</version>
    <executions>
        <execution>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
                <inputSpec>${project.basedir}/src/main/resources/open-api.yml</inputSpec>
                <generatorName>spring</generatorName>
                <apiPackage>something.gen.rest</apiPackage>
                <modelPackage>something.gen.model</modelPackage>
                <generateApiDocumentation>false</generateApiDocumentation>
                <generateModelDocumentation>false</generateModelDocumentation>
                <generateApis>true</generateApis>
                <generateModels>true</generateModels>
                <library>spring-boot</library>
                <additionalProperties>
                    <!-- currently ignored due to a bug, but should work later -->
                    <!-- https://github.com/OpenAPITools/openapi-generator/issues/8192 -->
                    <additionalProperty>
                        useSpringfox=false
                    </additionalProperty>
                </additionalProperties>
                <configOptions>
                    <interfaceOnly>true</interfaceOnly>
                    <skipDefaultInterface>true</skipDefaultInterface>
                    <sourceFolder>src/gen/java/</sourceFolder>
                    <hateoas>true</hateoas>
                </configOptions>
            </configuration>
        </execution>
    </executions>
</plugin>
openapi-generator version

6.1.0, 6.2.0

Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/13488

Suggest a fix

make it possible to opt out of @RequestMapping annotation generation.

uweschaefer commented 2 years ago

looks like https://github.com/OpenAPITools/openapi-generator/pull/7014 might solve the issue

jorgerod commented 2 years ago

Hi

I have the same problem. In my opinion, a breaking change has been added in version 6.1.0. https://github.com/OpenAPITools/openapi-generator/pull/10573

We have a lot of interfaces implemented by classes with @RestController and without @RequestMapping. When adding @RequestMapping to the interface they no longer work.

Please, could we add a property to disable this new behavior? In this PR it has been added to take into account that it is not used for feign.

How do you see it? @cachescrubber @welshm @MelleD @atextor @manedev79 @javisst @borsch @Zomzog @wing328

Marcin-Kwiatkowski-NA commented 1 month ago

I have the same problem with @RequestMapping("${openapi.myProject.base-path:}") on annotated interfaces on version 7.8.0 of openapi-generator-maven-plugin

        <plugin>
            <groupId>org.openapitools</groupId>
            <artifactId>openapi-generator-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                    <configuration>
                        <inputSpec>${project.basedir}/src/main/resources/api/openapi.yaml</inputSpec>
                        <generatorName>spring</generatorName>
                        <configOptions>
                            <sourceFolder>src/main/java</sourceFolder>
                            <dateLibrary>java8</dateLibrary>
                            <library>spring-boot</library>
                            <hideGenerationTimestamp>true</hideGenerationTimestamp>
                            <modelPackage>mkpw.blablatwo.model</modelPackage>
                            <apiPackage>mkpw.blablatwo.api</apiPackage>
                            <invokerPackage>mkpw.blablatwo.api</invokerPackage>
                            <serializableModel>false</serializableModel>
                            <useTags>true</useTags>
                            <useGzipFeature>true</useGzipFeature>
                            <hateoas>true</hateoas>
                            <unhandledException>true</unhandledException>
                            <useSpringBoot3>true</useSpringBoot3>
                            <requestMappingMode>none</requestMappingMode>
                            <useSwaggerUI>true</useSwaggerUI>
                            <importMappings>
                                <importMapping>ResourceSupport=org.springframework.hateoas.RepresentationModel</importMapping>
                                <importMapping>Link=org.springframework.hateoas.Link</importMapping>
                            </importMappings>
                            <ignoreFileOverride>${project.basedir}/src/main/resources/api/.openapi-generator-ignore</ignoreFileOverride>
                            <interfaceOnly>true</interfaceOnly>
                        </configOptions>
                        <output>${project.build.directory}/openapi-code-blaBlaTwo</output>
                        <supportingFilesToGenerate>
                            ApiUtil.java
                        </supportingFilesToGenerate>
                        <generateModels>true</generateModels>
                        <generateApis>true</generateApis>
                    </configuration>
                </execution>
            </executions>
        </plugin>