rabl icon indicating copy to clipboard operation
rabl copied to clipboard

config.include_child_root has no effect on #collection

Open machisuji opened this issue 11 years ago • 2 comments

I'm not sure if this is a bug or working as intended, but in any case it doesn't work as I would expect it to work.

I've created a little test project here.

First the rabl code:

collection @posts

attributes :id, :title, :body, :published

And the configuration:

Rabl.configure do |config|
  config.include_json_root = true
  config.include_child_root = false
end

In this configuration I observe the following behaviour:

curl -s localhost:3000/posts.json && echo
[{"post":{"id":1,"title":"Wurst","body":"foobar","published":true}},{"post":{"id":2,"title":"Quick","body":"Quack","published":false}}]

What I'd expect is the following, though:

[{"id":1,"title":"Wurst","body":"foobar","published":true},{"id":2,"title":"Quick","body":"Quack","published":false}]

Actually I'd also expect a root element "posts", but that is (I believe) another bug.

Could you please clarify what the intended behaviour is? If I'm right I could go ahead and fix it. I already have written a monkey patch and it wouldn't be far from there to a pull request with a proper fix.

P.S. It does work as expected with #child, just not with #collection.

machisuji avatar Dec 13 '13 13:12 machisuji

If you set include_json_root to false does it work as expected?

— Nathan Esquenazi CodePath Co-founder http://thecodepath.com

On Sat, Dec 14, 2013 at 12:36 AM, Markus Kahl [email protected] wrote:

I'm not sure if this is a bug or working as intended, but in any case it doesn't work as I would expect it to work. I've created a little test project here. In this configuration I observe the following behaviour: curl -s localhost:3000/posts.json && echo [{"post":{"id":1,"title":"Wurst","body":"foobar","published":true}},{"post":{"id":2,"title":"Quick","body":"Quack","published":false}}] What I'd expect is the following, though: [{"id":1,"title":"Wurst","body":"foobar","published":true},{"id":2,"title":"Quick","body":"Quack","published":false}] Actually I'd also expect a root element "posts", but that is (I believe) another bug.

Could you please clarify what the intended behaviour is? If I'm right I could go ahead and fix it. I already have written a monkey patch and it wouldn't be far from there to a pull request with a proper fix.

Reply to this email directly or view it on GitHub: https://github.com/nesquena/rabl/issues/513

nesquena avatar Dec 13 '13 13:12 nesquena

Yes, it does:

curl -s localhost:3000/posts.json && echo
[{"id":1,"title":"Wurst","body":"foobar","published":true},{"id":2,"title":"Quick","body":"Quack","published":false}]

The thing is, that I do want roots everywhere except in collections. I.e. whenever there is an array of things there shouldn't be roots there. I thought that's what 'config.include_child_root' was for. It does work for the #child call, but I would expect it to extend to #collection as well.

Is there a reason for it not to?

machisuji avatar Dec 13 '13 14:12 machisuji