Skip to content

Commit

Permalink
Merge pull request #124 from papegaaij/fix-int64-slice-query-paramers
Browse files Browse the repository at this point in the history
Parameters with lists of numbers not supported in URIs
  • Loading branch information
baywet authored Nov 24, 2023
2 parents edbb11f + de1d6c9 commit 920cc73
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 7 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
25 changes: 18 additions & 7 deletions request_information.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions request_information_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 920cc73

Please sign in to comment.