Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for MultipleOpenApiSupport when a Bean of type List<GroupedOpenApi> is present #1919

Closed
rajadilipkolli opened this issue Oct 29, 2022 · 4 comments
Labels
wontfix This will not be worked on

Comments

@rajadilipkolli
Copy link

rajadilipkolli commented Oct 29, 2022

Is your feature request related to a problem? Please describe.

  • I would like to create an aggregate Swagger API in the spring-cloud-gateway service using GroupedOpenAPI. To achieve this, I'm trying to trigger the definitions from RouteDefinitionLocator using below sample code
    @Bean
    List<GroupedOpenApi> groupedOpenApiList(RouteDefinitionLocator locator) {
        return locator.getRouteDefinitions()
                .filter(
                        routeDefinition ->
                                routeDefinition.getId().matches(".*-service"))
                .map(
                        routeDefinition -> {
                            String name =
                                    routeDefinition
                                            .getId()
                                            .replaceAll("-service", "");
                            return GroupedOpenApi.builder()
                                    .pathsToMatch("/" + name + "/**")
                                    .group(name)
                                    .build();
                        })
                .toStream()
                .toList();
    }

I expect MultipleOpenApiSupportConfiguration to trigger during the AutoConfiguration Scanning as spring boot application contains the Bean of type List<GroupedOpenApi>, however this is not triggered because of the failed condition @Conditional(MultipleOpenApiSupportCondition.class) . This is failing as MultipleOpenApiGroupsCondition.class only accepts a single GroupedOpenApi Bean or conditional property

Describe the solution you'd like

  • Solution to make MultipleOpenApiGroupsCondition.java accept List<GroupedOpenApi> to make the conditional annotation become true

Describe alternatives you've considered

  • Declaring the SpringBoot Main class with below package scanning is making is pass but this is wrong approach
    @SpringBootApplication(scanBasePackages = {"org.springdoc.webflux.api", "com.example.api.gateway"})

Additional context

  • Below is the sample code used to demonstrate the idea
    demo.zip
bnasslahsen added a commit that referenced this issue Nov 14, 2022
@rajadilipkolli
Copy link
Author

Thanks @bnasslahsen, when is this available for testing?

Should I be waiting till 2.0.0-RC2?

@bnasslahsen
Copy link
Collaborator

bnasslahsen commented Nov 14, 2022

@rajadilipkolli,

I believe you have few workarounds.
I have reverted this change. Which is a breaking change when services are deployed outside of spring-cloud-gateway

bnasslahsen added a commit that referenced this issue Nov 19, 2022
@bnasslahsen bnasslahsen added the wontfix This will not be worked on label Nov 19, 2022
bnasslahsen added a commit that referenced this issue Nov 19, 2022
@celcius112
Copy link

hello @bnasslahsen, I arrive a bit late but would it be possible that you elaborate on I believe you have few workarounds ? I have the same need as what was described in this issue, and the initial commit resolves it.

@rajadilipkolli
Copy link
Author

Hi @celcius112 ,

Use below code for grouping as an alternative

    @Bean
    public List<GroupedOpenApi> groupedOpenApis(
            RouteDefinitionLocator locator, SwaggerUiConfigProperties swaggerUiConfigProperties) {
        Set<AbstractSwaggerUiConfigProperties.SwaggerUrl> swaggerUrls =
                swaggerUiConfigProperties.getUrls();
        return locator.getRouteDefinitions()
                .toStream()
                .map(RouteDefinition::getId)
                .filter(id -> id.endsWith("-service"))
                .map(
                        routeDefinitionId -> {
                            String name = extractNameFromRouteDefinitionId(routeDefinitionId);

                            swaggerUrls.add(
                                    new AbstractSwaggerUiConfigProperties.SwaggerUrl(
                                            name,
                                            "/" + routeDefinitionId + "/v3/api-docs",
                                            routeDefinitionId));
                            return GroupedOpenApi.builder()
                                    .pathsToMatch("/" + name + "/**")
                                    .group(name)
                                    .build();
                        })
                .toList();
    }

    private String extractNameFromRouteDefinitionId(String routeDefinitionId) {
        return routeDefinitionId.replace("-service", "");
    }

Sample can be found here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants