vinit saha
vinit saha

Reputation: 57

How to exclude the "servers" section from the generated OpenAPI doc in Spring?

OpenAPI 3.0 Spec has a set of Fixed fields mentioned as OpenAPI Object https://swagger.io/specification/

If I enable OpenAPI spec by default it will have a servers section (https://swagger.io/specification/#server-object) with details like url and description. I would not want to have the servers section in my api-docs file. Is there a way to disable the servers section?

Upvotes: 1

Views: 1258

Answers (1)

Nico Van Belle
Nico Van Belle

Reputation: 5166

I have no experience with Springfox OpenAPI but I did notice looking at the source code that spring-boot autoconfiguration registers a bean that dynamically adds the servers section to the specification upon a request.

Autoconfiguration which registers the specification transformer:

@Configuration
@ConditionalOnWebApplication(type = ConditionalOnWebApplication.Type.SERVLET)
@Conditional(OnServletBasedWebApplication.class)
@EnablePluginRegistries(WebMvcOpenApiTransformationFilter.class)
public class OpenApiWebMvcConfiguration {
  @Bean
  public WebMvcOpenApiTransformationFilter webMvcOpenApiTransformer(
      @Value(OPEN_API_SPECIFICATION_PATH) String oasPath) {
    return new WebMvcBasePathAndHostnameTransformationFilter(oasPath);
  }
}

Implementation of the default transformer which adds to the server section:

@Order(Ordered.HIGHEST_PRECEDENCE)
public class WebMvcBasePathAndHostnameTransformationFilter implements WebMvcOpenApiTransformationFilter {
  private static final Logger LOGGER = getLogger(WebMvcBasePathAndHostnameTransformationFilter.class);
  private final String requestPrefix;

  public WebMvcBasePathAndHostnameTransformationFilter(@Value(OPEN_API_SPECIFICATION_PATH) String oasPath) {
    this.requestPrefix = StringUtils.trimTrailingCharacter(oasPath, '/');
  }

  @Override
  public OpenAPI transform(OpenApiTransformationContext<HttpServletRequest> context) {
    OpenAPI openApi = context.getSpecification();
    context.request().ifPresent(servletRequest -> {
      ForwardedHeaderExtractingRequest filter
          = new ForwardedHeaderExtractingRequest(servletRequest, new UrlPathHelper());
      openApi.servers(Collections.singletonList(inferredServer(requestPrefix, filter.adjustedRequestURL())));
    });
    return openApi;
  }

  @Override
  public boolean supports(DocumentationType delimiter) {
    return delimiter == DocumentationType.OAS_30;
  }
}

If no suitable workaround can be found, I'd try to register a new bean of type WebMvcOpenApiTransformationFilter that overrides the current one.

Example:

@Configuration
public class AppConfig {

  @Primary
  @Bean
  public WebMvcOpenApiTransformationFilter customMvcOpenApiTransformer() {
    return new MyCustomOpenApiTransformationFilter();
  }
}



@Order(Ordered.HIGHEST_PRECEDENCE)
public class MyCustomOpenApiTransformationFilter implements WebMvcOpenApiTransformationFilter {
  @Override
  public OpenAPI transform(OpenApiTransformationContext<HttpServletRequest> context) {
    return context.getSpecification();
  }

  @Override
  public boolean supports(DocumentationType delimiter) {
    return delimiter == DocumentationType.OAS_30;
  }
}

Upvotes: 1

Related Questions