Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix: respect configured generator URL in swagger config (OpenAPITools…
…#12064)

* fix: respect configured generator URL in swagger config

The generated OpenAPI spec does not reflect the GENERATOR_HOST which causes wrong generated code and non-functional snippets in the UI.

This PR improves that by adding the relevant parts to the spec.

* style: use `OpenAPI` instead of `Swagger`

* refactor: make Set creation Java 8 compatible

* fix: add missing import
  • Loading branch information
fgreinacher authored Apr 12, 2022
commit 79de04ed326fb8bd976d48148fd2ea6aa908836b
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,23 @@
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Properties;
import java.util.Set;
import java.util.HashSet;


@Configuration
@EnableSwagger2
public class OpenAPIDocumentationConfig {
private final Logger LOGGER = LoggerFactory.getLogger(OpenAPIDocumentationConfig.class);

ApiInfo apiInfo() {
final Properties properties = new Properties();
Expand All @@ -63,7 +71,7 @@ ApiInfo apiInfo() {

@Bean
public Docket customImplementation(){
return new Docket(DocumentationType.SWAGGER_2)
Docket docket = new Docket(DocumentationType.SWAGGER_2)
.select()
.apis(RequestHandlerSelectors.basePackage("org.openapitools.codegen.online.api"))
.build()
Expand All @@ -74,6 +82,29 @@ public Docket customImplementation(){
.ignoredParameterTypes(Resource.class)
.ignoredParameterTypes(InputStream.class)
.apiInfo(apiInfo());

String hostString = System.getenv("GENERATOR_HOST");
if (!StringUtils.isBlank(hostString)) {
try {
URI hostURI = new URI(hostString);
String scheme = hostURI.getScheme();
if (scheme != null) {
Set<String> protocols = new HashSet<String>();
protocols.add(scheme);
docket.protocols(protocols);
}
String authority = hostURI.getAuthority();
if (authority != null) {
// In OpenAPI `host` refers to host _and_ port, a.k.a. the URI authority
docket.host(authority);
}
docket.pathMapping(hostURI.getPath());
} catch(URISyntaxException e) {
LOGGER.warn("Could not parse configured GENERATOR_HOST '" + hostString + "': " + e.getMessage());
}
}

return docket;
}

}