Skip to content
Open
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
11 changes: 11 additions & 0 deletions pkg/parsers/parsers.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ func ParseMapForFormData(params map[string]interface{}, parent string, index int
keyname = key
}

// Handle null values by sending an empty string
if value == nil {
data = append(data, fmt.Sprintf("%s=", keyname))
continue
}

// Check the type of the value for this pair. If this is a
// terminal type, append the data with the key. For maps and
// arrays, keep parsing.
Expand Down Expand Up @@ -227,6 +233,11 @@ func ParseArrayForFormData(params []interface{}, parent string, queryRespMap map
// The index is only used for arrays of maps
index := -1
for _, value := range params {
if value == nil {
data = append(data, fmt.Sprintf("%s[]=", parent))
continue
}

switch v := reflect.ValueOf(value); v.Kind() {
case reflect.String:
// A string can be a regular value or one we need to look up first, ex: ${product.id}
Expand Down
30 changes: 30 additions & 0 deletions pkg/parsers/parsers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,36 @@ func TestParseInterfaceToJSON(t *testing.T) {
require.Equal(t, "tax_id_data[1][value]=value_1", output[7])
}

func TestParseInterfaceWithNulls(t *testing.T) {
data := map[string]interface{}{
"name": "Bender Bending Rodriguez",
"description": nil,
}

output, _ := ParseToFormData(data, make(map[string]gjson.Result))

require.Equal(t, 2, len(output))
require.Contains(t, output, "description=")
require.Contains(t, output, "name=Bender Bending Rodriguez")
}

func TestParseArrayWithNulls(t *testing.T) {
data := map[string]interface{}{
"tax_id_data": []interface{}{
map[string]interface{}{"type": "type_0", "value": nil},
nil,
},
}

output, _ := ParseToFormData(data, make(map[string]gjson.Result))
sort.Strings(output)

require.Equal(t, 3, len(output))
require.Contains(t, output, "tax_id_data[0][type]=type_0")
require.Contains(t, output, "tax_id_data[0][value]=")
require.Contains(t, output, "tax_id_data[]=")
}

func TestParseWithQueryIgnoreDefault(t *testing.T) {
queryRespMap := map[string]gjson.Result{
"cust_bender": gjson.Parse(`{"id": "cust_bend123456789", "currency": "eur"}`),
Expand Down