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

Document @PathVariable if java interface is used

Open PakhomovAlexander opened this issue 2 years ago • 1 comments

Issue description

Hello, I did some trivial changes in my project that uses micronaut-openapi and spend a lot of time trying to figure out what was going on. Here is what happened:

  • I used classes annotated with @Controller and one of them had a method declaration like @Get("/{path}") String getByPath(@PathVariable String path);
  • all controllers were located in maven sub modules but not in the module that generated the openapi spec out of them
  • then I just changed the controller declaration from class to interface and I expected the openapi spec is generated as always
  • got the compilation error like "path variable name: 'arg0' not found in path".

After reading the source code of micronaut-openapi project I found the root cause. Java compiles interfaces without original method variable names and the declaration @Get("/{path}") String getByPath(@PathVariable String path); were not valid because @PathVariable pointed at "arg0" variable name but @Get("/{path}") used "path" variable name. The fix is obvious: @Get("/{path}") String getByPath(@PathVariable("path") String path); but really triky to figure out if the error happened.

Notes:

  • Placing a "broken" interface at the same maven module where openapi spec generation happened solved the issue. This helped probably because the maven compiler plugin had an access to the interface AST and resolved variable names correctly.
  • Crasy thing like @Get("/{arg0}") String getByPath(@PathVariable String path); solved the issue as well.

It would be very useful to point out this behavior in the documentation. So, users won't spend their time trying to understand what is going on.

Also, micronaut-core had the same problem but with another case.

Thank you.

PakhomovAlexander avatar Jul 17 '22 12:07 PakhomovAlexander

@PakhomovAlexander Hi! Could you create sample project to reproduce this problem?

altro3 avatar Jul 23 '22 14:07 altro3