diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java index 06c4f0ddfae..93eecd03251 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/GoClientCodegen.java @@ -382,13 +382,21 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { // if the return type is not primitive, import encoding/json for (CodegenOperation operation : operations) { if(operation.returnBaseType != null && needToImport(operation.returnBaseType)) { - Map customImport = new HashMap(); - customImport.put("import", "encoding/json"); - imports.add(customImport); + imports.add(createMapping("import", "encoding/json")); break; //just need to import once } } + // this will only import "strings" "fmt" if there are items in pathParams + for (CodegenOperation operation : operations) { + if(operation.pathParams != null && operation.pathParams.size() > 0) { + imports.add(createMapping("import", "fmt")); + imports.add(createMapping("import", "strings")); + break; //just need to import once + } + } + + // recursivly add import for mapping one type to multipe imports List> recursiveImports = (List>) objs.get("imports"); if (recursiveImports == null) @@ -400,9 +408,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { // if the import package happens to be found in the importMapping (key) // add the corresponding import package to the list if (importMapping.containsKey(_import)) { - Map newImportMap= new HashMap(); - newImportMap.put("import", importMapping.get(_import)); - listIterator.add(newImportMap); + listIterator.add(createMapping("import", importMapping.get(_import))); } } @@ -432,9 +438,7 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { // if the import package happens to be found in the importMapping (key) // add the corresponding import package to the list if (importMapping.containsKey(_import)) { - Map newImportMap= new HashMap(); - newImportMap.put("import", importMapping.get(_import)); - listIterator.add(newImportMap); + listIterator.add(createMapping("import", importMapping.get(_import))); } } @@ -465,4 +469,11 @@ public class GoClientCodegen extends DefaultCodegen implements CodegenConfig { public String escapeUnsafeCharacters(String input) { return input.replace("*/", "*_/").replace("/*", "/_*"); } + + public Map createMapping(String key, String value){ + Map customImport = new HashMap(); + customImport.put(key, value); + + return customImport; + } } diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index db14fa0f157..ed0f8875541 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -3,10 +3,8 @@ package {{packageName}} {{#operations}} import ( - "strings" - "fmt" "net/url" - {{#imports}}"{{import}}" +{{#imports}} "{{import}}" {{/imports}} ) diff --git a/samples/client/petstore/go/go-petstore/pet_api.go b/samples/client/petstore/go/go-petstore/pet_api.go index 61819f1be78..d294e6c4a7e 100644 --- a/samples/client/petstore/go/go-petstore/pet_api.go +++ b/samples/client/petstore/go/go-petstore/pet_api.go @@ -23,12 +23,12 @@ package petstore import ( - "strings" - "fmt" "net/url" "os" -"io/ioutil" -"encoding/json" + "io/ioutil" + "encoding/json" + "fmt" + "strings" ) type PetApi struct { diff --git a/samples/client/petstore/go/go-petstore/store_api.go b/samples/client/petstore/go/go-petstore/store_api.go index 09d3a179b0b..5def446a211 100644 --- a/samples/client/petstore/go/go-petstore/store_api.go +++ b/samples/client/petstore/go/go-petstore/store_api.go @@ -23,10 +23,10 @@ package petstore import ( - "strings" - "fmt" "net/url" "encoding/json" + "fmt" + "strings" ) type StoreApi struct { diff --git a/samples/client/petstore/go/go-petstore/user_api.go b/samples/client/petstore/go/go-petstore/user_api.go index e591fd01ef9..4f2e364d0f8 100644 --- a/samples/client/petstore/go/go-petstore/user_api.go +++ b/samples/client/petstore/go/go-petstore/user_api.go @@ -23,10 +23,10 @@ package petstore import ( - "strings" - "fmt" "net/url" "encoding/json" + "fmt" + "strings" ) type UserApi struct {