Skip to content

Commit 3b7b6e5

Browse files
fix incorrect marshalling of query parameters
1 parent 46602dc commit 3b7b6e5

15 files changed

+98
-59
lines changed

attachment-api/attachment_request_builder_file_query_parameters.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,11 @@ type AttachmentRequestBuilderFileQueryParameters struct {
55
//Specify this parameter to allow only users with the specified encryption context to access the attachment.
66
//For additional information on encryption context records,
77
// see [Encryption Support]:https://docs.servicenow.com/csh?topicname=c_EncryptionSupport&version=vancouver&pubname=vancouver-platform-security.
8-
EncryptionContext string `query:"encryption_context"`
8+
EncryptionContext string `url:"encryption_context"`
99
//FileName Name to give the attachment.
10-
FileName string `query:"file_name"`
10+
FileName string `url:"file_name"`
1111
//TableName Name of the table to attach the file to.
12-
TableName string `query:"table_name"`
12+
TableName string `url:"table_name"`
1313
//TableSysId Sys_id of the record in the table specified in table_name that you want to attach the file to.
14-
TableSysId string `query:"table_sys_id"` //nolint:stylecheck
14+
TableSysId string `url:"table_sys_id"` //nolint:stylecheck
1515
}

attachment-api/attachment_request_builder_get_query_parameters.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ package attachmentapi
22

33
type AttachmentRequestBuilderGetQueryParameters struct {
44
// Limit Limit to be applied on pagination.
5-
Limit int `query:"sysparm_limit"`
5+
Limit int `url:"sysparm_limit"`
66
// Offset Number of records to exclude from the query. Use this parameter to get more records than specified in sysparm_limit. For example, if sysparm_limit is set to 500, but there are additional records you want to query, you can specify a sysparm_offset value of 500 to get the second set of records.
7-
Offset int `query:"sysparm_offset"`
7+
Offset int `url:"sysparm_offset"`
88
// Query Encoded query. Queries for the Attachment API are relative to the Attachments [sys_attachment] table.
9-
Query string `query:"sysparm_query"`
9+
Query string `url:"sysparm_query"`
1010
}

core/helper.go

+35-11
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,57 @@ package core
22

33
import (
44
"encoding/json"
5+
"fmt"
56
"io"
67
"net/http"
78
"reflect"
89
"strings"
910

10-
"github.com/hetiansu5/urlquery"
11+
"github.com/google/go-querystring/query"
1112
"github.com/yosida95/uritemplate/v3"
1213
)
1314

