vt-elixir / ja_serializer

JSONAPI.org Serialization in Elixir.
Other
640 stars 148 forks source link

Sparse fields should consider relationships as field #240

Closed rbao closed 5 years ago

rbao commented 7 years ago

Currently with the following code:

has_many :comments, serializer: MyApp.CommentView, include: false, identifiers: :when_included

And requesting with sparse fields: http://localhost:4000/posts?fields[posts]=name the resulting response has attributes correctly filtered but has the following additional fields:

# ...
"relationships": {
   "comments": {}
}
# ...

And when using identifiers: :always the response also have the following additional fields:

# ...
"relationships": {
  "comments": {
    "data": [
      {
        "type": "S3FileSet",
        "id": "33768c8a-a7e7-448e-ad2c-4279228b5bf4"
      }
  }
}
# ...

I think for both the above cases the response should respect the sparse fields, and NOT include the comments or even better to just omit the entire relationships object, since the client is only requesting the name fields which is an attribute and in the JSON API spec it states:

If a client requests a restricted set of fields for a given resource type, an endpoint MUST NOT include additional fields in resource objects of that type in its response.

The definition for fields in the JSON API spec include both attributes and relationships:

A resource object’s attributes and its relationships are collectively called its “fields”.

but right now only attributes are being filtered, relationships are not

zachdaniel commented 7 years ago

We are seeing this same behavior and will need to address this as well.