Fix issue #10 to avoid panics with invalid JSON

This commit is contained in:
Christian Klotz 2015-08-29 18:29:58 +01:00
parent e93213ecfb
commit b11e9985b4
2 changed files with 19 additions and 1 deletions

View File

@ -65,7 +65,13 @@ func UnmarshalPayload(in io.Reader, model interface{}) error {
}
func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) error {
func unmarshalNode(data *Node, model reflect.Value, included *map[string]*Node) (err error) {
defer func() {
if r := recover(); r != nil {
err = fmt.Errorf("data is not a jsonapi representation of '%v'", model.Type())
}
}()
modelValue := model.Elem()
modelType := model.Type().Elem()

View File

@ -5,6 +5,7 @@ import (
"encoding/json"
"io"
"regexp"
"strings"
"testing"
"time"
)
@ -27,6 +28,17 @@ func TestMalformedTag(t *testing.T) {
}
}
func TestUnmarshalInvalidJSON(t *testing.T) {
in := strings.NewReader("{}")
out := new(Blog)
err := UnmarshalPayload(in, out)
if err == nil {
t.Fatalf("Did not error out the invalid JSON.")
}
}
func TestUnmarshalSetsId(t *testing.T) {
in := samplePayloadWithId()
out := new(Blog)