Let’s consider a valid and supported request to include related resources, for example, GET /posts?include=comments. Now, if we consider the case where no post has comments, what is the best (or the recommended) approach:
I did not find a clear answer by checking the specification, the forum, and the JSON Schema, so I created this post.
I believe option (1) to be the best. Also, option (3) seems to be the worst because the client may be lead into believe that the “include” query was ignored.
I think that an empty array for included when inclusion is explicit in the request is more predictable and more friendly to the consumer of the API. Omission of the included keyword when requested may generate confusion.
In fact, while JSON:API 1.0 allows the included member to be omitted if the list is empty, JSON:API 1.1 specifically requires it to be present if an include query parameter is sent.
So returning an empty list for included is the best approach if include was specified and there’s nothing to include.