From ab96b318e2d604d405c318010e17760e985990ec Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Fri, 5 Aug 2016 14:53:05 -0700 Subject: [PATCH 1/2] [GO] Fixing compilation issue when pathParams is not presented --- .../codegen/languages/GoClientCodegen.java | 29 +++++++++++++------ .../src/main/resources/go/api.mustache | 5 ++-- .../client/petstore/go/go-petstore/pet_api.go | 9 +++--- .../petstore/go/go-petstore/store_api.go | 5 ++-- .../petstore/go/go-petstore/user_api.go | 5 ++-- 5 files changed, 33 insertions(+), 20 deletions(-) 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..2f1898f1903 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -3,10 +3,9 @@ package {{packageName}} {{#operations}} import ( - "strings" - "fmt" + "errors" "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..ba239630c92 100644 --- a/samples/client/petstore/go/go-petstore/pet_api.go +++ b/samples/client/petstore/go/go-petstore/pet_api.go @@ -23,12 +23,13 @@ package petstore import ( - "strings" - "fmt" + "errors" "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..c0f772cf758 100644 --- a/samples/client/petstore/go/go-petstore/store_api.go +++ b/samples/client/petstore/go/go-petstore/store_api.go @@ -23,10 +23,11 @@ package petstore import ( - "strings" - "fmt" + "errors" "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..1c403e43464 100644 --- a/samples/client/petstore/go/go-petstore/user_api.go +++ b/samples/client/petstore/go/go-petstore/user_api.go @@ -23,10 +23,11 @@ package petstore import ( - "strings" - "fmt" + "errors" "net/url" "encoding/json" + "fmt" + "strings" ) type UserApi struct { From bef5c74da7771659d8e642dc74ca7b74bf9e6844 Mon Sep 17 00:00:00 2001 From: Guo Huang Date: Fri, 5 Aug 2016 21:34:04 -0700 Subject: [PATCH 2/2] removed unneeded "errors" import --- modules/swagger-codegen/src/main/resources/go/api.mustache | 1 - samples/client/petstore/go/go-petstore/pet_api.go | 1 - samples/client/petstore/go/go-petstore/store_api.go | 1 - samples/client/petstore/go/go-petstore/user_api.go | 1 - 4 files changed, 4 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/go/api.mustache b/modules/swagger-codegen/src/main/resources/go/api.mustache index 2f1898f1903..ed0f8875541 100644 --- a/modules/swagger-codegen/src/main/resources/go/api.mustache +++ b/modules/swagger-codegen/src/main/resources/go/api.mustache @@ -3,7 +3,6 @@ package {{packageName}} {{#operations}} import ( - "errors" "net/url" {{#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 ba239630c92..d294e6c4a7e 100644 --- a/samples/client/petstore/go/go-petstore/pet_api.go +++ b/samples/client/petstore/go/go-petstore/pet_api.go @@ -23,7 +23,6 @@ package petstore import ( - "errors" "net/url" "os" "io/ioutil" diff --git a/samples/client/petstore/go/go-petstore/store_api.go b/samples/client/petstore/go/go-petstore/store_api.go index c0f772cf758..5def446a211 100644 --- a/samples/client/petstore/go/go-petstore/store_api.go +++ b/samples/client/petstore/go/go-petstore/store_api.go @@ -23,7 +23,6 @@ package petstore import ( - "errors" "net/url" "encoding/json" "fmt" diff --git a/samples/client/petstore/go/go-petstore/user_api.go b/samples/client/petstore/go/go-petstore/user_api.go index 1c403e43464..4f2e364d0f8 100644 --- a/samples/client/petstore/go/go-petstore/user_api.go +++ b/samples/client/petstore/go/go-petstore/user_api.go @@ -23,7 +23,6 @@ package petstore import ( - "errors" "net/url" "encoding/json" "fmt"