Skip to content

Commit 8625cae

Browse files
authored
Merge pull request #51 from RexSkz/rex/prevent-panic-caused-by-null-value
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.
2 parents ee7f63f + 2295389 commit 8625cae

File tree

2 files changed

+5
-0
lines changed

2 files changed

+5
-0
lines changed

roller.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ func (r *roller) traverseMap(iface interface{}) {
183183
switch t := iface.(type) {
184184
case map[string]interface{}:
185185
for k, v := range t {
186+
// drop null values in json to prevent panic caused by reflect.TypeOf(nil)
187+
if v == nil {
188+
continue
189+
}
186190
switch reflect.TypeOf(v).Kind() {
187191
case reflect.Struct:
188192
r.typeName = k // set the map key as name

roller_test.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ func init() {
8383
m["person"] = "John Doe"
8484
m["iface"] = map[string]string{"another_person": "does it change root!"}
8585
m["array"] = [5]int{1, 4, 5, 6, 7}
86+
m["null"] = nil
8687
}
8788

8889
func TestRoller_push(t *testing.T) {

0 commit comments

Comments
 (0)