Skip to content

Commit

Permalink
Fix WithQueryParameters to properly encode multi-values (#467)
Browse files Browse the repository at this point in the history
The call to ensureValueStrings() was flattening slices into a string and
as a result multi-value query parameters were malformed.
  • Loading branch information
jhendrixMSFT authored Sep 6, 2019
1 parent 403aff9 commit 69b4126
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 10 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
# CHANGELOG

## v13.0.1

## Bug Fixes

- Fixed `autorest.WithQueryParameters()` so that it properly encodes multi-value query parameters.

## v13.0.0

## Breaking Changes
Expand Down
14 changes: 8 additions & 6 deletions autorest/preparer.go
Original file line number Diff line number Diff line change
Expand Up @@ -523,22 +523,24 @@ func parseURL(u *url.URL, path string) (*url.URL, error) {
// WithQueryParameters returns a PrepareDecorators that encodes and applies the query parameters
// given in the supplied map (i.e., key=value).
func WithQueryParameters(queryParameters map[string]interface{}) PrepareDecorator {
parameters := ensureValueStrings(queryParameters)
parameters := MapToValues(queryParameters)
return func(p Preparer) Preparer {
return PreparerFunc(func(r *http.Request) (*http.Request, error) {
r, err := p.Prepare(r)
if err == nil {
if r.URL == nil {
return r, NewError("autorest", "WithQueryParameters", "Invoked with a nil URL")
}

v := r.URL.Query()
for key, value := range parameters {
d, err := url.QueryUnescape(value)
if err != nil {
return r, err
for i := range value {
d, err := url.QueryUnescape(value[i])
if err != nil {
return r, err
}
value[i] = d
}
v.Add(key, d)
v[key] = value
}
r.URL.RawQuery = v.Encode()
}
Expand Down
7 changes: 4 additions & 3 deletions autorest/preparer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,8 @@ func ExampleWithPathParameters() {
// Create a request with query parameters
func ExampleWithQueryParameters() {
params := map[string]interface{}{
"q1": "value1",
"q2": "value2",
"q1": []string{"value1"},
"q2": []string{"value2"},
}
r, err := Prepare(&http.Request{},
WithBaseURL("https://microsoft.com/"),
Expand Down Expand Up @@ -835,6 +835,7 @@ func TestModifyingRequestWithExistingQueryParameters(t *testing.T) {
WithPath("search"),
WithQueryParameters(map[string]interface{}{"q": "golang the best"}),
WithQueryParameters(map[string]interface{}{"pq": "golang+encoded"}),
WithQueryParameters(map[string]interface{}{"zq": []string{"one", "two"}}),
)
if err != nil {
t.Fatalf("autorest: Preparing an existing request returned an error (%v)", err)
Expand All @@ -848,7 +849,7 @@ func TestModifyingRequestWithExistingQueryParameters(t *testing.T) {
t.Fatalf("autorest: Preparing an existing request failed when setting the path (%s)", r.URL.Path)
}

if r.URL.RawQuery != "pq=golang+encoded&q=golang+the+best" {
if r.URL.RawQuery != "pq=golang+encoded&q=golang+the+best&zq=one&zq=two" {
t.Fatalf("autorest: Preparing an existing request failed when setting the query parameters (%s)", r.URL.RawQuery)
}
}
Expand Down
2 changes: 1 addition & 1 deletion autorest/version.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"runtime"
)

const number = "v13.0.0"
const number = "v13.0.1"

var (
userAgent = fmt.Sprintf("Go/%s (%s-%s) go-autorest/%s",
Expand Down

0 comments on commit 69b4126

Please sign in to comment.