OpenAPITools / OpenAPITools/openapi-generator

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

Open
#13,552 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Reproduce the issue with the Maven plugin configuration shown, using the Java/Spring generator with the spring-boot library and hateoas enabled. Inspect the Spring generator's controller annotation generation and configuration options. Done means an opt-out is available and generated controllers no longer include the unresolved @RequestMapping expression when it is disabled.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring, spring-boot
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.