From fcaa8a317457f339ddd34e833df2cd0e4b1d0c7d Mon Sep 17 00:00:00 2001 From: Emond Papegaaij Date: Thu, 23 Nov 2023 17:45:10 +0100 Subject: [PATCH 1/2] Normalize slices of all types to []any --- request_information.go | 25 ++++++++++++++++++------- request_information_test.go | 12 ++++++++++++ 2 files changed, 30 insertions(+), 7 deletions(-) diff --git a/request_information.go b/request_information.go index 621192e..78de020 100644 --- a/request_information.go +++ b/request_information.go @@ -109,7 +109,7 @@ func (request *RequestInformation) GetUri() (*u.URL, error) { substitutions[key] = value } for key, value := range request.PathParametersAny { - substitutions[key] = request.normalizeEnumParameters(reflect.ValueOf(value), value, false) + substitutions[key] = request.normalizeParameters(reflect.ValueOf(value), value, false) } for key, value := range request.QueryParameters { substitutions[key] = value @@ -521,29 +521,40 @@ func (request *RequestInformation) AddQueryParameters(source any) { if arr, ok := value.([]any); ok && len(arr) > 0 { request.QueryParametersAny[fieldName] = arr } - normalizedEnumValue := request.normalizeEnumParameters(valueOfValue, value, true) - if normalizedEnumValue != nil { - request.QueryParametersAny[fieldName] = normalizedEnumValue + normalizedValue := request.normalizeParameters(valueOfValue, value, true) + if normalizedValue != nil { + request.QueryParametersAny[fieldName] = normalizedValue } } } -func (request *RequestInformation) normalizeEnumParameters(valueOfValue reflect.Value, value any, returnNilIfNotEnum bool) any { + +// Normalize different types to values that can be rendered in an URL: +// enum -> string (name) +// []enum -> []string (containing names) +// []non_interface -> []any (like []int64 -> []any) +func (request *RequestInformation) normalizeParameters(valueOfValue reflect.Value, value any, returnNilIfNotNormalizable bool) any { if valueOfValue.Kind() == reflect.Slice && valueOfValue.Len() > 0 { //type assertions to "enums" don't work if you don't know the enum type in advance, we need to use reflection enumArr := valueOfValue.Slice(0, valueOfValue.Len()) - strRepresentations := make([]string, valueOfValue.Len()) if _, ok := enumArr.Index(0).Interface().(kiotaEnum); ok { // testing the first value is an enum to avoid iterating over the whole array if it's not + strRepresentations := make([]string, valueOfValue.Len()) for i := range strRepresentations { strRepresentations[i] = enumArr.Index(i).Interface().(kiotaEnum).String() } return strRepresentations + } else { + anySlice := make([]any, valueOfValue.Len()) + for i := range anySlice { + anySlice[i] = enumArr.Index(i).Interface() + } + return anySlice } } else if enum, ok := value.(kiotaEnum); ok { return enum.String() } - if returnNilIfNotEnum { + if returnNilIfNotNormalizable { return nil } else { return value diff --git a/request_information_test.go b/request_information_test.go index 29122f7..39a7f2e 100644 --- a/request_information_test.go +++ b/request_information_test.go @@ -103,6 +103,18 @@ type getQueryParameters struct { Filter *string `uriparametername:"%24filter"` Orderby []string `uriparametername:"%24orderby"` Search *string `uriparametername:"%24search"` + Number []int64 `uriparametername:"%24number"` +} + +func TestItSetsNumberArrayQueryParameters(t *testing.T) { + requestInformation := NewRequestInformation() + requestInformation.UrlTemplate = "http://localhost/me{?%24number}" + requestInformation.AddQueryParameters(getQueryParameters{ + Number: []int64{1, 2, 4}, + }) + resultUri, err := requestInformation.GetUri() + assert.Nil(t, err) + assert.Equal(t, "http://localhost/me?%24number=1,2,4", resultUri.String()) } func TestItSetsSelectAndCountQueryParameters(t *testing.T) { From de1d6c994c2f28c81a82da8155a907e00da17f05 Mon Sep 17 00:00:00 2001 From: Emond Papegaaij Date: Fri, 24 Nov 2023 08:14:54 +0100 Subject: [PATCH 2/2] Update changelog --- CHANGELOG.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 33b8bc9..1069415 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed +## [1.5.3] - 2023-11-24 + +### Added + +- Added support for multi valued query and path parameters of type other than string. [#124](https://github.com/microsoft/kiota-abstractions-go/pull/124) + ## [1.5.2] - 2023-11-22 ### Added