Skip to content

Commit 6ef60de

Browse files
committed
Merge pull request #155 from manyminds/issue-154
Fix renaming bug that ocured while unmarshaling
2 parents cf7c978 + 879813a commit 6ef60de

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

api_interfaces_test.go

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,9 @@ import (
1313
)
1414

1515
type SomeData struct {
16-
ID string `json:"-"`
17-
Data string
16+
ID string `json:"-"`
17+
Data string
18+
CustomerID string `jsonapi:"name=customerId"`
1819
}
1920

2021
func (s SomeData) GetID() string {
@@ -103,6 +104,14 @@ var _ = Describe("Test interface api type casting", func() {
103104
api.Handler().ServeHTTP(rec, req)
104105
Expect(rec.Code).To(Equal(http.StatusOK))
105106
})
107+
108+
It("Post works with lowercase renaming", func() {
109+
reqBody := strings.NewReader(`{"data": [{"attributes":{"customerId": "2" }, "type": "someDatas"}]}`)
110+
req, err := http.NewRequest("POST", "/v1/someDatas", reqBody)
111+
Expect(err).To(BeNil())
112+
api.Handler().ServeHTTP(rec, req)
113+
Expect(rec.Code).To(Equal(http.StatusCreated))
114+
})
106115
})
107116

108117
var _ = Describe("Test return code behavior", func() {

jsonapi/unmarshal.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ func UnmarshalInto(input map[string]interface{}, targetStructType reflect.Type,
260260
for x := 0; x < val.NumField(); x++ {
261261
tfield := val.Type().Field(x)
262262
name := GetTagValueByName(tfield, "name")
263-
if name == strings.ToLower(fieldName) {
263+
if strings.ToLower(name) == strings.ToLower(fieldName) {
264264
field = val.Field(x)
265265
}
266266
}

0 commit comments

Comments
 (0)