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;
}
}