Skip to content

Omit empty data array in relationships #471

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

Closed
mdsharpe opened this issue Feb 6, 2019 · 2 comments
Closed

Omit empty data array in relationships #471

mdsharpe opened this issue Feb 6, 2019 · 2 comments
Labels

Comments

@mdsharpe
Copy link

mdsharpe commented Feb 6, 2019

Description

I have a requirement to build a JSON API compliant endpoint which returns an entity without its related data. The user would then request the relationship data separately. For example:

Entity Request: v1/myentity/{entity-id}.

Entity Relationship Request: v1/myentity/{entity-id}/items.

My endpoint currently returns this when an entity request is made:

{
  "data": {
    "attributes": {
      "foo": 2,
      "bar": 1
    },
    "relationships": {
      "items": {
        "links": {
          "self": "http://localhost:60000/v1/myentity/1/relationships/items",
          "related": "http://localhost:60000/v1/myentity/1/items"
        },
        "data": []
      }
    },
    "type": "myentity",
    "id": "1"
  }
}

As you can see the data property is an empty array. This is somewhat misleading because there are related items, we have just not included them in this response.

In this section of the JSON:API specification, it shows that it is not required within the relationship section to show the data attribute. It is compliant to only have a links member and no data member.

Is there any way from the controller or via configuration I could omit the array from my response in order to be compliant with the specification and not give the impression the related data is empty?

Environment

  • JsonApiDotNetCore Version: 3.0.0
@jaredcnance
Copy link
Contributor

jaredcnance commented Feb 11, 2019

It actually should omit the relationship.data by default unless the navigation property value is being initialized somewhere. This is one reason it can happen.

@mdsharpe
Copy link
Author

Thanks, resolved by configuring AutoMapper to allow null collections.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Development

No branches or pull requests

2 participants