JsonApiDotNetCore: Relationship Data - missing or by design?

Should I always see relationship data, regardless of link config or include query?

By this, I mean /items currently produces:

{
    "data": [
        {
            "type": "items",
            "id": "item-de4e78d3-b2ec-48dd-8dd7-6088881a2bfe",
            "attributes": {
                "type": 0,
                "title": "New York",
                "cover": null,
                "slug": null,
                "version": 1
            },
            "relationships": {
                "owner": {
                    "links": {
                        "self": "/items/item-de4e78d3-b2ec-48dd-8dd7-6088881a2bfe/relationships/owner",
                        "related": "/items/item-de4e78d3-b2ec-48dd-8dd7-6088881a2bfe/owner"
                    },
                "createdBy": {},
                "updatedBy": {},
                "deletedBy": {}
                }
            }
        }
    ]
}

If I include owner (/items?include=owner):

{
    "data": [
        {
            "type": "items",
            "id": "item-de4e78d3-b2ec-48dd-8dd7-6088881a2bfe",
            "attributes": {
                "type": 0,
                "title": "New York",
                "cover": null,
                "slug": null,
                "version": 1
            },
            "relationships": {
                "owner": {
                    "links": {
                        "self": "/items/item-de4e78d3-b2ec-48dd-8dd7-6088881a2bfe/relationships/owner",
                        "related": "/items/item-de4e78d3-b2ec-48dd-8dd7-6088881a2bfe/owner"
                    },
                    "data": {
                        "type": "users",
                        "id": "user-b607e7dc-3a9d-4360-8495-dd37c4c99de8"
                    }
                },
                "createdBy": {},
                "updatedBy": {},
                "deletedBy": {}
            }
        }
    ],
    "included": [
        {
            "type": "users",
            "id": "user-b607e7dc-3a9d-4360-8495-dd37c4c99de8",
            "attributes": {
                "email": "poo@shorthand.com",
                "firstName": "Matthew",
                "lastName": "Gint2y",
                "slug": "slugger",
                "version": 1
            },
            "relationships": {
                "createdBy": {},
                "updatedBy": {},
                "deletedBy": {}
            }
        }
    ]
}

Even when I am not asking for includes (or if links are turned off), I was expecting the relationships to always have data:

"relationships": {
                "owner": {
                    "data": {
                        "type": "users",
                        "id": "user-b607e7dc-3a9d-4360-8495-dd37c4c99de8"
                    }
                },

Is this expected behavior or am I potentially missing something on my model?

      [HasOne("owner")]
      public virtual User Owner { get; set; }

      public string OwnerId { get; set; }

Based on http://jsonapi.org/format/#document-resource-objects and their { article } snippet, it suggests data should be there… I must be missing something somewhere?

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 18 (10 by maintainers)

Commits related to this issue

Most upvoted comments

Hy @jaredcnance.

After setting customerId as optional:

public int? CustomerId { get; set; }

the GET request returns all data relationships. But on the POST Method dotnet quits unexpectedly.

Hy.

I’m using Version 2.1.0 and would like to receive the relationships data object an every GET request. Is there any possibility to get only the { type, id } relationship infos without the included object?

some example:

{
  "data": {
    "type": "photos",
    "attributes": {
      "title": "Ember Hamster",
      "src": "http://example.com/images/productivity.png"
    },
    "relationships": {
      "photographer": {
        "data": { "type": "people", "id": "9" }
      }
    }
  }
}

Ah, yes sorry my mistake. You’re correct.

But isn’t that referring to the “included” section of the document response? This is different from the inclusion of the “data” field in the “relationships” section. Below is an example response showing a response with included data. This response returns the included information requested in the “included” section but still includes the “data” at the relationship level. This would always be present regardless of what included information was requested. image