5

The question is about JSON API specification and how properly do a request (I'm using ruby on rails and the json api resources gem but that's a general question anyway, I know how to implement it, I just want to follow the rules of JSON API at: http://jsonapi.org/format/)

Situation 1:

  • I want to get all shelves
  • I want to include all books that are on those shelves
  • The get I'm supposed to use in this case is: www.library.com/shelves?include=books

Situation 2:

  • I want to get all books but only books that are marked as unread

  • The get I'm supposed to use is: www.library.com/books?filter[unread]=true

What would be correct way of designing request for all shelves with included unread books?

Can't figure this one out

www.library.com/shelves?include=books&filter[books.unread]=true ?

www.library.com/shelves?include=unread_books ? <- would have to specify another resource, books that are unread

www.library.com/shelves?filter[books.unread]=true ?

What's the most correct way of doing this?

EDIT

After speaking with my tech lead and a few other programmers, the first options is favoured the most in such cases

beniutek
  • 1,672
  • 15
  • 32
  • Poeditor accepts array (which looks like this: ["option1", "option2"]) as their parameter: https://poeditor.com/api_reference/#export. So in your case it could looks like filter=["unread", "available"]. – Andrew Oct 23 '16 at 16:38
  • @jereksel sorry but I can't see how this applies to situation when you want to filter included resource. I don't want to filter by additional conditions/params in this case – beniutek Oct 24 '16 at 05:39

2 Answers2

7

I would bet on the first one:

www.library.com/shelves?include=books&filter[books.unread]=true
brian
  • 509
  • 3
  • 14
  • Yeah kinda looks ok, I guess it's more opinion that one straightforward answer. Do you have any resources to back your answer with? Would love to read up more on that topic – beniutek Oct 24 '16 at 05:40
  • yeah, was speaking with other devs about that and this was the most favoured option to follow – beniutek Oct 24 '16 at 09:46
2

JSON API currently does not support filtering includes, but this doesn't mean you have to be strict on the definition (check https://github.com/cerebris/jsonapi-resources/issues/314)

I would go with the same approach as brian: www.library.com/shelves?include=books&filter[books.unread]=true

I just wanted to give some more background to the answer.

  • im currently developing filtering on included resources because of that see https://github.com/beniutek/jsonapi-resources currently it's more or less working although I'm struggling with deeply nested resources and caching. When thats done and PR will be accepted for the official gem ill let you guys know in this topic – beniutek Nov 24 '16 at 17:33
  • it's an old thread but if anyone is interested, filtering of included resources is merged into the jsonapi-resources gem https://github.com/cerebris/jsonapi-resources/pull/1183 (not my work though :) ) – beniutek Apr 18 '19 at 17:50