[BUG] Invalid @HeadMapping annotation generated in Spring controller
Description
Executing the Spring generator on an Open API file having a HEAD method results in a compilation issue: the generator adds a @HeadMapping annotation which does not exist in spring.
One way to fix that would be to generate RequestMapping whenever the HEAD method is used:
@RequestMapping(value = "/pets",
method = RequestMethod.HEAD)
The issue was introduced by this commit: 8bd2dd4c9dc0d8445f1986d024ce1bd0552b0001
openapi-generator version
This issue affects openapi-generator from 5.0.0, 5.0.1 and 5.1.0.
OpenAPI declaration file content or url
https://gist.github.com/gonzalad/50a657c2ffbafeb85d955672d192e9d2
Generation Details
Just use
Steps to reproduce
See https://github.com/gonzalad/openapi-headmapping-issue
- clone the repo
- execute
mvn clean compile
And look at the PetsApi.java class that was generated, we see HeadMapping annotation which does not exist in Spring:
@HeadMapping(
value = "/pets",
produces = { "application/json" }
)
default ResponseEntity<List<Pet>> listPets(@ApiParam(value = "How many items to return at one time (max 100)") @Valid @RequestParam(value = "limit", required = false) Integer limit) {
Before the 5.0.0, the generated code was
@RequestMapping(value = "/pets",
method = RequestMethod.HEAD)
Related issues/PRs
None known
Suggest a fix
- One way to fix that would be to generate RequestMapping whenever the HEAD method is used:
@RequestMapping(value = "/pets",
method = RequestMethod.HEAD)
- Another way would be to revert this commit: 8bd2dd4c9dc0d8445f1986d024ce1bd0552b0001 And just use @RequestMapping instead of the new PutMapping/GetMapping/etc... The advantage with this approach imo is that the generator code remains simple.
Same issue is present for OPTIONS and TRACE It will generate a OptionsMapping and TraceMapping annotation. Which do not exist.
Hi I have the same problem.
Regarding the possible solutions proposed by @gonzalad, I think the best option would be to revert the commit and improve this behaviour in future improvements.
I have the same problem here.
Using HEAD and getting
error: cannot find symbol
@HeadMapping(
^
symbol: class HeadMapping
location: interface Api
Hi, it seems this issue was fixed in 5.2.1 https://github.com/OpenAPITools/openapi-generator/pull/9900
I tested this on latest master (7.25.0-SNAPSHOT, built locally) with a spec containing a HEAD operation:
java -jar openapi-generator-cli.jar generate -g spring -i head-test.yaml -o out --additional-properties interfaceOnly=true,useSpringBoot3=true
The generated API now uses the correct annotation:
@RequestMapping(
method = RequestMethod.HEAD,
...
No @HeadMapping is generated anymore. This was fixed at some point after the report. I think this issue can be closed.