15+
func toQueryMapFromStruct(source interface{}) (map[string]string, error) {
16+
queryValues, err := query.Values(source)
17+
if err != nil {
18+
return nil, err
19+
}
20+
21+
queryParams := map[string]string{}
22+
23+
for key, values := range queryValues {
24+
queryParams[key] = strings.Join(values, ",")
25+
}
26+
27+
return queryParams, nil
28+
}
29+
1430
// ToQueryMap converts a struct to query parameter map
1531
func ToQueryMap(source interface{}) (map[string]string, error) {
16-
if source == nil {
32+
var err error
33+
34+
if isNil(source) {
1735
return nil, ErrNilSource
1836
}
1937

20-
queryBytes, err := urlquery.Marshal(source)
21-
if err != nil {
22-
return nil, err
23-
}
38+
queryParams := make(map[string]string)
2439

25-
var queryMap map[string]string
26-
err = urlquery.Unmarshal(queryBytes, &queryMap)
27-
if err != nil {
28-
return nil, err
40+
sourceType := reflect.TypeOf(source)
41+
42+
if sourceType.Kind() != reflect.Map {
43+
source, err = toQueryMapFromStruct(source)
44+
if err != nil {
45+
return nil, err
46+
}
2947
}
3048

31-
return queryMap, nil
49+
sourceValue := reflect.ValueOf(source)
50+
for _, key := range sourceValue.MapKeys() {
51+
strKey := fmt.Sprintf("%v", key.Interface())
52+
strValue := fmt.Sprintf("%v", sourceValue.MapIndex(key).Interface())
53+
queryParams[strKey] = strValue
54+
}
55+
return queryParams, nil
3256
}
3357

3458
// normalizeVarNames normalizes variable names for URI template expansion.

core/helper_test.go

+14-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,11 @@ package core
33
import (
44
"crypto/rand"
55
"errors"
6+
"fmt"
67
"io"
78
"math/big"
89
"net/http"
10+
"reflect"
911
"strings"
1012
"testing"
1113

@@ -64,16 +66,16 @@ func TestToQueryMap(t *testing.T) {
6466
}{
6567
{
6668
Input: struct {
67-
Param1 string `query:"param_1"`
68-
Param2 int `query:"param_2"`
69-
Param3 bool `query:"param_3"`
69+
Param1 string `url:"param_1"`
70+
Param2 int `url:"param_2"`
71+
Param3 bool `url:"param_3"`
7072
}{
7173
Param1: "value1",
7274
Param2: 5,
7375
Param3: true,
7476
},
7577
ShouldError: false,
76-
Expected: map[string]string{"param_1": "value1", "param_2": "5", "param_3": "1"},
78+
Expected: map[string]string{"param_1": "value1", "param_2": "5", "param_3": "true"},
7779
CheckErr: nil,
7880
},
7981
{
@@ -82,6 +84,14 @@ func TestToQueryMap(t *testing.T) {
8284
Expected: nil,
8385
CheckErr: func(err error) bool { return assert.Equal(t, ErrNilSource, err) },
8486
},
87+
{
88+
Input: "test",
89+
ShouldError: true,
90+
Expected: nil,
91+
CheckErr: func(err error) bool {
92+
return assert.Equal(t, fmt.Errorf("query: Values() expects struct input. Got %v", reflect.String), err)
93+
},
94+
},
8595
}
8696

8797
for _, input := range inputs {

core/request_information_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ func TestNewRequestInformationSetStreamContenr(t *testing.T) {
3434

3535
func TestNewRequestInformationAddQueryParameters(t *testing.T) {
3636
source := struct {
37-
Var1 string `query:"var_1"`
37+
Var1 string `url:"var_1"`
3838
}{
3939
Var1: "Val1",
4040
}

core/url_information_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -186,8 +186,8 @@ func TestUrlInformationAddQueryParameters(t *testing.T) {
186186
ui := NewURLInformation()
187187
ui.QueryParameters = make(map[string]string)
188188
source := struct {
189-
Param1 string `query:"param1"`
190-
Param2 string `query:"param2"`
189+
Param1 string `url:"param1"`
190+
Param2 string `url:"param2"`
191191
}{Param1: "value1", Param2: "value2"}
192192

193193
err := ui.AddQueryParameters(source)

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ require (
1212

1313
require (
1414
github.com/davecgh/go-spew v1.1.1 // indirect
15+
github.com/google/go-querystring v1.1.0 // indirect
1516
github.com/kr/pretty v0.1.0 // indirect
1617
github.com/pmezard/go-difflib v1.0.0 // indirect
1718
golang.org/x/net v0.19.0 // indirect

go.sum

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c
22
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
33
github.com/gabriel-vasile/mimetype v1.4.3 h1:in2uUcidCuFcDKtdcBxlR0rJ1+fsokWf+uqxgUFjbI0=
44
github.com/gabriel-vasile/mimetype v1.4.3/go.mod h1:d8uq/6HKRL6CGdk+aubisF/M5GcPfT7nKyLpA0lbSSk=
5+
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
6+
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
7+
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
58
github.com/hetiansu5/urlquery v1.2.7 h1:jn0h+9pIRqUziSPnRdK/gJK8S5TCnk+HZZx5fRHf8K0=
69
github.com/hetiansu5/urlquery v1.2.7/go.mod h1:wFpZdTHRdwt7mk0EM/DdZEWtEN4xf8HJoH/BLXm/PG0=
710
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
@@ -19,6 +22,7 @@ github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zI
1922
github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=
2023
golang.org/x/net v0.19.0 h1:zTwKpTd2XuCqf8huc7Fo2iSy+4RHPd10s4KzeTnVr1c=
2124
golang.org/x/net v0.19.0/go.mod h1:CfAk/cbD4CthTvqiEl8NpboMuiuOYsAr/7NOjZJtv1U=
25+
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
2226
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
2327
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
2428
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=

table-api/table_item_request_builder_delete_query_parameters.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@ type TableItemRequestBuilderDeleteQueryParameters struct {
99
//- false: Exclude the record if it is in a domain that the currently logged in user is not configured to access.
1010
//
1111
//- true: Include the record even if it is in a domain that the currently logged in user is not configured to access.
12-
QueryNoDomain bool `query:"sysparm_query_no_domain"`
12+
QueryNoDomain bool `url:"sysparm_query_no_domain"`
1313
}

table-api/table_item_request_builder_delete_query_parameters_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ func TestTableItemRequestBuilderDeleteQueryParameters(t *testing.T) {
1717
t.Error(err)
1818
}
1919

20-
expectedValue := map[string]string{"sysparm_query_no_domain": "1"}
20+
expectedValue := map[string]string{"sysparm_query_no_domain": "true"}
2121

2222
assert.Equal(t, expectedValue, queryMap)
2323
}

table-api/table_item_request_builder_get_query_parameters.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ type TableItemRequestBuilderGetQueryParameters struct {
1212
//- Encrypted text: The database value is encrypted, while the displayed value is unencrypted based on the user's encryption context.
1313
//
1414
//- Reference fields: The database value is sys_id, but the display value is a display field of the referenced record.
15-
DisplayValue DisplayValue `query:"sysparm_display_value"`
15+
DisplayValue DisplayValue `url:"sysparm_display_value"`
1616
//ExcludeReferenceLink flag that indicates whether to exclude Table API links for reference fields.
1717
//
1818
//Valid values:
1919
//
2020
//- true: Exclude Table API links for reference fields.
2121
//
2222
//- false: Include Table API links for reference fields.
23-
ExcludeReferenceLink bool `query:"sysparm_exclude_reference_link"`
23+
ExcludeReferenceLink bool `url:"sysparm_exclude_reference_link"`
2424
// Fields list of fields to return in the response.
25-
Fields []string `query:"sysparm_fields"`
25+
Fields []string `url:"sysparm_fields"`
2626
//Flag that indicates whether to restrict the record search to only the domains for which the logged in user is configured.
2727
//
2828
//Valid values:
2929
//
3030
//- false: Exclude the record if it is in a domain that the currently logged in user is not configured to access.
3131
//
3232
//- true: Include the record even if it is in a domain that the currently logged in user is not configured to access.
33-
QueryNoDomain bool `query:"sysparm_query_no_domain"`
33+
QueryNoDomain bool `url:"sysparm_query_no_domain"`
3434
// View UI view for which to render the data. Determines the fields returned in the response.
3535
//
3636
//Valid values:
@@ -39,5 +39,5 @@ type TableItemRequestBuilderGetQueryParameters struct {
3939
//- mobile
4040
//- both
4141
//If you also specify the sysparm_fields parameter, it takes precedent.
42-
View View `query:"sysparm_view"`
42+
View View `url:"sysparm_view"`
4343
}

table-api/table_item_request_builder_put_query_parameters.go

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,26 @@ type TableItemRequestBuilderPutQueryParameters struct {
1212
//- Encrypted text: The database value is encrypted, while the displayed value is unencrypted based on the user's encryption context.
1313
//
1414
//- Reference fields: The database value is sys_id, but the display value is a display field of the referenced record.
15-
DisplayValue DisplayValue `query:"sysparm_display_value"`
15+
DisplayValue DisplayValue `url:"sysparm_display_value"`
1616
//ExcludeReferenceLink flag that indicates whether to exclude Table API links for reference fields.
1717
//
1818
//Valid values:
1919
//
2020
//- true: Exclude Table API links for reference fields.
2121
//
2222
//- false: Include Table API links for reference fields.
23-
ExcludeReferenceLink bool `query:"sysparm_exclude_reference_link"`
23+
ExcludeReferenceLink bool `url:"sysparm_exclude_reference_link"`
2424
// Fields list of fields to return in the response.
25-
Fields []string `query:"sysparm_fields"`
26-
InputDisplayValue bool `query:"sysparm_input_display_value"`
25+
Fields []string `url:"sysparm_fields"`
26+
InputDisplayValue bool `url:"sysparm_input_display_value"`
2727
// QueryNoDomain flag that indicates whether to restrict the record search to only the domains for which the logged in user is configured.
2828
//
2929
//Valid values:
3030
//
3131
//- false: Exclude the record if it is in a domain that the currently logged in user is not configured to access.
3232
//
3333
//- true: Include the record even if it is in a domain that the currently logged in user is not configured to access.
34-
QueryNoDomain bool `query:"sysparm_query_no_domain"`
34+
QueryNoDomain bool `url:"sysparm_query_no_domain"`
3535
// View UI view for which to render the data. Determines the fields returned in the response.
3636
//
3737
//Valid values:
@@ -40,5 +40,5 @@ type TableItemRequestBuilderPutQueryParameters struct {
4040
//- mobile
4141
//- both
4242
//If you also specify the sysparm_fields parameter, it takes precedent.
43-
View View `query:"sysparm_view"`
43+
View View `url:"sysparm_view"`
4444
}

table-api/table_request_builder_get_query_parameters.go

+11-11
Original file line numberDiff line numberDiff line change
@@ -12,25 +12,25 @@ type TableRequestBuilderGetQueryParameters struct {
1212
//- Encrypted text: The database value is encrypted, while the displayed value is unencrypted based on the user's encryption context.
1313
//
1414
//- Reference fields: The database value is sys_id, but the display value is a display field of the referenced record.
15-
DisplayValue DisplayValue `query:"sysparm_display_value"`
15+
DisplayValue DisplayValue `url:"sysparm_display_value,omitempty"`
1616
//Flag that indicates whether to exclude Table API links for reference fields.
1717
//
1818
//Valid values:
1919
//
2020
//- true: Exclude Table API links for reference fields.
2121
//
2222
//- false: Include Table API links for reference fields.
23-
ExcludeReferenceLink bool `query:"sysparm_exclude_reference_link"`
23+
ExcludeReferenceLink bool `url:"sysparm_exclude_reference_link,omitempty"`
2424
//list of fields to return in the response.
25-
Fields []string `query:"sysparm_fields"`
25+
Fields []string `url:"sysparm_fields,omitempty"`
2626
//Flag that indicates whether to restrict the record search to only the domains for which the logged in user is configured.
2727
//
2828
//Valid values:
2929
//
3030
//- false: Exclude the record if it is in a domain that the currently logged in user is not configured to access.
3131
//
3232
//- true: Include the record even if it is in a domain that the currently logged in user is not configured to access.
33-
QueryNoDomain bool `query:"sysparm_query_no_domain"`
33+
QueryNoDomain bool `url:"sysparm_query_no_domain,omitempty"`
3434
// UI view for which to render the data. Determines the fields returned in the response.
3535
//
3636
//Valid values:
@@ -39,11 +39,11 @@ type TableRequestBuilderGetQueryParameters struct {
3939
//- mobile
4040
//- both
4141
//If you also specify the sysparm_fields parameter, it takes precedent.
42-
View View `query:"sysparm_view"`
43-
Limit int `query:"sysparm_limit"`
44-
NoCount bool `query:"sysparm_no_count"`
45-
Offset int `query:"sysparm_offset"`
46-
Query string `query:"sysparm_query"`
47-
QueryCategory string `query:"sysparm_query_category"`
48-
SuppressPaginationHeader bool `uriparameter:"sysparm_suppress_pagination_header"`
42+
View View `url:"sysparm_view,omitempty"`
43+
Limit int `url:"sysparm_limit,omitempty"`
44+
NoCount bool `url:"sysparm_no_count,omitempty"`
45+
Offset int `url:"sysparm_offset,omitempty"`
46+
Query string `url:"sysparm_query,omitempty"`
47+
QueryCategory string `url:"sysparm_query_category,omitempty"`
48+
SuppressPaginationHeader bool `url:"sysparm_suppress_pagination_header,omitempty"`
4949
}

table-api/table_request_builder_post_query_parameters.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -12,18 +12,18 @@ type TableRequestBuilderPostQueryParameters struct {
1212
//- Encrypted text: The database value is encrypted, while the displayed value is unencrypted based on the user's encryption context.
1313
//
1414
//- Reference fields: The database value is sys_id, but the display value is a display field of the referenced record.
15-
DisplayValue DisplayValue `query:"sysparm_display_value"`
15+
DisplayValue DisplayValue `url:"sysparm_display_value"`
1616
//Flag that indicates whether to exclude Table API links for reference fields.
1717
//
1818
//Valid values:
1919
//
2020
//- true: Exclude Table API links for reference fields.
2121
//
2222
//- false: Include Table API links for reference fields.
23-
ExcludeReferenceLink bool `query:"sysparm_exclude_reference_link"`
23+
ExcludeReferenceLink bool `url:"sysparm_exclude_reference_link"`
2424
//list of fields to return in the response.
25-
Fields []string `query:"sysparm_fields"`
26-
InputDisplayValue bool `query:"sysparm_input_display_value"`
25+
Fields []string `url:"sysparm_fields"`
26+
InputDisplayValue bool `url:"sysparm_input_display_value"`
2727
// UI view for which to render the data. Determines the fields returned in the response.
2828
//
2929
//Valid values:
@@ -32,5 +32,5 @@ type TableRequestBuilderPostQueryParameters struct {
3232
//- mobile
3333
//- both
3434
//If you also specify the sysparm_fields parameter, it takes precedent.
35-
View View `query:"sysparm_view"`
35+
View View `url:"sysparm_view"`
3636
}

table-api/table_request_builder_post_query_paramters.go

+5-5
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,18 @@ type TableRequestBuilderPostQueryParamters struct {
1414
//- Encrypted text: The database value is encrypted, while the displayed value is unencrypted based on the user's encryption context.
1515
//
1616
//- Reference fields: The database value is sys_id, but the display value is a display field of the referenced record.
17-
DisplayValue DisplayValue `query:"sysparm_display_value"`
17+
DisplayValue DisplayValue `url:"sysparm_display_value"`
1818
//Flag that indicates whether to exclude Table API links for reference fields.
1919
//
2020
//Valid values:
2121
//
2222
//- true: Exclude Table API links for reference fields.
2323
//
2424
//- false: Include Table API links for reference fields.
25-
ExcludeReferenceLink bool `query:"sysparm_exclude_reference_link"`
25+
ExcludeReferenceLink bool `url:"sysparm_exclude_reference_link"`
2626
//list of fields to return in the response.
27-
Fields []string `query:"sysparm_fields"`
28-
InputDisplayValue bool `query:"sysparm_input_display_value"`
27+
Fields []string `url:"sysparm_fields"`
28+
InputDisplayValue bool `url:"sysparm_input_display_value"`
2929
// UI view for which to render the data. Determines the fields returned in the response.
3030
//
3131
//Valid values:
@@ -34,5 +34,5 @@ type TableRequestBuilderPostQueryParamters struct {
3434
//- mobile
3535
//- both
3636
//If you also specify the sysparm_fields parameter, it takes precedent.
37-
View View `query:"sysparm_view"`
37+
View View `url:"sysparm_view"`
3838
}

0 commit comments

Comments
 (0)