diff --git a/request_information.go b/request_information.go index b072161..621192e 100644 --- a/request_information.go +++ b/request_information.go @@ -28,12 +28,16 @@ type RequestInformation struct { Headers *RequestHeaders // The Query Parameters of the request. // Deprecated: use QueryParametersAny instead - QueryParameters map[string]string + QueryParameters map[string]string + // The Query Parameters of the request. QueryParametersAny map[string]any // The Request Body. Content []byte // The path parameters to use for the URL template when generating the URI. + // Deprecated: use PathParametersAny instead PathParameters map[string]string + // The path parameters to use for the URL template when generating the URI. + PathParametersAny map[string]any // The Url template for the current request. UrlTemplate string options map[string]RequestOption @@ -49,6 +53,7 @@ func NewRequestInformation() *RequestInformation { QueryParametersAny: make(map[string]any), options: make(map[string]RequestOption), PathParameters: make(map[string]string), + PathParametersAny: make(map[string]any), } } @@ -103,6 +108,9 @@ func (request *RequestInformation) GetUri() (*u.URL, error) { for key, value := range request.PathParameters { substitutions[key] = value } + for key, value := range request.PathParametersAny { + substitutions[key] = request.normalizeEnumParameters(reflect.ValueOf(value), value, false) + } for key, value := range request.QueryParameters { substitutions[key] = value } @@ -513,21 +521,32 @@ func (request *RequestInformation) AddQueryParameters(source any) { if arr, ok := value.([]any); ok && len(arr) > 0 { request.QueryParametersAny[fieldName] = arr } - 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 - for i := range strRepresentations { - strRepresentations[i] = enumArr.Index(i).Interface().(kiotaEnum).String() - } - request.QueryParametersAny[fieldName] = strRepresentations - } + normalizedEnumValue := request.normalizeEnumParameters(valueOfValue, value, true) + if normalizedEnumValue != nil { + request.QueryParametersAny[fieldName] = normalizedEnumValue } - if enum, ok := value.(kiotaEnum); ok { - request.QueryParameters[fieldName] = enum.String() + } +} +func (request *RequestInformation) normalizeEnumParameters(valueOfValue reflect.Value, value any, returnNilIfNotEnum 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 + for i := range strRepresentations { + strRepresentations[i] = enumArr.Index(i).Interface().(kiotaEnum).String() + } + return strRepresentations } + } else if enum, ok := value.(kiotaEnum); ok { + return enum.String() + } + + if returnNilIfNotEnum { + return nil + } else { + return value } } diff --git a/request_information_test.go b/request_information_test.go index ab9c267..29122f7 100644 --- a/request_information_test.go +++ b/request_information_test.go @@ -208,6 +208,34 @@ func TestItSetsEnumValuesInQueryParameters(t *testing.T) { assert.Equal(t, "http://localhost/users?statuses=active,suspended", resultUri.String()) } +func TestItSetsEnumValueInPathParameters(t *testing.T) { + requestInformation := NewRequestInformation() + requestInformation.UrlTemplate = "{+baseurl}/{status}" + + status := internal.ACTIVE + requestInformation.PathParameters["baseurl"] = "http://localhost" + requestInformation.PathParametersAny["status"] = &status + + resultUri, err := requestInformation.GetUri() + assert.Nil(t, err) + assert.Equal(t, "http://localhost/active", resultUri.String()) +} + +func TestItSetsEnumValuesInPathParameters(t *testing.T) { + requestInformation := NewRequestInformation() + requestInformation.UrlTemplate = "{+baseurl}/{statuses}" + + statuses := make([]internal.PersonStatus, 2) + statuses[0] = internal.ACTIVE + statuses[1] = internal.SUSPENDED + requestInformation.PathParameters["baseurl"] = "http://localhost" + requestInformation.PathParametersAny["statuses"] = statuses + + resultUri, err := requestInformation.GetUri() + assert.Nil(t, err) + assert.Equal(t, "http://localhost/active,suspended", resultUri.String()) +} + func TestItSetsExplodedQueryParameters(t *testing.T) { value := true requestInformation := NewRequestInformation()