swagger-api / swagger-codegen

swagger-codegen contains a template-driven engine to generate documentation, API clients and server stubs in different languages by parsing your OpenAPI / Swagger definition.
http://swagger.io
Apache License 2.0
17.01k stars 6.03k forks source link

Inconsistent use of GENERATOR_HOST value #10243

Open rmohta opened 4 years ago

rmohta commented 4 years ago
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 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.

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;
+    }
 }
rmohta commented 4 years ago

Image rmohta/swagger-generator:2.4.13-issue-10243 has the above patched code, and it's working as expected.

Lausselloic commented 3 years ago

Hello,

This issue still present on 2.4.21.

I'm facing the same issue as @rmohta , is there any chance to have a fix included in 2.4.22?

If you want to ensure that there's no breaking change, it's also possible to add a new variable dedicated to client generator.