Skip to content

Commit 4d248c7

Browse files
velovixwwwdata
authored andcommitted
Add missing error checks to SetToOneReferenceID and SetToManyReferenceIDs calls (#329)
* Add missing error check for dataless relationships * Add missing relationship error checking to api.go
1 parent d4f7fae commit 4d248c7

File tree

4 files changed

+45
-8
lines changed

4 files changed

+45
-8
lines changed

api.go

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,15 +1241,21 @@ func processRelationshipsData(data interface{}, linkName string, target interfac
12411241
return errors.New("target struct must implement interface UnmarshalToOneRelations")
12421242
}
12431243

1244-
target.SetToOneReferenceID(linkName, hasOneID)
1244+
err := target.SetToOneReferenceID(linkName, hasOneID)
1245+
if err != nil {
1246+
return err
1247+
}
12451248
} else if data == nil {
12461249
// this means that a to-one relationship must be deleted
12471250
target, ok := target.(jsonapi.UnmarshalToOneRelations)
12481251
if !ok {
12491252
return errors.New("target struct must implement interface UnmarshalToOneRelations")
12501253
}
12511254

1252-
target.SetToOneReferenceID(linkName, "")
1255+
err := target.SetToOneReferenceID(linkName, "")
1256+
if err != nil {
1257+
return err
1258+
}
12531259
} else {
12541260
hasMany, ok := data.([]interface{})
12551261
if !ok {
@@ -1276,7 +1282,10 @@ func processRelationshipsData(data interface{}, linkName string, target interfac
12761282
hasManyIDs = append(hasManyIDs, dataID)
12771283
}
12781284

1279-
target.SetToManyReferenceIDs(linkName, hasManyIDs)
1285+
err := target.SetToManyReferenceIDs(linkName, hasManyIDs)
1286+
if err != nil {
1287+
return err
1288+
}
12801289
}
12811290

12821291
return nil

jsonapi/fixtures_test.go

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -174,11 +174,14 @@ func (c Post) GetReferences() []Reference {
174174

175175
func (c *Post) SetToOneReferenceID(name, ID string) error {
176176
if name == "author" {
177-
intID, err := strconv.ParseInt(ID, 10, 64)
178-
if err != nil {
179-
return err
177+
// Ignore empty author relationships
178+
if ID != "" {
179+
intID, err := strconv.ParseInt(ID, 10, 64)
180+
if err != nil {
181+
return err
182+
}
183+
c.AuthorID = sql.NullInt64{Valid: true, Int64: intID}
180184
}
181-
c.AuthorID = sql.NullInt64{Valid: true, Int64: intID}
182185

183186
return nil
184187
}

jsonapi/unmarshal.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,10 @@ func setRelationshipIDs(relationships map[string]Relationship, target UnmarshalI
187187
return fmt.Errorf("struct %s does not implement UnmarshalToOneRelations", reflect.TypeOf(target))
188188
}
189189

190-
castedToOne.SetToOneReferenceID(name, "")
190+
err := castedToOne.SetToOneReferenceID(name, "")
191+
if err != nil {
192+
return err
193+
}
191194
continue
192195
}
193196

jsonapi/unmarshal_test.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -480,6 +480,28 @@ var _ = Describe("Unmarshal", func() {
480480
Expect(err).To(HaveOccurred())
481481
Expect(err.Error()).To(Equal("this never works"))
482482
})
483+
484+
datalessPostJSON := []byte(`{
485+
"data": {
486+
"id": "3",
487+
"type": "posts",
488+
"attributes": {
489+
"title": "Test"
490+
},
491+
"relationships": {
492+
"author": {
493+
"data": null
494+
}
495+
}
496+
}
497+
}`)
498+
499+
It("returns an error if SetToOneReferenceID returned an error, even for a relationship without a data field", func() {
500+
post := ErrorRelationshipPosts{}
501+
err := Unmarshal(datalessPostJSON, &post)
502+
Expect(err).To(HaveOccurred())
503+
Expect(err.Error()).To(Equal("this never works"))
504+
})
483505
})
484506

485507
Context("UnmarshalToManyRelations error handling", func() {

0 commit comments

Comments
 (0)