springdoc / springdoc-openapi

Library for OpenAPI 3 with spring-boot
https://springdoc.org
Apache License 2.0
3.26k stars 493 forks source link

GroupedOpenApi cannot work correctly. #2741

Closed wanghongzhou closed 4 days ago

wanghongzhou commented 4 days ago

When I use Spring Boot 3.2.10 and Springdoc 2.5.0, I get the desired results. However, when I upgrade to Spring Boot 3.3.4 and Springdoc 2.6, the same code does not produce the expected results. Below is the provided simple code. When running all services, accessing http://localhost:7001/swagger-ui.html gives the desired output, but accessing http://localhost:7002/swagger-ui.html does not yield the correct results

When using Spring Boot 3.2.10 and Springdoc 2.5.0, the gateway returns the correct JSON result at /v3/api-docs/test-service. However, after upgrading, /v3/api-docs/test-service returns the documentation for the gateway itself.

bnasslahsen commented 4 days ago

@wanghongzhou,

Starting from 2.5.0, this is the proper way to configure it, as you are pointing to an external URL.

    @Bean
    public Set<SwaggerUrl> apis(RouteDefinitionLocator locator, SwaggerUiConfigProperties swaggerUiConfigProperties) {
        Set<SwaggerUrl> urls = new HashSet<>();
        List<RouteDefinition> definitions = locator.getRouteDefinitions().collectList().block();
        definitions.forEach(routeDefinition -> {
            String name = routeDefinition.getId();
            SwaggerUrl swaggerUrl = new SwaggerUrl(name, DEFAULT_API_DOCS_URL + "/" + name, null);
            urls.add(swaggerUrl);

        });
        swaggerUiConfigProperties.setUrls(urls);
        return urls;
    }

GroupedOpenApi, is relevant, within the same application, as a scan will be done

This allows, to dynamically define ApiGroups #2584

wanghongzhou commented 4 days ago

@bnasslahsen Thanks for your reply! I just tested the code below, and it works great. Thanks again!

@Autowired
public void configureSwaggerUrls(SwaggerUiConfigProperties swaggerUiConfigProperties, RouteDefinitionLocator locator) {
    swaggerUiConfigProperties.setUrls(locator.getRouteDefinitions()
            .map(routeDefinition -> new SwaggerUrl(routeDefinition.getId(), DEFAULT_API_DOCS_URL + "/" + routeDefinition.getId(), routeDefinition.getId()))
            .toStream()
            .collect(Collectors.toSet()));
}