In springdoc, is it possible to show both OpenAPI generated UIs and ones off custom static yml files in the same set of groups?

Viewed 32

I'm building enhancements off a 3rd party software with my own APIs. I have the 3rd party's static .yml files for swagger, but for my own endpoints, I'm using the springdoc annotations and OpenAPI bean declarations to generate the documentation. Can I show both in the same swagger doc?

Dynamically generated doc:

@Configuration
public class SwaggerConfiguration {
   @Bean
   public GroupedOpenApi myOpenApi() {
      String group = "My Group API";
      String paths[] = { "/**" };
      GroupedOpenApi api = GroupedOpenApi.builder()
           .group(group)
           .pathsToMatch(paths)
           .packagesToScan("org.mypackage")
           .addOpenApiCustomiser(openApi -> {
                   openApi.setInfo(
                        new Info()
                            .title(group)
                            .description("[Base URL: /myapi ]")
                   );
           }).build();
           return api;
   }
}

Static .yml settings in application.yml:

springdoc:
   swagger-ui:
     urls:
       - name: My Group 1
         display-name: My Group 1
         url: group1.yml
       - name: My Group 2
         display-name: My Group 2
         url: group2.yml

And so by default, springdoc will show my dynamically generated API in swagger, but once I add the static settings, the static file driven swagger groups show up and the dynamic one isn't shown. Is it possible to show both?

1 Answers

I figured out a solution, but it might be a hack. Please let me know if there's a proper way to do this...

The dynamically generated openapi java beans are still executing even if I add the static settings, so the dynamic URL must still exist despite not being shown by springdoc when loading the swagger page.

So to get both static and dynamic swagger content to display using springdoc:

  1. Run your spring-boot app with just the dynamically generated swagger (no static settings in application.yml or comment it out)

  2. In the swagger page it generates, there should be a url link to your openapi generated file (http://localhost:8080/v3/api-docs/My%20Group%20API in this case). Click it and it'll open into a new tab.

  3. Add in the static settings to the application.yml file and add in another set entry that includes that url:

springdoc:
   swagger-ui:
     urls:
       - name: My Group 1
         display-name: My Group 1
         url: group1.yml
       - name: My Group 2
         display-name: My Group 2
         url: group2.yml
       - name: My Group API
         display-name: My Group API
         url: /v3/api-docs/My%20Group%20API

Start your spring-boot app and you'll have both your static file swagger groups and your dynamic group in the pulldown

Related