forked from Mirrors/jsonapi
Merge pull request #13 from christianklotz/master
Fix issue #10 to avoid panics with invalid JSON
This commit is contained in:
commit
2b929743a5
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue