thmasker
thmasker

Reputation: 638

Java OpenApi generator use object as query parameter

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.

Upvotes: 7

Views: 6824

Answers (2)

LMC
LMC

Reputation: 12692

Add @ParameterObject annotation to method signature.

Score getScore(@ParameterObject ScoreRequest request);
}

Upvotes: 5

diogenes_vz
diogenes_vz

Reputation: 87

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/

Upvotes: 0

Related Questions