trailblazer / roar-jsonapi

JSON API support for Roar.
http://trailblazer.to/gems/roar/jsonapi.html
MIT License
42 stars 18 forks source link

Collections `to_json(included: 'xxx')` doesn't output included records #30

Open fran-worley opened 7 years ago

fran-worley commented 7 years ago

When I call to_json() on a representer from collection without specifying included records I get the full compound document complete with the included records.

"{\"data\":[
  {\"relationships\":{
    \"feature-image\":{\"data\":{\"id\":\"1\",\"type\":\"images\"}},
    \"images\":{\"data\":[{\"id\":\"1\",\"type\":\"images\"}]},
    \"documents\":{\"data\":[]},
  },
  \"id\":\"1\",
  \"attributes\":{
    \"name\":\"Site Name 1\",
    \"reference\":\"760\",
    \"address\":{
      \"address_line_one\":\"Addr 1\",
      \"town_city\":\"Town\",
      \"county\":\"County\",
      \"postcode\":\"Some Postcode\",
      \"country\":\"England\"
    }
  },\"type\":\"sites\"},
  {\"relationships\":{
    \"feature-image\":{\"data\":{\"id\":\"2\",\"type\":\"images\"}},
    \"images\":{\"data\":[{\"id\":\"2\",\"type\":\"images\"}]},
    \"documents\":{\"data\":[]},
  },
  \"id\":\"2\",\"attributes\":{
    \"name\":\"Site Name 2\",
    \"reference\":\"114\",
    \"address\":{
      \"address_line_one\":\"Addr 1\",
      \"town_city\":\"Town\",
      \"county\":\"County\",
      \"postcode\":\"Some Postcode\",
      \"country\":\"England\"
    }
  },\"type\":\"sites\"}
],
\"included\":[
  {\"id\":\"1\",\"attributes\":{\"caption\":\"Exterior view of building\",\"file-url\":\"some_url\"},\"type\":\"images\",\"links\":{\"self\":\"http://images/1\"}},
  {\"id\":\"2\",\"attributes\":{\"caption\":\"Interior view of building\",\"file-url\":\"some_url\"},\"type\":\"images\",\"links\":{\"self\":\"http://images/2\"}}
]}"

When I pass in an included option, all relationship attributes are rendered, but nothing is included at all.

"{\"data\":[
  {\"relationships\":{
    \"feature-image\":{\"data\":{\"id\":\"1\",\"type\":\"images\"}},
    \"images\":{\"data\":[{\"id\":\"1\",\"type\":\"images\"}]},
    \"documents\":{\"data\":[]},
  },
  \"id\":\"1\",
  \"attributes\":{
    \"name\":\"Site Name 1\",
    \"reference\":\"760\",
    \"address\":{
      \"address_line_one\":\"Addr 1\",
      \"town_city\":\"Town\",
      \"county\":\"County\",
      \"postcode\":\"Some Postcode\",
      \"country\":\"England\"
    }
  },\"type\":\"sites\"},
  {\"relationships\":{
    \"feature-image\":{\"data\":{\"id\":\"2\",\"type\":\"images\"}},
    \"images\":{\"data\":[{\"id\":\"2\",\"type\":\"images\"}]},
    \"documents\":{\"data\":[]},
  },
  \"id\":\"2\",\"attributes\":{
    \"name\":\"Site Name 2\",
    \"reference\":\"114\",
    \"address\":{
      \"address_line_one\":\"Addr 1\",
      \"town_city\":\"Town\",
      \"county\":\"County\",
      \"postcode\":\"Some Postcode\",
      \"country\":\"England\"
    }
  },\"type\":\"sites\"}
]}"

When doing the same thing for a single representer I get the expected behaviour so I assume this issue is restricted to collections

myabc commented 7 years ago

Hi @fran-worley. Can you post the exact arguments? Note also that the option is include not included:

http://www.rubydoc.info/gems/roar-jsonapi/Roar%2FJSON%2FJSONAPI%2FDocument:to_hash

fran-worley commented 7 years ago

Yup, that was poor github issue writing on my part, my render call is pretty simple:

# r.params["include"] == 'feature-image'
 sites = Site.all
 representer = Site::FullRepresenter.for_collection.new(sites)
 response.write(representer.to_json(include: r.params["include"]))
myabc commented 7 years ago

@fran-worley if you're able to provide a reproduction (or at least the representers), that would help. Otherwise I'll try to reproduce this on the weekend.

fran-worley commented 7 years ago

Is this enough info? https://gist.github.com/fran-worley/39451042e02244d7efa9af3d9f952d58

myabc commented 7 years ago

@fran-worley I believe this is an issue with hyphenation: feature-image relationship name vs. feature_image method name, although I'm surprised it isn't affecting single representers as well.

I would suspect that if you were to rename feature-image to feature then it would work.

In any case, this is definitely a bug.

fran-worley commented 7 years ago

Hadn't thought of that, I'll run the same test with a non-hyphenated name and see what happens!

fran-worley commented 7 years ago

I've got the same issue when passing in a fields option into to_json. I've checked both dasherised and underscored names and it makes no difference.

rohitpaulk commented 2 years ago

Can confirm that relationships passed in via include: don't work if they are hyphenated (like it would be for a JSONAPI request). We've got a "users" resource that has a "team-memberships" relationship on it. Passing in include=team_memberships works, but include=team-memberships doesn't.

I'll see if I can pin this down and send a PR, here's the workaround we're using for now:

normalized_include = (params[:include] || "").split(",").map(&:underscore)
render json: UserRepresenter.for_collection.prepare(users).to_json(include: normalized_include)
apotonick commented 2 years ago

Hi @rohitpaulk :beers: thanks for posting your fix! The maintainer of this gem is presently lost without a trace, once he's back we might revisit this gem!