Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure uninitialized map is initialized when unmarshaling json #609

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions slices/map.go
Expand Up @@ -51,6 +51,9 @@ func (m Map) UnmarshalJSON(b []byte) error {
if err != nil {
return err
}
if m == nil {
m = Map{}
}
for key, value := range stuff {
m[key] = value
}
Expand Down
11 changes: 11 additions & 0 deletions slices/map_test.go
Expand Up @@ -24,3 +24,14 @@ func Test_Map_MarshalJSON(t *testing.T) {
r.NoError(err)
r.Equal([]byte(`{"a":"b"}`), b)
}

func Test_Map_UnMarshalJSON_uninitialized_map_does_not_panic(t *testing.T) {
r := require.New(t)

maps := make([]Map, 0)
r.NotPanics(func() {
err := json.Unmarshal([]byte(`[{"a": "b"}]`), &maps)
r.NoError(err)
r.Len(maps, 1)
})
}