wvteijlingen / Spine

A Swift library for working with JSON:API APIs. It supports mapping to custom model classes, fetching, advanced querying, linking and persisting.
MIT License
266 stars 109 forks source link

Querying nested relationships doesn't use relationships link URL #178

Open bparise opened 7 years ago

bparise commented 7 years ago

I am trying to traverse nested relationships and when Query with resourceCollection, the URL being generated is not the one as defined in the relationship:

{
  "data": [
    {
      "type": "universities",
      "id": "1",
      "attributes": {
        "name": "Test University"
      },
      "relationships": {
        "organizations": {
          "data": [
            {
              "type": "organizations",
              "id": "1"
            },
            {
              "type": "organizations",
              "id": "2"
            }
          ],
          "links": {
            "self": "/universities/1/relationships/organizations"
          }
        }
      }
    }
  ]
}
func testNested() {
  spine.findAll(University.self).onSuccess { collection, meta, jsonapi in
      for university in collection.resources as! [University] {

          // This attempts to load GET /organizations (which is wrong)
          let query = Query(resourceType: Organization.self, resourceCollection: university.organizations!)

          // This attempts to load GET /universities/1/relationships/organizations (which is right)
          let query = Query(resourceType: Organization.self, path: (university.organizations?.linkURL?.relativeString)!)
      }
  }
}