From 2295389270aa4724afec1b37aa88dfc74ac6b116 Mon Sep 17 00:00:00 2001 From: Rex Zeng Date: Fri, 28 Dec 2018 21:24:44 +0800 Subject: [PATCH] fix(roller): Drop null values in json If the request body is a JSON like `{"test":null}`, govalidator will panic when executing `traverseMap`. ```go var m = make(map[string]interface{}) json.Unmarshal([]byte(`{"test":null}`), &m) ``` Since `m["test"]` is `nil`, `reflect.TypeOf(v)` will panic. This PR fixes the bug by dropping null values in json. --- roller.go | 4 ++++ roller_test.go | 1 + 2 files changed, 5 insertions(+) diff --git a/roller.go b/roller.go index 11e4a58..45431e2 100644 --- a/roller.go +++ b/roller.go @@ -183,6 +183,10 @@ func (r *roller) traverseMap(iface interface{}) { switch t := iface.(type) { case map[string]interface{}: for k, v := range t { + // drop null values in json to prevent panic caused by reflect.TypeOf(nil) + if v == nil { + continue + } switch reflect.TypeOf(v).Kind() { case reflect.Struct: r.typeName = k // set the map key as name diff --git a/roller_test.go b/roller_test.go index 30e2032..e6a46b7 100644 --- a/roller_test.go +++ b/roller_test.go @@ -83,6 +83,7 @@ func init() { m["person"] = "John Doe" m["iface"] = map[string]string{"another_person": "does it change root!"} m["array"] = [5]int{1, 4, 5, 6, 7} + m["null"] = nil } func TestRoller_push(t *testing.T) {