Ember JSONApi adapter not working as I'd expect


#1

Ok so i am loading in the following JSON into my adapter:

{
  "links": {
    "up": "https://localhost:44314/api",
    "self": "https://localhost:44314/api/jobs"
  },
  "data": [
    {
      "type": "jobs",
      "id": "cec9da3d-7a87-46e8-9a86-0941d89e426a",
      "attributes": {
        "contact-name": "asdasda",
        "contact-trade-name": "asdasd",
        "contact-phone": "asdasd",
        "contact-email": "asdasd@asd.asd",
        "is-accepting-card-payments": false,
        "is-pos": false,
        "created-date": "2016-12-20T08:11:47.773",
        "last-modified-date": "2016-12-20T09:36:39.033",
        "is-deleted": false,
        "status": "AwaitingBillData",
        "owner-user": null,
        "share-user": null,
        "is-shared": true
      },
      "relationships": {
        "owner-user": {
          "links": {
            "related": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a/owner-user",
            "self": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a/relationships/owner-user"
          }
        },
        "share-user": {
          "links": {
            "related": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a/share-user",
            "self": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a/relationships/share-user"
          }
        }
      },
      "links": {
        "self": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a"
      }
    },
    {
      "type": "jobs",
      "id": "8d922678-bd4c-4199-8260-ef063b4b070a",
      "attributes": {
        "contact-name": "ppp",
        "contact-trade-name": "ppp",
        "contact-phone": "ppp",
        "contact-email": "ppp@asdasd.asd",
        "is-accepting-card-payments": false,
        "is-pos": false,
        "created-date": "2016-12-20T08:11:28.94",
        "last-modified-date": "2016-12-20T09:36:44.77",
        "is-deleted": false,
        "status": "AwaitingBillData",
        "owner-user": null,
        "share-user": null,
        "is-shared": true
      },
      "relationships": {
        "owner-user": {
          "links": {
            "related": "https://localhost:44314/api/jobs/8d922678-bd4c-4199-8260-ef063b4b070a/owner-user",
            "self": "https://localhost:44314/api/jobs/8d922678-bd4c-4199-8260-ef063b4b070a/relationships/owner-user"
          }
        },
        "share-user": {
          "links": {
            "related": "https://localhost:44314/api/jobs/8d922678-bd4c-4199-8260-ef063b4b070a/share-user",
            "self": "https://localhost:44314/api/jobs/8d922678-bd4c-4199-8260-ef063b4b070a/relationships/share-user"
          }
        }
      },
      "links": {
        "self": "https://localhost:44314/api/jobs/8d922678-bd4c-4199-8260-ef063b4b070a"
      }
    }
  ]
}

And that works fine - however when I try to access shareUser on one of the jobs that are loaded into the model - it goes to the server using something like https://localhost:44314/api/jobs/8d922678-bd4c-4199-8260-ef063b4b070a/share-user and i can see that a user is being returned but ember always says it’s null - when I try to {{log job.shareUser}} in a template say, it’s always got not content. Even though calling .shareUser on the job model creates a call to the server that responds with:

{
  "links": {
    "up": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a",
    "self": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a/share-user"
  },
  "data": [
    {
      "type": "users",
      "id": "a75cc44b-6471-4b0d-8f92-f14ae6e42086",
      "attributes": {
        "name": "Bob",
        "email": "bob@signumcorporate.com",
        "phone-number": "+44 00000000",
        "ids-id": 2,
        "is-deleted": false,
        "shared-jobs": null,
        "owned-jobs": null
      }
    }
  ]
}

i wouold expect ember to tie these together. I have async: true on the belongs-to relationship in my model definition.


#2

My quick guess is that it’s expecting "data" to be a single resource (this is a belongsTo not a hasMany, correct?) rather than an array resource. So, perhaps, what you’ll want is the server to return:

{
  "links": {
    "up": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a",
    "self": "https://localhost:44314/api/jobs/cec9da3d-7a87-46e8-9a86-0941d89e426a/share-user"
  },
  "data": {
    "type": "users",
    "id": "a75cc44b-6471-4b0d-8f92-f14ae6e42086",
    "attributes": {
      "name": "Bob",
      "email": "bob@signumcorporate.com",
      "phone-number": "+44 00000000",
      "ids-id": 2,
      "is-deleted": false,
      "shared-jobs": null,
      "owned-jobs": null
    }
  }
}

If you’re not able to change that resource, then you may just need to override your Serializer’s normalizeFindBelongsToResponse hook to pull that object out of the array.