Search code examples
spring-bootswaggerspring-webfluxspringfox

Swagger overwrites methods with the same path and method but different parameters


Swagger overwrites methods with the same path and method but different parameters

I have an application with Spring Boot 2.3.5.RELEASE, webflux and springfox 3.0.0. I have developed two GET methods with the same path but different parameters, one does not receive parameters and returns a list and others for findAll.

The case is that Swagger only generates the documentation of one of the methods, sometimes the listing, others the paging. How can I tell swagger that they are different methods and document both for me?

My Controller code:

@GetMapping(value = "/foo", params = {"page", "size"})
@ResponseBody
public Mono<ResponseEntity<Mono<Page<FooDTO>>>> findByFilter(FooFilterDTO filter,
        @SortDefault(sort = "id", direction = Sort.Direction.DESC) @PageableDefault(value = 10) Pageable pageable) {
//...
}

@GetMapping(value = "/foo")
@ResponseBody
public Mono<ResponseEntity<Flux<FooDTO>>> findAll() {
//...
}

My Swagger configuration:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Value("${app.version}")
    private String version;
    
    @Bean
    public Docket docketUsersV1() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(this.fooApiInfo())
                .enable(true)
                .groupName("foo-api")
                .securityContexts(Arrays.asList(securityContext()))
                .securitySchemes(Arrays.asList(apiKey()))
                .select()
                .paths(fooPaths())
                .build();
    }
    
    private ApiInfo fooApiInfo() {
        return new ApiInfoBuilder()
                .title("Reactive Foo")
                .description("Reactive API")
                .version(appVersion)
                .build();
    }

    
    private Predicate<String> fooPaths() {
        return regex("/foo.*");
    }
}

Solution

  • As far as I know, you can only define one API path by each HTTP verb (GET, POST...), independently of the optional parameters the API consumer sends.

    My advice would be to define a single GET /foo path, with optional parameters page & size (ie. not required)

    Then I would have a single entrypoint function in the controller, then redirect to each findByFilter private method or findAll private method depending on whether page & size are defined or not.