Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -415,13 +415,12 @@ public Map<String, Object> postProcessOperations(Map<String, Object> objs) {

// this will only import "fmt" if there are items in pathParams
for (CodegenOperation operation : operations) {
if(operation.pathParams != null && operation.pathParams.size() > 0) {
if(operation.allParams != null && operation.allParams.size() > 0) {
imports.add(createMapping("import", "fmt"));
break; //just need to import once
}
}


// recursively add import for mapping one type to multiple imports
List<Map<String, String>> recursiveImports = (List<Map<String, String>>) objs.get("imports");
if (recursiveImports == null)
Expand Down
71 changes: 56 additions & 15 deletions modules/swagger-codegen/src/main/resources/go/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -34,15 +34,48 @@ func New{{classname}}WithBasePath(basePath string) *{{classname}} {
* {{summary}}{{#notes}}
* {{notes}}{{/notes}}
*
{{#allParams}} * @param {{paramName}} {{description}}
{{#allParams}} * @param {{baseName}} {{^isPrimitiveType}}{{^isContainer}}{{^isDate}}{{^isDateTime}}*{{/isDateTime}}{{/isDate}}{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}{{#required}} (required){{/required}} {{description}}
{{/allParams}} * @return {{#returnType}}{{^isListContainer}}*{{/isListContainer}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
*/
func (a {{{classname}}}) {{{nickname}}}({{#allParams}}{{paramName}} {{{dataType}}}{{#hasMore}}, {{/hasMore}}{{/allParams}}) ({{#returnType}}{{^isListContainer}}*{{/isListContainer}}{{{returnType}}}, {{/returnType}}*APIResponse, error) {
func (a {{{classname}}}) {{{nickname}}}(params map[string]interface{}) ({{#returnType}}successPayload {{^isListContainer}}*{{/isListContainer}}{{{returnType}}}, {{/returnType}}localVarAPIResponse *APIResponse, err error) {

{{#allParams}}
{{#required}}
var {{paramName}} {{^isPrimitiveType}}{{^isContainer}}{{^isDate}}{{^isDateTime}}*{{/isDateTime}}{{/isDate}}{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}
if params["{{baseName}}"] == nil {
err = fmt.Errorf("Param '{{baseName}}' is required")
return
} else {
var ok bool
{{paramName}}, ok = params["{{baseName}}"].({{^isPrimitiveType}}{{^isContainer}}{{^isDate}}{{^isDateTime}}*{{/isDateTime}}{{/isDate}}{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}})
if !ok {
err = fmt.Errorf("Invalid type for param '{{baseName}}', must be '{{^isPrimitiveType}}{{^isContainer}}{{^isDate}}{{^isDateTime}}*{{/isDateTime}}{{/isDate}}{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}'")
return
}
}
{{/required}}
{{^required}}
var {{paramName}} {{^isPrimitiveType}}{{^isContainer}}{{^isDate}}{{^isDateTime}}*{{/isDateTime}}{{/isDate}}{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}
if params["{{baseName}}"] != nil {
var ok bool
{{paramName}}, ok = params["{{baseName}}"].({{^isPrimitiveType}}{{^isContainer}}{{^isDate}}{{^isDateTime}}*{{/isDateTime}}{{/isDate}}{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}})
if !ok {
err = fmt.Errorf("Invalid type for param '{{baseName}}', must be '{{^isPrimitiveType}}{{^isContainer}}{{^isDate}}{{^isDateTime}}*{{/isDateTime}}{{/isDate}}{{/isContainer}}{{/isPrimitiveType}}{{{dataType}}}'")
return
}
}
{{/required}}
{{/allParams}}

var localVarHttpMethod = strings.ToUpper("{{httpMethod}}")
// create path and map variables
localVarPath := a.Configuration.BasePath + "{{path}}"{{#pathParams}}
localVarPath = strings.Replace(localVarPath, "{"+"{{baseName}}"+"}", fmt.Sprintf("%v", {{paramName}}), -1){{/pathParams}}
localVarPath := a.Configuration.BasePath + "{{path}}"
{{#pathParams}}
if params["{{baseName}}"] != nil {
localVarPath = strings.Replace(localVarPath, "{"+"{{baseName}}"+"}", a.Configuration.APIClient.ParameterToString(
{{paramName}}, ""), -1)
}
{{/pathParams}}

localVarHeaderParams := make(map[string]string)
localVarQueryParams := url.Values{}
Expand Down Expand Up @@ -83,16 +116,18 @@ func (a {{{classname}}}) {{{nickname}}}({{#allParams}}{{paramName}} {{{dataType}
{{#queryParams}}
{{#isListContainer}}
var collectionFormat = "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"
if collectionFormat == "multi" {
if collectionFormat == "multi" && params["{{baseName}}"] != nil {
for _, value := range {{paramName}} {
localVarQueryParams.Add("{{baseName}}", value)
localVarQueryParams.Add("{{baseName}}", fmt.Sprintf("%v", value))
}
} else {
} else if params["{{baseName}}"] != nil {
localVarQueryParams.Add("{{baseName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, collectionFormat))
}
{{/isListContainer}}
{{^isListContainer}}
if params["{{baseName}}"] != nil {
localVarQueryParams.Add("{{baseName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, ""))
}
{{/isListContainer}}
{{/queryParams}}
{{/hasQueryParams}}
Expand Down Expand Up @@ -120,7 +155,9 @@ func (a {{{classname}}}) {{{nickname}}}({{#allParams}}{{paramName}} {{{dataType}
{{#hasHeaderParams}}
{{#headerParams}}
// header params "{{baseName}}"
localVarHeaderParams["{{baseName}}"] = a.Configuration.APIClient.ParameterToString({{paramName}}, "")
if params["{{baseName}}"] != nil {
localVarHeaderParams["{{baseName}}"] = a.Configuration.APIClient.ParameterToString({{paramName}}, "")
}
{{/headerParams}}
{{/hasHeaderParams}}
{{#hasFormParams}}
Expand All @@ -131,31 +168,35 @@ func (a {{{classname}}}) {{{nickname}}}({{#allParams}}{{paramName}} {{{dataType}
localVarFileName = file.Name()
{{/isFile}}
{{^isFile}}
localVarFormParams["{{paramName}}"] = a.Configuration.APIClient.ParameterToString({{paramName}}, "")
if params["{{baseName}}"] != nil {
localVarFormParams["{{paramName}}"] = a.Configuration.APIClient.ParameterToString({{paramName}}, "")
}
{{/isFile}}
{{/formParams}}
{{/hasFormParams}}
{{#hasBodyParam}}
{{#bodyParams}} // body params
localVarPostBody = &{{paramName}}
if params["{{baseName}}"] != nil {
localVarPostBody = {{paramName}}
}
{{/bodyParams}}
{{/hasBodyParam}}
{{#returnType}}
var successPayload = new({{returnType}})
successPayload = new({{returnType}})
{{/returnType}}
localVarHttpResponse, err := a.Configuration.APIClient.CallAPI(localVarPath, localVarHttpMethod, localVarPostBody, localVarHeaderParams, localVarQueryParams, localVarFormParams, localVarFileName, localVarFileBytes)
if err != nil {
return
}

var localVarURL, _ = url.Parse(localVarPath)
localVarURL.RawQuery = localVarQueryParams.Encode()
var localVarAPIResponse = &APIResponse{Operation: "{{operationId}}", Method: localVarHttpMethod, RequestURL: localVarURL.String()}
localVarAPIResponse = &APIResponse{Operation: "{{operationId}}", Method: localVarHttpMethod, RequestURL: localVarURL.String()}
if localVarHttpResponse != nil {
localVarAPIResponse.Response = localVarHttpResponse.RawResponse
localVarAPIResponse.Payload = localVarHttpResponse.Body()
}

if err != nil {
return {{#returnType}}{{#isListContainer}}*{{/isListContainer}}successPayload, {{/returnType}}localVarAPIResponse, err
}
{{#returnType}}
err = json.Unmarshal(localVarHttpResponse.Body(), &successPayload)
{{/returnType}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import ({{/imports}}{{#imports}}
type {{classname}} struct {
{{#vars}}{{#description}}
// {{{description}}}{{/description}}
{{name}} {{{datatype}}} `json:"{{baseName}},omitempty"`
{{name}} {{^isPrimitiveType}}{{^isContainer}}{{^isDate}}{{^isDateTime}}*{{/isDateTime}}{{/isDate}}{{/isContainer}}{{/isPrimitiveType}}{{{datatype}}} `json:"{{baseName}}{{^required}},omitempty{{/required}}"`
{{/vars}}
}
{{/model}}{{/models}}
2 changes: 2 additions & 0 deletions samples/client/petstore/go/go-petstore/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ Class | Method | HTTP request | Description
- [ArrayTest](docs/ArrayTest.md)
- [Cat](docs/Cat.md)
- [Category](docs/Category.md)
- [ClassModel](docs/ClassModel.md)
- [Client](docs/Client.md)
- [Dog](docs/Dog.md)
- [EnumArrays](docs/EnumArrays.md)
Expand All @@ -72,6 +73,7 @@ Class | Method | HTTP request | Description
- [Name](docs/Name.md)
- [NumberOnly](docs/NumberOnly.md)
- [Order](docs/Order.md)
- [OuterEnum](docs/OuterEnum.md)
- [Pet](docs/Pet.md)
- [ReadOnlyFirst](docs/ReadOnlyFirst.md)
- [SpecialModelName](docs/SpecialModelName.md)
Expand Down
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/animal.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package petstore

type Animal struct {

ClassName string `json:"className,omitempty"`
ClassName string `json:"className"`

Color string `json:"color,omitempty"`
}
2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package petstore

type Cat struct {

ClassName string `json:"className,omitempty"`
ClassName string `json:"className"`

Color string `json:"color,omitempty"`

Expand Down
17 changes: 17 additions & 0 deletions samples/client/petstore/go/go-petstore/class_model.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/*
* Swagger Petstore
*
* This spec is mainly for testing Petstore server and contains fake endpoints, models. Please do not use this for any other purpose. Special characters: \" \\
*
* OpenAPI spec version: 1.0.0
* Contact: apiteam@swagger.io
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*/

package petstore

// Model for testing model with \"_class\" property
type ClassModel struct {

Class string `json:"_class,omitempty"`
}
10 changes: 10 additions & 0 deletions samples/client/petstore/go/go-petstore/docs/ClassModel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# ClassModel

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**Class** | **string** | | [optional] [default to null]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


1 change: 1 addition & 0 deletions samples/client/petstore/go/go-petstore/docs/EnumTest.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ Name | Type | Description | Notes
**EnumString** | **string** | | [optional] [default to null]
**EnumInteger** | **int32** | | [optional] [default to null]
**EnumNumber** | **float64** | | [optional] [default to null]
**OuterEnum** | [**OuterEnum**](OuterEnum.md) | | [optional] [default to null]

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

Expand Down
6 changes: 5 additions & 1 deletion samples/client/petstore/go/go-petstore/docs/FakeApi.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ Method | HTTP request | Description

To test \"client\" model

To test \"client\" model


### Parameters

Expand Down Expand Up @@ -83,6 +85,8 @@ void (empty response body)

To test enum parameters

To test enum parameters


### Parameters

Expand All @@ -94,7 +98,7 @@ Name | Type | Description | Notes
**enumHeaderString** | **string**| Header parameter enum test (string) | [optional] [default to -efg]
**enumQueryStringArray** | [**[]string**](string.md)| Query parameter enum test (string array) | [optional]
**enumQueryString** | **string**| Query parameter enum test (string) | [optional] [default to -efg]
**enumQueryInteger** | **float32**| Query parameter enum test (double) | [optional]
**enumQueryInteger** | **int32**| Query parameter enum test (double) | [optional]
**enumQueryDouble** | **float64**| Query parameter enum test (double) | [optional]

### Return type
Expand Down
9 changes: 9 additions & 0 deletions samples/client/petstore/go/go-petstore/docs/OuterEnum.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# OuterEnum

## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------

[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)


2 changes: 1 addition & 1 deletion samples/client/petstore/go/go-petstore/dog.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ package petstore

type Dog struct {

ClassName string `json:"className,omitempty"`
ClassName string `json:"className"`

Color string `json:"color,omitempty"`

Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/go/go-petstore/enum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ type EnumTest struct {
EnumInteger int32 `json:"enum_integer,omitempty"`

EnumNumber float64 `json:"enum_number,omitempty"`

OuterEnum *OuterEnum `json:"outerEnum,omitempty"`
}
Loading