7

I am having the following controller:

public interface GetScoreController {
  @GetMapping(value = "/score", produces = MediaType.APPLICATION_JSON_VALUE)
  @Operation(parameters = {@Parameter(in = ParameterIn.QUERY, name = "request")})
  Score getScore(ScoreRequest request);
}

And I'd like OpenApi to show all the attributes in ScoreRequest as query params when generating the Swagger documentation since this is the result when the request is a POJO:

enter image description here

I don't know if actually, OpenApi allows this, but if I have too many request params it is more useful to collect them in a unique POJO.

Sam Joshua
  • 310
  • 6
  • 17
thmasker
  • 406
  • 1
  • 9
  • 21
  • 2
    Seems you need the [@ParameterObject](https://github.com/springdoc/springdoc-openapi/pull/505) annotation in your method signature. – LMC Jun 02 '21 at 23:34
  • 1
    @LuisMuñoz this is exactly what I'm looking for. Thanks! – thmasker Jun 03 '21 at 07:20
  • 1
    Turned comment into an answer since @thmasker confirmed it solved his issue ;-) – LMC Jun 03 '21 at 13:29

2 Answers2

3

Add @ParameterObject annotation to method signature.

Score getScore(@ParameterObject ScoreRequest request);
}
LMC
  • 10,453
  • 2
  • 27
  • 52
0

Apparently you trying to develop a RESTful or a REST based implementation... In this case, is not recomended that your GET endpoint use Objects in it, only Path Params or Query Params.

It's very common to see APIs with lots of query params as filter options. Don't be affraid.

But, i think that your method getScore should only receive scoreId as a path param (or even be query param too), as a best practices of OOP.

See this article on Stack, it's very focused on best parctices in Rest Api Design: https://stackoverflow.blog/2020/03/02/best-practices-for-rest-api-design/

  • 1
    Thanks for your feedback! I'll definitely take it into account. But your answer has absolutely nothing to do with my question... – thmasker Jun 01 '21 at 12:36
  • 1
    Sorry @thmasker i wasn't too clear... Is not possible to do what you want. There's no way to print an Object's properties in query params of your OpenApi Specification. The only way is doing it manually, setting manually every single property you want. – diogenes_vz Jun 01 '21 at 16:43