3

We are porting springfox to springdoc and are having issues getting the global parameters and default responses to show up in the /v3/api-docs response.

They show up fine in the Swagger UI but not in the json returned from /v3/api-docs. We are generating code from these API docs.

Was able to get the headers to show up under the components section but the headers and responses do not show up under each endpoint in the json api-docs output like it did with springfox.

@Bean
public GroupedOpenApi groupedOpenApi() {
    final OperationCustomizer globalHeader = (operation, handlerMethod) -> {
        operation.addParametersItem(new HeaderParameter()
            .$ref("#/components/parameters/testheader"));
        return operation;
    };

    return GroupedOpenApi.builder()
        .group("default").pathsToMatch("/**")
        .addOperationCustomizer(globalHeader)
        .addOpenApiCustomiser(getResponseMessages()).build();
}

@Bean
public OpenAPI openApi() {
    return new OpenAPI()
        .info(new Info().title("testing").description("testing").termsOfService("")
            .license(new License().name("").url("")).version("1.0"))
        .components(new Components()
            .addParameters(
                "testheader",
                new Parameter()
                    .in(ParameterIn.HEADER.toString())
                    .name("testheader").description("test header")
                    .required(true).example("sdfdsafsf").schema(new StringSchema())));
}

private OpenApiCustomiser getResponseMessages() {
    return openApi -> {
        openApi.getPaths().values().forEach(pathItem ->
            pathItem.readOperations().forEach(operation -> {
                ApiResponses apiResponses = operation.getResponses();
                apiResponses.addApiResponse(
                    String.valueOf(HttpStatus.BAD_REQUEST.value()),
                    new ApiResponse().description("Bad request"));
                apiResponses.addApiResponse(
                    String.valueOf(HttpStatus.UNAUTHORIZED.value()),
                    new ApiResponse().description("Not authorized"));
            }));
    };
}

Any ideas on what I'm missing? Thank you.

Here is a small spring boot application that demonstrates the issue: https://github.com/ens121/swaggertest

Ens
  • 308
  • 3
  • 15

1 Answers1

0

The global API definitions can be set with the @OpenAPIDefinition annotation :

@OpenAPIDefinition(info = @Info(title = "${openapi.info.title}", version = "${openapi.info.version}",
        description = "${openapi.info.description}", contact = @Contact(name = "${openapi.info.contact.name}", email = "${openapi.info.contact.email}"),
        license = @License(url = "${openapi.info.license.url}", name = "${openapi.info.license.name}")))
@SpringBootApplication

with the values provided in your application.properties :

# OPENAPI INFOS displayed with the @OpenAPIDefinition annotation
openapi.info.title=App Title
openapi.info.version=2.12.5
openapi.info.description=Application Description
openapi.info.contact.name=Principal Direction, Subdivision Name, Acronym
openapi.info.contact.email=info@mycorp.com
openapi.info.license.name=CorpName
openapi.info.license.url=http://www.mycorp.com

server.servlet.context-path=/context
server.port=27743

which yields :

swagger output

Pierre C
  • 2,920
  • 1
  • 35
  • 35