Skip to content

Commit

Permalink
Merge pull request #51 from RexSkz/rex/prevent-panic-caused-by-null-v…
Browse files Browse the repository at this point in the history
…alue

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.
  • Loading branch information
thedevsaddam committed Dec 28, 2018
2 parents ee7f63f + 2295389 commit 8625cae
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 0 deletions.
4 changes: 4 additions & 0 deletions roller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions roller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 8625cae

Please sign in to comment.