Merge remote-tracking branch 'origin/master' into client-ids

* origin/master:
  Fix issue #10 to avoid panics with invalid JSON
This commit is contained in:
Sam Woodard 2015-10-12 11:08:23 -07:00
commit 57cb7c9f3f
2 changed files with 36 additions and 1 deletions

View File

@ -66,7 +66,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

@ -3,7 +3,10 @@ package jsonapi
import (
"bytes"
"encoding/json"
"fmt"
"io"
"regexp"
"strings"
"testing"
"time"
)
@ -12,6 +15,32 @@ type BadModel struct {
Id int `jsonapi:"primary"`
}
func TestMalformedTag(t *testing.T) {
out := new(BadModel)
err := UnmarshalPayload(samplePayload(), out)
if err == nil {
t.Fatalf("Did not error out with wrong number of arguments in tag")
}
fmt.Println(err.Error())
r := regexp.MustCompile(`too few arguments`)
if !r.Match([]byte(err.Error())) {
t.Fatalf("The err was not due too few arguments in a tag")
}
}
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)