Skip to content

Commit

Permalink
fix: respect configured generator URL in swagger config (OpenAPITools…
Browse files Browse the repository at this point in the history
…#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 and rk0n committed Apr 24, 2022
1 parent 8b0e3f0 commit 5adc7ee
Showing 1 changed file with 32 additions and 1 deletion.
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;
}

}

0 comments on commit 5adc7ee

Please sign in to comment.