swagger-api / swagger-api/swagger-codegen
Inconsistent use of GENERATOR_HOST value
Nobody has claimed this yet.
- Dominant language
- Mustache
- Stars
- 17.8k
- Forks
- 6k
- PR merge metrics
- No merged PRs in 30d
Description
Description
While using swaggerapi/swagger-generator:2.4.13 docker image in a multi-container pod along with swagger-editor, noticed server code generation response does not use the value of GENERATOR_HOST env variable. Whereas client code generation honors the value of GENERATOR_HOST
Swagger-codegen version
- swagger-generator:2.4.13
- I think it might be the case for swaggerapi/swagger-generator-v3:3.0.19 too
Swagger declaration file content or url
Use petstore swagger file, which is embedded in the the editor.
Command line used for generation
Not Applicable
Steps to reproduce
Use Developer Tools in Chrome to track Network requests, and preview response.
- Create docker container
docker run -d -e GENERATOR_HOST=http://locahost:81 -p 80:8080 swaggerapi/swagger-generator:2.4.13 - Open browser and point to http://localhost/. Swagger Editor UI with Pet Store
- Open Developer tools and click on Network tab
- Generate Client code for any language.
- Notice download of Client zip file fails, as the editor is trying to download from http://localhost:81 (EXPECTED. It honors GENERATOR_HOST env variable)
- Generate Server code for any language.
- Server code downloads.
(UNEXPECTED. It does not honor GENERATOR_HOST env variable)
Related issues/PRs
Unable to find any related
Suggest a fix/enhancement
Able to fix using the below patch
diff --git modules/swagger-generator/src/main/java/io/swagger/generator/resource/SwaggerResource.java modules/swagger-generator/src/main/java/io/swagger/generator/resource/SwaggerResource.java
index 4220e1034..207394f73 100644
--- modules/swagger-generator/src/main/java/io/swagger/generator/resource/SwaggerResource.java
+++ modules/swagger-generator/src/main/java/io/swagger/generator/resource/SwaggerResource.java
@@ -91,19 +91,7 @@ public class SwaggerResource {
throws Exception {
String filename = Generator.generateClient(language, opts);
- String host = System.getenv("GENERATOR_HOST");
-
- if (StringUtils.isBlank(host)) {
- String scheme = request.getHeader("X-SSL");
- String port = "";
- if ("1".equals(scheme)) {
- scheme = "https";
- } else {
- scheme = request.getScheme();
- port = ":" + request.getServerPort();
- }
- host = scheme + "://" + request.getServerName() + port;
- }
+ String host = getHost(request);
if (filename != null) {
String code = String.valueOf(UUID.randomUUID().toString());
@@ -192,9 +180,7 @@ public class SwaggerResource {
String filename = Generator.generateServer(framework, opts);
System.out.println("generated name: " + filename);
- String host =
- request.getScheme() + "://" + request.getServerName() + ":"
- + request.getServerPort();
+ String host = getHost(request);
if (filename != null) {
String code = String.valueOf(UUID.randomUUID().toString());
@@ -209,4 +195,11 @@ public class SwaggerResource {
return Response.status(500).build();
}
}
+
+ private String getHost( HttpServletRequest request){
+ String host = System.getenv("GENERATOR_HOST");
+ return StringUtils.isBlank(host)
+ ? (request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort())
+ : host;
+ }
}
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Inspect modules/swagger-generator/src/main/java/io/swagger/generator/resource/SwaggerResource.java, focusing on how the client and server generation responses construct their download hosts. Reproduce the behavior with the swagger-generator:2.4.13 Docker image, GENERATOR_HOST, and the embedded Petstore definition; done means server downloads honor the configured host as client downloads do.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- docker, java
- Domain
- api, backend
- Issue type
- Bug
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Activity status
- Stale
- Clarity
- Clearly specified
- Newbie friendliness
- 30/100