1

I would like to create OpenAPI UI spec, using Spring WebFlux functional router on Kotlin. Spring Boot v.2.5.2, springdoc-openapi v.1.5.9. Here is my router class:

@Configuration
class UserServiceRouter {

    @Bean
    @RouterOperations(
        RouterOperation(
            path = "/user/{id}",
            method = [RequestMethod.GET],
            beanClass = UserService::class,
            beanMethod = "getUserCredentialsById",
        operation = Operation(
            parameters = [Parameter(
                style = ParameterStyle.SIMPLE,
                explode = Explode.FALSE,
                `in` = ParameterIn.PATH
            )]
        )
        ),
        RouterOperation(
            path = "/user/profile/{id}",
            method = [RequestMethod.GET],
            beanClass = UserService::class,
            beanMethod = "getUserProfileById",
        operation = Operation(
            parameters = [Parameter(
                style = ParameterStyle.SIMPLE,
                explode = Explode.FALSE,
                `in` = ParameterIn.PATH
            )]
        )
    )
    fun userRoute(userServiceHandler: UserServiceHandler) = coRouter {
        ("/user").nest {
            GET("/{id}", userServiceHandler::getUserCredentialsById)
            GET("/profile/{id}", userServiceHandler::getUserById)
        }
    }
}

I want to create a GET query like

http://localhost:8080/user/1

where 1 is my desired user`s ID.

But my OpenAPI`s UI still tries to create query with GET parameter

http://localhost:8080/user/{id}?id=1

which obviously leads to error 400. Would be nice to know how my @RouterOperations should be configured appropriately.

Yaroslav
  • 67
  • 2
  • 8

1 Answers1

1

The correct config for Path Variables looks like (I`ve left only one GET query here for brevity):

@Bean
@RouterOperations(
    RouterOperation(
        path = "/user/{id}",
        method = arrayOf(RequestMethod.GET),
        beanClass = UserService::class,
        beanMethod = "getUserCredentialsById",
        operation = Operation(
            operationId =  "getUserCredentialsById",
            method = "GET",
            parameters = [
                Parameter(
                    name = "id",
                    `in` = ParameterIn.PATH,
                    style = ParameterStyle.SIMPLE,
                    explode = Explode.FALSE,
                    required = true
                )]
        )
    )
)
fun userRoute(userServiceHandler: UserServiceHandler) = coRouter {
    path("/user")
        .nest {
            GET("/{id}", userServiceHandler::getUserCredentialsById)
        }
}

See specs here. Important points:

  • Operation ID is mandatory
  • Default value in Operation parameters[] is "query parameters" instead of "path parameters" + we should explicitly point parameter name (name = "id") in order to override default behavior.
  • As provided in question, ParameterIn.PATH is also mandatory.
Yaroslav
  • 67
  • 2
  • 8