Skip to content

Commit

Permalink
Fix node id marshalling for custom string types
Browse files Browse the repository at this point in the history
  • Loading branch information
hypnoglow committed Apr 27, 2018
1 parent 2dcc18f commit d11604b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion response.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func visitModelNode(model interface{}, included *map[string]*Node,
// Handle allowed types
switch kind {
case reflect.String:
node.ID = v.Interface().(string)
node.ID = v.String()
case reflect.Int:
node.ID = strconv.FormatInt(int64(v.Interface().(int)), 10)
case reflect.Int8:
Expand Down
32 changes: 32 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,38 @@ func TestMarshalIDPtr(t *testing.T) {
}
}

func TestMarshalIDTypeOfString(t *testing.T) {
type (
IBSN string

Book struct {
ID IBSN `jsonapi:"primary,books"`
}
)

book := &Book{ID: IBSN("978-3-16-148410-0")}

out := &bytes.Buffer{}
if err := MarshalPayload(out, book); err != nil {
t.Fatal(err)
}

var payload map[string]interface{}
if err := json.Unmarshal(out.Bytes(), &payload); err != nil {
t.Fatal(err)
}

data := payload["data"].(map[string]interface{})
id, ok := data["id"]
if !ok {
t.Fatal("Was expecting the data.id value to exist")
}

if id != "978-3-16-148410-0" {
t.Fatalf("Was expecting the data.id value to be %s but got %s", "978-3-16-148410-0", id)
}
}

func TestMarshalOnePayload_omitIDString(t *testing.T) {
type Foo struct {
ID string `jsonapi:"primary,foo"`
Expand Down

0 comments on commit d11604b

Please sign in to comment.