forked from Mirrors/jsonapi
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:
commit
57cb7c9f3f
|
@ -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()
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
|
Loading…
Reference in New Issue