Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add testcase to demonstrate broken exploded query parameters #113

Merged
merged 6 commits into from
Oct 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.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
Original file line number Diff line number Diff line change
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, ",")
baywet marked this conversation as resolved.
Show resolved Hide resolved

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
Original file line number Diff line number Diff line change
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