1 year ago

#386760

test-img

Christophe Andrey

OpenAPI, contract-first with Springdoc and multiple specs

I am trying to port a Swagger UI from Springfox to Springdoc. The Swagger UI is generated via Maven plugin

<groupId>org.openapitools</groupId>
<artifactId>openapi-generator-maven-plugin</artifactId>

... from two different OpenAPI specifications, both implemented in the same Spring Boot application. We use two different specs for two different clients. So far, my Swagger UI looked like this with Springfox. Note the drop-down list that allows to choose the spec to display.

Now, with Springdoc 1.6.6, the Swagger-UI looks like this. Note that:

  • The default landing page is the sample Pet Store and none of my specifications, although I specified in my Spring Boot's application.yaml:

    springdoc:
      swagger-ui:
        disable-swagger-default-url: true
    
  • No drop-down

  • I have to manually enter my specification's name into the "Explore" text field in order to see its Swagger UI

  • I tried addressing the missing drop-down by following I have installed OpenAPI 3 using springdoc, but the URL is strange. Can I change it to the expected value?, which claims to display a drop-down, but to no avail.

My questions:

  • How do I display the drop-down? Does Springdoc support it at all out-of-the-box?
    • Apparently, accessing http://localhost:8080/swagger-ui/index.html?urls.primaryName=Information makes the drop-down list of specs magically appear.
  • How do I make sure that the default URL http://localhost:8080/swagger-ui/index.html lands on one of my specs, and not on the Pet Store?

Here is btw. my Bean configuration:

@Configuration
public class SwaggerDocumentationConfig {

/**
 * Path of the OpenAPI package extracted out of a random class in that package
 */
private static final String INFORMATION_PACKAGE = InformationApi.class.getPackageName();
/**
 * Path of the OpenAPI package extracted out of a random class in that package
 */
private static final String OPERATIONS_PACKAGE = OperationsApi.class.getPackageName();

private GroupedOpenApi getBaseApiDoc(String groupName, String packagePath) {
    return GroupedOpenApi.builder()
        .group(groupName)
        .packagesToScan(packagePath)
        .build();
}

@Bean
public GroupedOpenApi getOperationsApiDoc() {
    return getBaseApiDoc("Operations", OPERATIONS_PACKAGE);
}

@Bean
public GroupedOpenApi getInfoApiDoc() {
    return getBaseApiDoc("Information", INFORMATION_PACKAGE);
}
@Bean
public OpenAPI springShopOpenAPI() {
    return new OpenAPI()
        .info(new Info().title("SpringShop API")
            .description("Spring shop sample application")
            .version("v0.0.1")
            .license(new License().name("Apache 2.0").url("http://springdoc.org")))
        .externalDocs(new ExternalDocumentation()
            .description("SpringShop Wiki Documentation")
            .url("https://springshop.wiki.github.org/docs"));
}}

swagger-ui

springdoc

springdoc-openapi-ui

openapi-generator-maven-plugin

0 Answers

Your Answer

Accepted video resources