Remove invalid code from mustache for arrays. (#4266)

Rewrite ParameterToString to handle other slice types other than just string.
This commit is contained in:
Dan Wilson 2016-12-13 02:13:06 -06:00 committed by wing328
parent 5818f2c882
commit e55664cdc9
17 changed files with 102 additions and 61 deletions

View File

@ -83,13 +83,8 @@ func (a {{{classname}}}) {{{nickname}}}({{#allParams}}{{paramName}} {{{dataType}
{{#queryParams}}
{{#isListContainer}}
var collectionFormat = "{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}"
if collectionFormat == "multi" {
for _, value := range {{paramName}} {
localVarQueryParams.Add("{{baseName}}", value)
}
} else {
localVarQueryParams.Add("{{baseName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, collectionFormat))
}
{{/isListContainer}}
{{^isListContainer}}
localVarQueryParams.Add("{{baseName}}", a.Configuration.APIClient.ParameterToString({{paramName}}, ""))

View File

@ -80,17 +80,20 @@ func (c *APIClient) CallAPI(path string, method string,
}
func (c *APIClient) ParameterToString(obj interface{}, collectionFormat string) string {
if reflect.TypeOf(obj).String() == "[]string" {
delimiter := ""
switch collectionFormat {
case "pipes":
return strings.Join(obj.([]string), "|")
delimiter = "|"
case "ssv":
return strings.Join(obj.([]string), " ")
delimiter = " "
case "tsv":
return strings.Join(obj.([]string), "\t")
delimiter = "\t"
case "csv":
return strings.Join(obj.([]string), ",")
delimiter = ","
}
if reflect.TypeOf(obj).Kind() == reflect.Slice {
return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
}
return fmt.Sprintf("%v", obj)

View File

@ -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)
@ -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)

View File

@ -89,17 +89,20 @@ func (c *APIClient) CallAPI(path string, method string,
}
func (c *APIClient) ParameterToString(obj interface{}, collectionFormat string) string {
if reflect.TypeOf(obj).String() == "[]string" {
delimiter := ""
switch collectionFormat {
case "pipes":
return strings.Join(obj.([]string), "|")
delimiter = "|"
case "ssv":
return strings.Join(obj.([]string), " ")
delimiter = " "
case "tsv":
return strings.Join(obj.([]string), "\t")
delimiter = "\t"
case "csv":
return strings.Join(obj.([]string), ",")
delimiter = ","
}
if reflect.TypeOf(obj).Kind() == reflect.Slice {
return strings.Trim(strings.Replace(fmt.Sprint(obj), " ", delimiter, -1), "[]")
}
return fmt.Sprintf("%v", obj)

View File

@ -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"`
}

View File

@ -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)

View File

@ -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)

View File

@ -1,6 +1,6 @@
# \FakeApi
All URIs are relative to *http://petstore.swagger.io/v2*
All URIs are relative to **
Method | HTTP request | Description
------------- | ------------- | -------------

View File

@ -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)

View File

@ -1,6 +1,6 @@
# \PetApi
All URIs are relative to *http://petstore.swagger.io/v2*
All URIs are relative to **
Method | HTTP request | Description
------------- | ------------- | -------------

View File

@ -1,6 +1,6 @@
# \StoreApi
All URIs are relative to *http://petstore.swagger.io/v2*
All URIs are relative to **
Method | HTTP request | Description
------------- | ------------- | -------------

View File

@ -1,6 +1,6 @@
# \UserApi
All URIs are relative to *http://petstore.swagger.io/v2*
All URIs are relative to **
Method | HTTP request | Description
------------- | ------------- | -------------

View File

@ -17,4 +17,6 @@ type EnumTest struct {
EnumInteger int32 `json:"enum_integer,omitempty"`
EnumNumber float64 `json:"enum_number,omitempty"`
OuterEnum OuterEnum `json:"outerEnum,omitempty"`
}

View File

@ -219,13 +219,8 @@ func (a FakeApi) TestEnumParameters(enumFormStringArray []string, enumFormString
localVarHeaderParams[key] = a.Configuration.DefaultHeader[key]
}
var collectionFormat = "csv"
if collectionFormat == "multi" {
for _, value := range enumQueryStringArray {
localVarQueryParams.Add("enum_query_string_array", value)
}
} else {
localVarQueryParams.Add("enum_query_string_array", a.Configuration.APIClient.ParameterToString(enumQueryStringArray, collectionFormat))
}
localVarQueryParams.Add("enum_query_string", a.Configuration.APIClient.ParameterToString(enumQueryString, ""))
localVarQueryParams.Add("enum_query_integer", a.Configuration.APIClient.ParameterToString(enumQueryInteger, ""))

View File

@ -0,0 +1,14 @@
/*
* 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
type OuterEnum struct {
}

View File

@ -202,13 +202,8 @@ func (a PetApi) FindPetsByStatus(status []string) ([]Pet, *APIResponse, error) {
localVarHeaderParams[key] = a.Configuration.DefaultHeader[key]
}
var collectionFormat = "csv"
if collectionFormat == "multi" {
for _, value := range status {
localVarQueryParams.Add("status", value)
}
} else {
localVarQueryParams.Add("status", a.Configuration.APIClient.ParameterToString(status, collectionFormat))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }
@ -276,13 +271,8 @@ func (a PetApi) FindPetsByTags(tags []string) ([]Pet, *APIResponse, error) {
localVarHeaderParams[key] = a.Configuration.DefaultHeader[key]
}
var collectionFormat = "csv"
if collectionFormat == "multi" {
for _, value := range tags {
localVarQueryParams.Add("tags", value)
}
} else {
localVarQueryParams.Add("tags", a.Configuration.APIClient.ParameterToString(tags, collectionFormat))
}
// to determine the Content-Type header
localVarHttpContentTypes := []string{ }