Skip to content

Commit

Permalink
Merge pull request #113 from papegaaij/exploded-query-params
Browse files Browse the repository at this point in the history
Add testcase to demonstrate broken exploded query parameters
  • Loading branch information
baywet committed Oct 31, 2023
2 parents 6c59617 + 20a8c3e commit c6b1cb2
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 8 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Expand Up @@ -11,6 +11,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

## [1.3.1] - 2023-10-31

### Changed

- Fixed an issue where query parameters of type array of anything else than string would not be expanded properly. [#114](https://github.com/microsoft/kiota-abstractions-go/issues/114)

## [1.3.0] - 2023-10-12

### Added
Expand Down
36 changes: 29 additions & 7 deletions request_information.go
Expand Up @@ -27,7 +27,9 @@ type RequestInformation struct {
// The Request Headers.
Headers *RequestHeaders
// The Query Parameters of the request.
QueryParameters map[string]string
// Deprecated: use QueryParametersAny instead
QueryParameters map[string]string
QueryParametersAny map[string]any
// The Request Body.
Content []byte
// The path parameters to use for the URL template when generating the URI.
Expand All @@ -42,10 +44,11 @@ const raw_url_key = "request-raw-url"
// NewRequestInformation creates a new RequestInformation object with default values.
func NewRequestInformation() *RequestInformation {
return &RequestInformation{
Headers: NewRequestHeaders(),
QueryParameters: make(map[string]string),
options: make(map[string]RequestOption),
PathParameters: make(map[string]string),
Headers: NewRequestHeaders(),
QueryParameters: make(map[string]string),
QueryParametersAny: make(map[string]any),
options: make(map[string]RequestOption),
PathParameters: make(map[string]string),
}
}

Expand All @@ -59,6 +62,8 @@ func (request *RequestInformation) GetUri() (*u.URL, error) {
return nil, errors.New("uri template parameters cannot be nil")
} else if request.QueryParameters == nil {
return nil, errors.New("uri query parameters cannot be nil")
} else if request.QueryParametersAny == nil {
return nil, errors.New("uri query parameters cannot be nil")
} else if request.PathParameters[raw_url_key] != "" {
uri, err := u.Parse(request.PathParameters[raw_url_key])
if err != nil {
Expand All @@ -79,6 +84,9 @@ func (request *RequestInformation) GetUri() (*u.URL, error) {
for key, value := range request.QueryParameters {
substitutions[key] = value
}
for key, value := range request.QueryParametersAny {
substitutions[key] = value
}
url, err := stduritemplate.Expand(request.UrlTemplate, substitutions)
if err != nil {
return nil, err
Expand All @@ -97,6 +105,9 @@ func (request *RequestInformation) SetUri(url u.URL) {
for k := range request.QueryParameters {
delete(request.QueryParameters, k)
}
for k := range request.QueryParametersAny {
delete(request.QueryParametersAny, k)
}
}

// AddRequestOptions adds an option to the request to be read by the middleware infrastructure.
Expand Down Expand Up @@ -465,9 +476,20 @@ func (request *RequestInformation) AddQueryParameters(source interface{}) {
if ok && it != nil {
request.QueryParameters[fieldName] = strconv.FormatInt(int64(*it), 10)
}
arr, ok := value.([]string)
strArr, ok := value.([]string)
if ok && len(strArr) > 0 {
// populating both query parameter fields to avoid breaking compatibility with code reading this field
request.QueryParameters[fieldName] = strings.Join(strArr, ",")

tmp := make([]any, len(strArr))
for i, v := range strArr {
tmp[i] = v
}
request.QueryParametersAny[fieldName] = tmp
}
arr, ok := value.([]any)
if ok && len(arr) > 0 {
request.QueryParameters[fieldName] = strings.Join(arr, ",")
request.QueryParametersAny[fieldName] = arr
}
}
}
32 changes: 31 additions & 1 deletion request_information_test.go
Expand Up @@ -15,6 +15,7 @@ import (
type QueryParameters struct {
Count *bool
Expand []string
ExpandAny []any
Filter *string
Orderby []string
Search *string
Expand All @@ -32,6 +33,7 @@ func TestItAddsStringQueryParameters(t *testing.T) {
requestInformation.AddQueryParameters(queryParameters)

assert.Equal(t, value, requestInformation.QueryParameters["Filter"])
assert.Nil(t, requestInformation.QueryParametersAny["Filter"])
}

func TestItAddsBoolQueryParameters(t *testing.T) {
Expand All @@ -42,6 +44,7 @@ func TestItAddsBoolQueryParameters(t *testing.T) {
}
requestInformation.AddQueryParameters(queryParameters)
assert.Equal(t, "true", requestInformation.QueryParameters["Count"])
assert.Nil(t, requestInformation.QueryParametersAny["Count"])
}

func TestItAddsIntQueryParameters(t *testing.T) {
Expand All @@ -52,6 +55,7 @@ func TestItAddsIntQueryParameters(t *testing.T) {
}
requestInformation.AddQueryParameters(queryParameters)
assert.Equal(t, "42", requestInformation.QueryParameters["Top"])
assert.Nil(t, requestInformation.QueryParametersAny["Top"])
}

func TestItAddsStringArrayQueryParameters(t *testing.T) {
Expand All @@ -62,6 +66,18 @@ func TestItAddsStringArrayQueryParameters(t *testing.T) {
}
requestInformation.AddQueryParameters(queryParameters)
assert.Equal(t, "somefilter,someotherfilter", requestInformation.QueryParameters["Expand"])
assert.Equal(t, []any{"somefilter", "someotherfilter"}, requestInformation.QueryParametersAny["Expand"])
}

func TestItAddsAnyArrayQueryParameters(t *testing.T) {
requestInformation := NewRequestInformation()
value := []any{"somefilter", "someotherfilter"}
queryParameters := QueryParameters{
ExpandAny: value,
}
requestInformation.AddQueryParameters(queryParameters)
assert.Empty(t, requestInformation.QueryParameters["ExpandAny"])
assert.Equal(t, []any{"somefilter", "someotherfilter"}, requestInformation.QueryParametersAny["ExpandAny"])
}

func TestItSetsTheRawURL(t *testing.T) {
Expand All @@ -75,6 +91,7 @@ func TestItSetsTheRawURL(t *testing.T) {
assert.Nil(t, err)
assert.Equal(t, "https://someurl.com", uri.String())
assert.Equal(t, 0, len(requestInformation.QueryParameters))
assert.Equal(t, 0, len(requestInformation.QueryParametersAny))
}

type getQueryParameters struct {
Expand All @@ -96,7 +113,7 @@ func TestItSetsSelectAndCountQueryParameters(t *testing.T) {
})
resultUri, err := requestInformation.GetUri()
assert.Nil(t, err)
assert.Equal(t, "http://localhost/me?%24select=id%2CdisplayName&%24count=true", resultUri.String())
assert.Equal(t, "http://localhost/me?%24select=id,displayName&%24count=true", resultUri.String())
}

func TestItDoesNotSetEmptySelectQueryParameters(t *testing.T) {
Expand Down Expand Up @@ -157,6 +174,19 @@ func TestItBuildsUrlOnProvidedBaseUrl(t *testing.T) {
assert.Equal(t, "http://localhost/users", resultUri.String())
}

func TestItSetsExplodedQueryParameters(t *testing.T) {
value := true
requestInformation := NewRequestInformation()
requestInformation.UrlTemplate = "http://localhost/me{?%24select*}"
requestInformation.AddQueryParameters(getQueryParameters{
Select_escaped: []string{"id", "displayName"},
Count: &value,
})
resultUri, err := requestInformation.GetUri()
assert.Nil(t, err)
assert.Equal(t, "http://localhost/me?%24select=id&%24select=displayName", resultUri.String())
}

func TestItSetsContentFromParsable(t *testing.T) {
requestInformation := NewRequestInformation()
requestInformation.UrlTemplate = "{+baseurl}/users{?%24count}"
Expand Down

0 comments on commit c6b1cb2

Please sign in to comment.