Skip to content

Commit

Permalink
Merge pull request #155 from manyminds/issue-154
Browse files Browse the repository at this point in the history
Fix renaming bug that ocured while unmarshaling
  • Loading branch information
sharpner committed Aug 16, 2015
2 parents cf7c978 + 879813a commit 6ef60de
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 3 deletions.
13 changes: 11 additions & 2 deletions api_interfaces_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,9 @@ import (
)

type SomeData struct {
ID string `json:"-"`
Data string
ID string `json:"-"`
Data string
CustomerID string `jsonapi:"name=customerId"`
}

func (s SomeData) GetID() string {
Expand Down Expand Up @@ -103,6 +104,14 @@ var _ = Describe("Test interface api type casting", func() {
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusOK))
})

It("Post works with lowercase renaming", func() {
reqBody := strings.NewReader(`{"data": [{"attributes":{"customerId": "2" }, "type": "someDatas"}]}`)
req, err := http.NewRequest("POST", "/v1/someDatas", reqBody)
Expect(err).To(BeNil())
api.Handler().ServeHTTP(rec, req)
Expect(rec.Code).To(Equal(http.StatusCreated))
})
})

var _ = Describe("Test return code behavior", func() {
Expand Down
2 changes: 1 addition & 1 deletion jsonapi/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ func UnmarshalInto(input map[string]interface{}, targetStructType reflect.Type,
for x := 0; x < val.NumField(); x++ {
tfield := val.Type().Field(x)
name := GetTagValueByName(tfield, "name")
if name == strings.ToLower(fieldName) {
if strings.ToLower(name) == strings.ToLower(fieldName) {
field = val.Field(x)
}
}
Expand Down

0 comments on commit 6ef60de

Please sign in to comment.