openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] Invalid @HeadMapping annotation generated in Spring controller

Open gonzalad opened this issue 5 years ago • 5 comments

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 spring on the previous openapi file.

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
  1. One way to fix that would be to generate RequestMapping whenever the HEAD method is used:
        @RequestMapping(value = "/pets",
        method = RequestMethod.HEAD)
  1. 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.

gonzalad avatar Apr 27 '21 12:04 gonzalad

Same issue is present for OPTIONS and TRACE It will generate a OptionsMapping and TraceMapping annotation. Which do not exist.

kroutal avatar Jun 10 '21 15:06 kroutal

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.

jorgerod avatar Jun 29 '21 08:06 jorgerod

I have the same problem here.

Using HEAD and getting

error: cannot find symbol
    @HeadMapping(
     ^
  symbol:   class HeadMapping
  location: interface Api

dickhaut avatar Aug 06 '21 07:08 dickhaut

Hi, it seems this issue was fixed in 5.2.1 https://github.com/OpenAPITools/openapi-generator/pull/9900

kroutal avatar Oct 08 '21 09:10 kroutal

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.

SubhamAshok avatar Aug 02 '26 11:08 SubhamAshok