7

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.

Marx
  • 804
  • 10
  • 23

2 Answers2

3

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.

Dennis
  • 31
  • 2
0

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);
}
Karthick Nagarajan
  • 1,327
  • 2
  • 15
  • 27
  • I find better solution. You can use `@Parameter(description = "filter",hidden = true)` on you `@QuerydslPredicate`. And `@Parameter` annotation on you endpoint method. [example](https://gist.github.com/alamer/8377785e5ff8162fd3cfc23385cd3901) – loljeene Sep 01 '20 at 09:22