Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Micronaut for Spring Boot ignores path on @RestController #144

Closed
mikaelvik opened this issue Feb 17, 2020 · 11 comments
Closed

Micronaut for Spring Boot ignores path on @RestController #144

mikaelvik opened this issue Feb 17, 2020 · 11 comments
Labels
info: good first issue Good for newcomers type: bug Something isn't working

Comments

@mikaelvik
Copy link
Contributor

mikaelvik commented Feb 17, 2020

I've created an example project using Micronaut to generate OpenAPI yaml for a Spring Boot application.

Micronaut seems to ignore the path specified on @RestController annotations when generating the yaml file.

The only Swagger annotation I've used is @OpenAPIDefinition on the Micronaut application class (and I'd like to keep it that way).

Controller:

@RestController
@RequestMapping("/api/goodbyes") // edited
public class GoodbyeRestController {

    @GetMapping("/{name}")
    Map<String, String> goodbye(@PathVariable String name) {
        return Collections.singletonMap("goodbye", name);
    }
}

Excerpt from openapi.gradle (applied by build.gradle):

sourceSets {
    openapi {
        compileClasspath += sourceSets.main.compileClasspath
        java {
            srcDirs = [
                'src/main/java',
                'src/openapi/java'
            ]
        }
    }
}

Application class:

@OpenAPIDefinition(info = @Info(title = "mvik-openapi-microboot", version = "0.1"))
public class MicronautApplication {

    public static void main(String[] args) {
        Micronaut.run(MicronautApplication.class);
    }
}

Output:

openapi: 3.0.1
info:
  title: mvik-openapi-microboot
  version: "0.1"
paths:
  /{name}:
    get:
      operationId: goodbye
      parameters:
      - name: name
        in: path
        required: true
        schema:
          type: string
      responses:
        default:
          description: goodbye default response
          content:
            application/json:
              schema:
                type: object
                additionalProperties:
                  type: string
@graemerocher
Copy link
Contributor

According to https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/bind/annotation/RestController.html

The value of @RestController represents the bean name in Spring and not the URI. I believe to get the behaviour you want have to use:

@RestController
@RequestMapping("/api/goodbyes")

@mikaelvik
Copy link
Contributor Author

Sorry for the terrible example code, the problem is also there when using @RequestMapping. I've used the same approach on an existing work project where all the @RequestMapping paths were ignored.

I've changed the example code, and the path is still being ignored.

@mikaelvik
Copy link
Contributor Author

The example uses Micronaut 1.3.1 and Spring Boot 2.2.4.RELEASE.

@graemerocher
Copy link
Contributor

From the looks of this, it seems that Micronaut Spring turns @RequestMapping into Micronaut's @UriMapping here https://github.com/micronaut-projects/micronaut-spring/blob/master/spring-web-annotation/src/main/java/io/micronaut/spring/web/annotation/RequestMappingAnnotationMapper.java#L85

Micronaut OpenAPI only considers the @Controller annotation though unfortunately https://github.com/micronaut-projects/micronaut-openapi/blob/master/openapi/src/main/java/io/micronaut/openapi/visitor/OpenApiControllerVisitor.java#L142

So the fix would be to also consider @UriMapping I imagine.

PRs welcome.

@graemerocher
Copy link
Contributor

In fact since @Controller is an alias for @UrlMapping in Micronaut https://github.com/micronaut-projects/micronaut-core/blob/7f48aac4c8090974e56d1cc89830cb6b201dc1d2/http/src/main/java/io/micronaut/http/annotation/Controller.java#L58

Probably only @UriMapping needs to be considered

@graemerocher graemerocher added type: bug Something isn't working info: good first issue Good for newcomers labels Feb 17, 2020
@mikaelvik
Copy link
Contributor Author

I'll give it a try. Thanks for the feedback.

mikaelvik pushed a commit to mikaelvik/micronaut-openapi that referenced this issue Feb 17, 2020
…n the element's declaring type instead of Controller.
mikaelvik pushed a commit to mikaelvik/micronaut-openapi that referenced this issue Feb 17, 2020
…n the element's declaring type instead of Controller.
mikaelvik added a commit to mikaelvik/micronaut-openapi that referenced this issue Feb 17, 2020
…n the element's declaring type instead of Controller.
@mikaelvik
Copy link
Contributor Author

PR created: #150

@mikaelvik
Copy link
Contributor Author

The fix worked on my example project, that's at least something. Thanks for the pointers.

graemerocher pushed a commit that referenced this issue Feb 18, 2020
@graemerocher
Copy link
Contributor

@mikaelvik you can try the just release 1.4.0 version in your project. Let us know how it goes. Hope you write a blog post about what you are doing, seems really interesting :)

@mikaelvik
Copy link
Contributor Author

@mikaelvik you can try the just release 1.4.0 version in your project. Let us know how it goes. Hope you write a blog post about what you are doing, seems really interesting :)

The release worked perfectly, thanks. Looking forward to the next final release of Micronaut.

I'm not much of a blogger, but I'd be happy to contribute with a section to the docs. The idea is to use a separate source set for the openapi stuff to avoid leaking dependencies into the classpath of the Spring Boot application.

@graemerocher
Copy link
Contributor

@mikaelvik sounds good

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
info: good first issue Good for newcomers type: bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants