Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Included relationships of relationships in JSONAPI serialization #132

Open
joanromano opened this issue Oct 6, 2016 · 0 comments
Open
Assignees
Labels
Projects

Comments

@joanromano
Copy link
Member

Given this model:

    struct Dog: JSONAPIEntity {
        let id: String
        let name: String
        let cat: Cat?
    }

    struct Cat: JSONAPIEntity {
        let id: String
        let name: String
    }

    struct User: JSONAPIEntity {
        let id: String
        let name: String
        let dog: Dog
        let cats: [Cat]
    }

And the serialization:

let cats = [Cat(id: "33", name: "Stancho"), Cat(id: "44", name: "Hez")]
let dog = Dog(id: "22", name: "Joan", cat: Cat(id: "55", name: "Max"))
let user = User(id: "11", name: "Alex", dog: dog, cats: cats)
JSONAPISerializer(user, includingChildren: true)

What we get back is the following JSON:

{
  "included" : [
    {
      "id" : "55",
      "type" : "cat",
      "attributes" : {
        "name" : "Max"
      }
    },
    {
      "id" : "33",
      "type" : "cat",
      "attributes" : {
        "name" : "Stancho"
      }
    },
    {
      "id" : "22",
      "type" : "dog",
      "attributes" : {
        "name" : "Joan"
      }
    },
    {
      "id" : "44",
      "type" : "cat",
      "attributes" : {
        "name" : "Hez"
      }
    }
  ],
  "data" : {
    "attributes" : {
      "name" : "Alex"
    },
    "id" : "11",
    "type" : "user",
    "relationships" : {
      "cats" : {
        "data" : [
          {
            "id" : "33",
            "type" : "cat"
          },
          {
            "id" : "44",
            "type" : "cat"
          }
        ]
      },
      "dog" : {
        "data" : {
          "id" : "22",
          "type" : "dog"
        }
      }
    }
  }
}

Where the dog object in included is missing his relationships attributes, so should be like:

{
      "id" : "22",
      "type" : "dog",
      "attributes" : {
        "name" : "Joan"
      },
      "relationships" : {
        "cat" : {
          "data" : {
            "id" : "55",
            "type" : "cat"
          }
        }
      }
}

So this should be fixed on JSONAPISerializable's func includedRelationships(includingChildren: Bool, transformingKeys keyTransformer: KeyTransformer?) -> [Any]?

@joanromano joanromano added the bug label Oct 6, 2016
@joanromano joanromano self-assigned this Oct 18, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

No branches or pull requests

1 participant