diff --git a/request.go b/request.go index 03a7d0d..4f30021 100644 --- a/request.go +++ b/request.go @@ -42,6 +42,11 @@ func unmarshalJsonApiNode(data *JsonApiNode, model reflect.Value) error { args := strings.Split(tag, ",") + if len(args) != 2 { + er = errors.New(fmt.Sprintf("jsonapi tag, on %s, had two few arguments", fieldType.Name)) + return false + } + if len(args) >= 1 && args[0] != "" { annotation := args[0] diff --git a/request_test.go b/request_test.go index 56b4b1b..a74e337 100644 --- a/request_test.go +++ b/request_test.go @@ -4,9 +4,28 @@ import ( "bytes" "encoding/json" "io" + "regexp" "testing" ) +type BadModel struct { + Id int `jsonapi:"primary"` +} + +func TestMalformedTag(t *testing.T) { + out := new(BadModel) + err := UnmarshalJsonApiPayload(samplePayload(), out) + if err == nil { + t.Fatalf("Did not error out with wrong number of arguments in tag") + } + + r := regexp.MustCompile(`two few arguments`) + + if !r.Match([]byte(err.Error())) { + t.Fatalf("The err was not due two two few arguments in a tag") + } +} + //func TestUnmarshalSetsId(t *testing.T) { //in := samplePayload() //out := new(Blog) @@ -21,10 +40,8 @@ import ( //} func TestUnmarshalSetsAttrs(t *testing.T) { - in := samplePayload() - out := new(Blog) - - if err := UnmarshalJsonApiPayload(in, out); err != nil { + out, err := unmarshalSamplePayload() + if err != nil { t.Fatal(err) } @@ -43,10 +60,8 @@ func TestUnmarshalSetsAttrs(t *testing.T) { } func TestUnmarshalRelationships(t *testing.T) { - in := samplePayload() - out := new(Blog) - - if err := UnmarshalJsonApiPayload(in, out); err != nil { + out, err := unmarshalSamplePayload() + if err != nil { t.Fatal(err) } @@ -64,10 +79,8 @@ func TestUnmarshalRelationships(t *testing.T) { } func TestUnmarshalNestedRelationships(t *testing.T) { - in := samplePayload() - out := new(Blog) - - if err := UnmarshalJsonApiPayload(in, out); err != nil { + out, err := unmarshalSamplePayload() + if err != nil { t.Fatal(err) } @@ -84,6 +97,17 @@ func TestUnmarshalNestedRelationships(t *testing.T) { } } +func unmarshalSamplePayload() (*Blog, error) { + in := samplePayload() + out := new(Blog) + + if err := UnmarshalJsonApiPayload(in, out); err != nil { + return nil, err + } + + return out, nil +} + func samplePayload() io.Reader { payload := &JsonApiOnePayload{ Data: &JsonApiNode{ diff --git a/response.go b/response.go index 9b3dfc4..4f9ad60 100644 --- a/response.go +++ b/response.go @@ -84,6 +84,11 @@ func visitModelNode(model interface{}) (*JsonApiNode, []*JsonApiNode, error) { args := strings.Split(tag, ",") + if len(args) != 2 { + er = errors.New(fmt.Sprintf("jsonapi tag, on %s, had two few arguments", structField.Name)) + return false + } + if len(args) >= 1 && args[0] != "" { annotation := args[0] diff --git a/response_test.go b/response_test.go index 504026e..7b69839 100644 --- a/response_test.go +++ b/response_test.go @@ -5,6 +5,7 @@ import ( "encoding/json" "fmt" "reflect" + "regexp" "testing" "time" ) @@ -40,6 +41,21 @@ func (b Blogs) GetData() []interface{} { return d } +func TestMalformedTagResposne(t *testing.T) { + testModel := &BadModel{} + _, err := MarshalJsonApiOnePayload(testModel) + + if err == nil { + t.Fatalf("Did not error out with wrong number of arguments in tag") + } + + r := regexp.MustCompile(`two few arguments`) + + if !r.Match([]byte(err.Error())) { + t.Fatalf("The err was not due two two few arguments in a tag") + } +} + func TestHasPrimaryAnnotation(t *testing.T) { testModel := &Blog{ Id: 5,