Skip to content

Commit

Permalink
Merge pull request #3543 from guohuang/compile_issue2
Browse files Browse the repository at this point in the history
[GO] Fixing compilation issue when pathParams  is not presented
  • Loading branch information
wing328 authored Aug 6, 2016
2 parents c220dbb + bef5c74 commit e8095c6
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,21 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
// if the return type is not primitive, import encoding/json
for (CodegenOperation operation : operations) {
if(operation.returnBaseType != null && needToImport(operation.returnBaseType)) {
Map<String, String> customImport = new HashMap<String, String>();
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<Map<String, String>> recursiveImports = (List<Map<String, String>>) objs.get("imports");
if (recursiveImports == null)
Expand All @@ -400,9 +408,7 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
// 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<String, String> newImportMap= new HashMap<String, String>();
newImportMap.put("import", importMapping.get(_import));
listIterator.add(newImportMap);
listIterator.add(createMapping("import", importMapping.get(_import)));
}
}

Expand Down Expand Up @@ -432,9 +438,7 @@ public Map<String, Object> postProcessModels(Map<String, Object> objs) {
// 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<String, String> newImportMap= new HashMap<String, String>();
newImportMap.put("import", importMapping.get(_import));
listIterator.add(newImportMap);
listIterator.add(createMapping("import", importMapping.get(_import)));
}
}

Expand Down Expand Up @@ -465,4 +469,11 @@ public String escapeQuotationMark(String input) {
public String escapeUnsafeCharacters(String input) {
return input.replace("*/", "*_/").replace("/*", "/_*");
}

public Map<String, String> createMapping(String key, String value){
Map<String, String> customImport = new HashMap<String, String>();
customImport.put(key, value);

return customImport;
}
}
4 changes: 1 addition & 3 deletions modules/swagger-codegen/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,8 @@ package {{packageName}}

{{#operations}}
import (
"strings"
"fmt"
"net/url"
{{#imports}}"{{import}}"
{{#imports}} "{{import}}"
{{/imports}}
)

Expand Down
8 changes: 4 additions & 4 deletions samples/client/petstore/go/go-petstore/pet_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/go/go-petstore/store_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
package petstore

import (
"strings"
"fmt"
"net/url"
"encoding/json"
"fmt"
"strings"
)

type StoreApi struct {
Expand Down
4 changes: 2 additions & 2 deletions samples/client/petstore/go/go-petstore/user_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@
package petstore

import (
"strings"
"fmt"
"net/url"
"encoding/json"
"fmt"
"strings"
)

type UserApi struct {
Expand Down

0 comments on commit e8095c6

Please sign in to comment.