google / jsonapi

jsonapi.org style payload serializer and deserializer
http://godoc.org/github.com/google/jsonapi
MIT License
1.42k stars 211 forks source link

Empty relation atrributes #210

Closed deividaspetraitis closed 3 years ago

deividaspetraitis commented 3 years ago

Hello,

Probably some silly mistake, but can't figure out why UnmarshalPayload and UnmarshalManyPayload does not fill up relation atrributes but ID`s:

2009/11/10 23:00:00 author posts &{ID:456 Title:Foo Text:bar baz boo!}
2009/11/10 23:00:00 customer equipment &{ID:18706 SerialNumber:}
2009/11/10 23:00:00 customer equipment &{ID:18706 SerialNumber:}

Program:

package main

import (
    "bytes"
    "log"
    "reflect"

    "github.com/google/jsonapi"
)

type Customer struct {
    ID        string       `jsonapi:"primary,customers"`
    FirstName string       `jsonapi:"attr,first_name"`
    LastName  string       `jsonapi:"attr,last_name"`
    Equipment []*Equipment `jsonapi:"relation,equipment"`
}

type Equipment struct {
    ID           string `jsonapi:"primary,equipment"`
    SerialNumber string `jsonapi:"attr,serial_number"`
}

type Author struct {
    ID        string  `jsonapi:"primary,authors"`
    FirstName string  `jsonapi:"attr,first_name"`
    LastName  string  `jsonapi:"attr,last_name"`
    Posts     []*Post `jsonapi:"relation,posts"`
}

type Post struct {
    ID    string `jsonapi:"primary,posts"`
    Title string `jsonapi:"attr,title"`
    Text  string `jsonapi:"attr,text"`
}

func main() {
    UnmarshalTmp()
    UnmarshalPayload()
    UnmarshalManyPayload()
}

const payloadOne = `{
    "data": {
        "id": "225971",
        "type": "customers",
        "attributes": {
            "first_name": "First",
            "last_name": "Last"
        },
        "relationships": {
            "equipment": {
                "data": [{
                        "id": "18706",
                        "type": "equipment"
                }]
            },
            "included": [{
                "type": "equipment",
                "id": "18706",
                "attributes": {
                    "serial_number": "1234"
                }
            }]
        }
    }
}`

const payloadMany = `{
    "data": [{
        "type": "customers",
        "id": "225971",
        "attributes": {
            "first_name": "First",
            "last_name": "Last"
        },
        "relationships": {
            "equipment": {
                "data": [{
                        "type": "equipment",
                        "id": "18706"
                    }

                ]
            },
            "included": [{
                "type": "equipment",
                "id": "18706",
                "attributes": {
                    "serial_number": "1234"
                }
            }]
        }
    }]
}`

const payloadTmp = `{
    "data": {
        "id": "123",
        "type": "authors",
        "attributes": {
            "first_name": "John",
            "last_name": "Doe"
        },
        "relationships": {
            "posts": {
                "data": [{
                    "id": "456",
                    "type": "posts"
                }],
                "links": {
                    "self": "http://example.com/api/authors/123/posts",
                    "related": "http://example.com/api/authors/123/relationships/posts"
                }
            }
        },
        "links": {
            "self": "http://example.com/api/authors/123"
        },
        "meta": {
            "includes": ["posts"],
            "duration": 600
        }
    },
    "included": [{
        "id": "456",
        "type": "posts",
        "attributes": {
            "title": "Foo",
            "text": "bar baz boo!"
        }
    }]
}`

func UnmarshalPayload() {
    reader := bytes.NewReader([]byte(payloadOne))

    var customer Customer
    jsonapi.UnmarshalPayload(reader, &customer)

    log.Printf("customer equipment %+v\n", customer.Equipment[0])
}

func UnmarshalTmp() {
    reader := bytes.NewReader([]byte(payloadTmp))

    var author Author
    jsonapi.UnmarshalPayload(reader, &author)

    log.Printf("author posts %+v\n", author.Posts[0])
}

func UnmarshalManyPayload() {
    reader := bytes.NewReader([]byte(payloadMany))

    customers, _ := jsonapi.UnmarshalManyPayload(reader, reflect.TypeOf(new(Customer)))
    for _, customer := range customers {
        log.Printf("customer equipment %+v\n", customer.(*Customer).Equipment[0])
    }
}

Playground: https://play.golang.org/p/eLomvUvrU-q

deividaspetraitis commented 3 years ago

"included" must appear as top level document element.