'Document QueryDSL endpoint with Swagger

I'm using Spring Boot Data, QueryDSL and Swagger. I've define endpoint like this:

@GetMapping
public ResponseEntity<?> listOfThings(
        @PageableDefault(size = 20, sort = "uID", direction = Sort.Direction.DESC) final Pageable pageable,
        @QuerydslPredicate(root = Thing.class) final Predicate predicate)

However Swagger define only variables: page, size, sort - it doesn't seem to parse Entity to show all fields as filterable.

I have repository like this:

@Repository
public interface ThingRepository
        extends JpaSpecificationExecutor<Thing>, CrudRepository<Thing, String>, PagingAndSortingRepository<Thing, String>,
        QuerydslPredicateExecutor<Thing>, QuerydslBinderCustomizer<QThing>
{
    @Override
    default void customize(final QuerydslBindings bindings, final QThing thing)
    {
        bindings.bind(thing.status).first((status, value) -> status.eq(value));
        bindings.bind(thing.recipient).first(StringExpression::containsIgnoreCase);
        bindings.bind(String.class).first((StringPath path, String value) -> path.containsIgnoreCase(value));
    }

}

I expect Swagger to display all String fields as filters, especially status & recipient which are strictly defined.



Solution 1:[1]

Define some dummy parameters and add all query parameters as RequestParams but do not use them ... just use the predicate. We use this as a workaround to support swagger file for codegeneration. Not perfect but works!

public Iterable<SCGameInfo> findSCGameInfo(
  @QuerydslPredicate(root = GameInfo.class) Predicate predicate,
  @RequestParam(name= "gameName",required = false) String gameName,
  @RequestParam(name= "vendor",required = false) String vendor
){
 return  scService.findAllGameInfosForPredicate(predicate);
}

Solution 2:[2]

Use maven dependency:

    <dependency>
        <groupId>org.springdoc</groupId>
        <artifactId>springdoc-openapi-data-rest</artifactId>
        <version>1.6.8</version>
    </dependency>

And specify your endpoint like this:

@GetMapping(produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<Case>> getCasesByQuery(@ParameterObject
                                                  @QuerydslPredicate(root = Case.class, bindings = CaseRepository.class) Predicate predicate,
                                                  @ParameterObject Pageable pageable) {

Now you should see all query params in SwaggerUI.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Karthick Nagarajan
Solution 2 Dennis