Skip to content

Commit 79de04e

Browse files
authored
fix: respect configured generator URL in swagger config (#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
1 parent e988dc8 commit 79de04e

File tree

1 file changed

+32
-1
lines changed

1 file changed

+32
-1
lines changed

modules/openapi-generator-online/src/main/java/org/openapitools/codegen/online/configuration/OpenAPIDocumentationConfig.java

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,23 @@
2828
import springfox.documentation.spi.DocumentationType;
2929
import springfox.documentation.spring.web.plugins.Docket;
3030
import springfox.documentation.swagger2.annotations.EnableSwagger2;
31+
import org.apache.commons.lang3.StringUtils;
32+
import org.slf4j.Logger;
33+
import org.slf4j.LoggerFactory;
3134

3235
import java.io.IOException;
3336
import java.io.InputStream;
37+
import java.net.URI;
38+
import java.net.URISyntaxException;
3439
import java.util.Properties;
40+
import java.util.Set;
41+
import java.util.HashSet;
3542

3643

3744
@Configuration
3845
@EnableSwagger2
3946
public class OpenAPIDocumentationConfig {
47+
private final Logger LOGGER = LoggerFactory.getLogger(OpenAPIDocumentationConfig.class);
4048

4149
ApiInfo apiInfo() {
4250
final Properties properties = new Properties();
@@ -63,7 +71,7 @@ ApiInfo apiInfo() {
6371

6472
@Bean
6573
public Docket customImplementation(){
66-
return new Docket(DocumentationType.SWAGGER_2)
74+
Docket docket = new Docket(DocumentationType.SWAGGER_2)
6775
.select()
6876
.apis(RequestHandlerSelectors.basePackage("org.openapitools.codegen.online.api"))
6977
.build()
@@ -74,6 +82,29 @@ public Docket customImplementation(){
7482
.ignoredParameterTypes(Resource.class)
7583
.ignoredParameterTypes(InputStream.class)
7684
.apiInfo(apiInfo());
85+
86+
String hostString = System.getenv("GENERATOR_HOST");
87+
if (!StringUtils.isBlank(hostString)) {
88+
try {
89+
URI hostURI = new URI(hostString);
90+
String scheme = hostURI.getScheme();
91+
if (scheme != null) {
92+
Set<String> protocols = new HashSet<String>();
93+
protocols.add(scheme);
94+
docket.protocols(protocols);
95+
}
96+
String authority = hostURI.getAuthority();
97+
if (authority != null) {
98+
// In OpenAPI `host` refers to host _and_ port, a.k.a. the URI authority
99+
docket.host(authority);
100+
}
101+
docket.pathMapping(hostURI.getPath());
102+
} catch(URISyntaxException e) {
103+
LOGGER.warn("Could not parse configured GENERATOR_HOST '" + hostString + "': " + e.getMessage());
104+
}
105+
}
106+
107+
return docket;
77108
}
78109

79110
}

0 commit comments

Comments
 (0)