question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

WebFlux functional DSL does not recognise query parameters

See original GitHub issue

This route from the WebFlux functional demo https://github.com/springdoc/springdoc-openapi-demos/blob/ff9b81f88ce1c66ffb5bf70d9bbbd69f5d77aed6/springdoc-openapi-spring-boot-2-webflux-functional/src/main/java/org/springdoc/demo/app4/user/RoutingConfiguration.java#L21 has 2 issues:

  1. user parameter is documented as type “string” but it should be a User object. But this can be fixed by accepting User instead of Mono<User>, so it’s not the fault of springdoc.
  2. If I change the id parameter from PATH to QUERY then it is documented as a single body schema that contains id field and user field. But I want id to be a query parameter and user to be in the request body. @RequestBody on user is ignored.
Screenshot 2020-12-04 at 19 50 22

also, maven project doesn’t seem to include -parameters compiler flag, so the real parameter names aren’t seen by reflection

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
bnasslahsencommented, Dec 4, 2020

@Sam-Kruglov,

I see what you want now.

If you just respect the contribution guide, you will make your requests easier for you and for us.

This is what is already written: If you are reporting a bug, please help to speed up problem diagnosis by providing as much information as possible:

  • You need to describe your context (the title of an issue is not enough)
  • What version of spring-boot you are using?
  • What modules and versions of springdoc-openapi are you using?
  • What are the actual and the expected result using OpenAPI Description (yml or json)?
  • Provide a Minimal, Reproducible Example - with HelloController that reproduces the problem

Please make sure you respect this guidelines if you open any new issue.

0reactions
Sam-Kruglovcommented, Dec 12, 2020

Similar issue is still present for me. I will open another issue if I have time. But basically, if I specify beanClass and methodName, and there I have parameters MyDto, String, String and the operation is not GET, then the resulting spec will have a request body with all these parameters as a single object similar to what we have here on the screenshot.

I say that any request body that does not set $ref to /components/schema is wrong.

If I specify @RequestBody MyDto, @Parameter String, @Parameter String then nothing changes. I have to explicitly say @RequestBody MyDto, @Parameter(in=QUERY) String, @Parameter(in=QUERY) String for it to change as I want.

I think it is a reasonable default to assume the dto is in the body and any literals is a query parameter unless specified otherwise. Always building a composite request body seems a very strange approach. I saw there a condition to always create a request body if the HTTP method is anything except GET (see the only usage of org.springdoc.core.RequestBodyService#calculateRequestBodyInfo). I might not have any body in POST request. Hopefully that helps.

upd: current workaround:

/**
 * If a method has parameters (DTO, String, String) it will create a request body with 3 fields containing all these.
 * We could annotate every single place with [io.swagger.v3.oas.annotations.Parameter] or we could write a customizer
 * like this one to pick out all the non-complex objects out of request body
 */
@Bean
fun moveSimplePropsFromBodyToQuery() = OperationCustomizer { operation, _ ->
  val content = operation.requestBody?.content?.get(APPLICATION_JSON_VALUE)
  if (content?.schema != null && content.schema.`$ref` == null) {
    val simpleProperties = content.schema.properties.entries.filter { (_, value) -> value.`$ref` == null }
    simpleProperties.forEach { content.schema.properties.remove(it.key) }
    Assert.state(
        content.schema.properties.size <= 1
    ) {
      "Found more than one complex property in method parameters of ${operation.operationId}. " +
          "Consider annotating them with @RequestBody or @ParameterObject, or declaring as a composite single object."
    }
    if (content.schema.properties.size == 1) {
      content.schema = content.schema.properties.values.first()
      // for some reason it's marked as not required by default
      operation.requestBody.required(true)
    } else {
      // empty -- found merged schema with only simple properties
      operation.requestBody = null
    }
    simpleProperties.forEach { (name, schema) ->
      //schema.nullable is always null
      operation.addParametersItem(QueryParameter().name(name).schema(schema).required(!(schema.nullable ?: false)))
    }
  }
  operation
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Spring Boot Webflux: RouterFunctions Add Query Parameters
There is a queryParam() method on the RequestPredicates class you can use. RouterFunctions.route() .GET("/one", RequestPredicates.
Read more >
Web on Reactive Stack - Spring
Both Spring MVC and WebFlux controllers support reactive (Reactor and RxJava) return types, and, as a result, it is not easy to tell...
Read more >
Non-blocking with Spring WebFlux, Kotlin and Coroutines
In this article, we will develop a simple RESTful API using Spring WebFlux and aim to leverage the special Kotlin extensions in Spring....
Read more >
[Solved]-Spring And Kotlin Query-Springboot - appsloveworld
Coding example for the question Spring And Kotlin Query-Springboot. ... This is possible because the Order is appended to the given query string....
Read more >
springdoc-openapi v2.0.0
Spring-webflux/WebMvc.fn with Functional Endpoints. Since version v1.5.0, a functional DSL has been introduced, thanks to this enhancement ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found