# What if a sparse fieldset request excludes a field that was requested for inclusion?

**URL:** <https://discuss.jsonapi.org/t/what-if-a-sparse-fieldset-request-excludes-a-field-that-was-requested-for-inclusion/1031>\
**Category:** Uncategorized\
**Created:** [July 17, 2017, 11:22am UTC](https://discuss.jsonapi.org/t/what-if-a-sparse-fieldset-request-excludes-a-field-that-was-requested-for-inclusion/1031 "2017-07-17T11:22:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![OliverSalzburg](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/oliversalzburg/32/416_2.png) [@OliverSalzburg](https://discuss.jsonapi.org/u/OliverSalzburg)\
**Post date:** [July 17, 2017, 11:22am UTC](https://discuss.jsonapi.org/t/what-if-a-sparse-fieldset-request-excludes-a-field-that-was-requested-for-inclusion/1031/1 "2017-07-17T11:22:10Z")

</div>

Consider a request that supplies _both_ a request for **inclusion** of a related record `include=author` _and_ **excludes** the same field through a sparse fieldset `fields[post]=title`.

How should one respond to such a query? We currently reject the request with a status of 400, but I would like to hear how others see it.

---

<div class="post-metadata">

**Author:** ![michaelhibay](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/michaelhibay/32/405_2.png) [@michaelhibay](https://discuss.jsonapi.org/u/michaelhibay)\
**Post date:** [July 17, 2017, 3:39pm UTC](https://discuss.jsonapi.org/t/what-if-a-sparse-fieldset-request-excludes-a-field-that-was-requested-for-inclusion/1031/2 "2017-07-17T15:39:08Z")

</div>

[Sparse fieldsets](http://jsonapi.org/format/#fetching-sparse-fieldsets) are constrained by their TYPE.

> A client MAY request that an endpoint return only specific fields in the response on a per-type basis by including a fields[TYPE] parameter.

As such, the inclusion of fields[post]=title does not constitute a contradiction as sparse fieldsets only perform data shaping upon items which would have made it into the document after content processing including filter and ‘includes’ is completed.

Therefor the only contradiction possible would be if the fieldsets were declared multiple times, in which case you could reject with 400, or serve the request with the aggregated fieldset.

HOWEVER, it looks like you’re using includes to perform sparse fieldsets, or at least think it has that capability, where it is strictly about defining which related **_resources_** not attributes are to be added in the ‘includes’ section of the TLD.

Includes is for _document_ shaping and eliminating extraneous calls, sparse fieldsets are for performing _data_ shaping upon those returned types.

---

<div class="post-metadata">

**Author:** ![OliverSalzburg](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/oliversalzburg/32/416_2.png) [@OliverSalzburg](https://discuss.jsonapi.org/u/OliverSalzburg)\
**Post date:** [July 17, 2017, 4:05pm UTC](https://discuss.jsonapi.org/t/what-if-a-sparse-fieldset-request-excludes-a-field-that-was-requested-for-inclusion/1031/3 "2017-07-17T16:05:43Z")

</div>

Maybe my example wasn’t ideal. Let me elaborate.

I’m assuming `post` has a relationship to a person, the `author`. It also has a attribute `title`, which is a string.

It is my understanding that sparse fieldsets have an impact on both relationships and attributes. Thus, a sparse fieldset definition may exclude information that is relevant to the consistency of the relationship.  
So my example was trying to say “Give me only the title of the post, but include the author of the post in the document”.

You say that the sparse fieldset is only intended to shape the final resulting document. If that is the case, then I agree, there is no contradiction. However, I did not interpret it that way and implemented it so that the field selection is the basis for storage queries. So I only retrieve the fields that I will use in the resulting document. This way, I’m now possibly missing information which I would require to include the requested resources. Possibly a mistake.

Additionally, if I were to _include_ the referenced records, but strip away the relationship information to satisfy the sparse fieldset request, would that not hurt the full-linkage goal?

Thanks for the feedback 🙂

---

<div class="post-metadata">

**Author:** ![michaelhibay](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/michaelhibay/32/405_2.png) [@michaelhibay](https://discuss.jsonapi.org/u/michaelhibay)\
**Post date:** [July 17, 2017, 6:07pm UTC](https://discuss.jsonapi.org/t/what-if-a-sparse-fieldset-request-excludes-a-field-that-was-requested-for-inclusion/1031/4 "2017-07-17T18:07:59Z")

</div>

I believe the issue boils down to how to present the type as requested in the fields parameter, while maintaining full linkage.

The primary data would of course always include everything, unless sparse fieldsets is used to reduce it. However, full linkage must be maintained so the appropriate resource objects with nothing more than type, id, and links to build the chain from primary data to the included resource.

The way I view include in this context is essentially a pre-fetch operation to batch the request for related resources without the need to individually request the items through relationship identifiers. It essentially is performing multiple requests in one. Sparse fieldsets then takes the document and reduces the result to the requested resources + any required objects for full linkage.

I have also had the urge to use sparse fieldsets to reduce the load on storage, so my advice would be to use the approach of returning minimum linkage + whatever is requested in the case of conflicting statements. In this way you would be able to retrieve all the keys required for full linkage, without including any of the unrequested data. This would still satisfy full linkage, as well the sparse fieldsets request.

Using the example you listed should yield something like this.

```
GET http://www.example.com/post/1?include=author&fields[post]=title

{
  "type": "post",
  "id": "1",
  "attributes": {
    "title": "It's a title."
  },
  "relationships": {
    "author": {
      "links": {
        "self": "http://example.com/articles/1/relationships/author",
        "related": "http://example.com/people?filter[article]=1"
      },
      "data":{
        "type":"people", "id":"9"
      }
    }
  },
  "included":[
    {
      "type":"people",
      "id": "9",
      "attributes": {
        "first-name": "Dan",
        "last-name": "Gebhardt",
        "twitter": "dgeb"
      },
      "links": {
        "self": "http://example.com/people/9"
      }
    }
  ],
  "links": {
    "self": "http://example.com/articles/1"
  }
}

```

Include is used to declare how far out on a particular relationship chain you would like included in this request, for convenience in this case. Sparse fieldsets is used to reduce the data sent for any of those types to the bare minimum of requested items + linkage.

---

<div class="post-metadata">

**Author:** ![OliverSalzburg](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/oliversalzburg/32/416_2.png) [@OliverSalzburg](https://discuss.jsonapi.org/u/OliverSalzburg)\
**Post date:** [July 17, 2017, 6:22pm UTC](https://discuss.jsonapi.org/t/what-if-a-sparse-fieldset-request-excludes-a-field-that-was-requested-for-inclusion/1031/5 "2017-07-17T18:22:54Z")

</div>

Awesome. Excellent help, as always. Thank you very much 🙂

---

<div class="post-metadata">

**Author:** ![michaelhibay](https://sea2.discourse-cdn.com/flex016/user_avatar/discuss.jsonapi.org/michaelhibay/32/405_2.png) [@michaelhibay](https://discuss.jsonapi.org/u/michaelhibay)\
**Post date:** [July 19, 2017, 4:53pm UTC](https://discuss.jsonapi.org/t/what-if-a-sparse-fieldset-request-excludes-a-field-that-was-requested-for-inclusion/1031/6 "2017-07-19T16:53:37Z")

</div>

It looks like I missed a point in the spec requiring full linkage to be maintained _ **except** _ in the case of sparse fieldsets. The rest of my post still applies, but I would amend it to say that querying would require the minimum of full linkage + sparse fieldset requested content, however the returned document itself does not require full linkage in this case. In that way you are free to include or exclude the linking minimal resource objects when building the complete document for the consumer.
