1

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?

Helen
  • 87,344
  • 17
  • 243
  • 314
vinit saha
  • 47
  • 3

1 Answers1

1

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;
  }
}
Nico Van Belle
  • 4,911
  • 4
  • 32
  • 49
  • Thanks for sharing this. Are you using Spring doc Open API library ? I could not find WebMvcOpenApiTransformationFilter available in that library. – vinit saha Nov 17 '21 at 07:15
  • @vinitsaha It should be present when you use the starter dependency from Spring namely `io.springfox:springfox-boot-starter:3.0.0` – Nico Van Belle Nov 17 '21 at 07:40
  • I want to use Open API spec v3.0. Hence the dependency I have is org.springdoc:springdoc-openapi-ui:1.5.11. If you can help with that then it would be great :) – vinit saha Nov 17 '21 at 08:44