Merge pull request #13 from christianklotz/master

Fix issue #10 to avoid panics with invalid JSON
This commit is contained in:
Sam Woodard 2015-09-12 11:00:52 -07:00
commit 2b929743a5
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)