diff --git a/response.go b/response.go index f6a1b86..fe3dbe1 100644 --- a/response.go +++ b/response.go @@ -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: diff --git a/response_test.go b/response_test.go index 157f434..9c20ea2 100644 --- a/response_test.go +++ b/response_test.go @@ -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"`