Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use underscored json_root when serializing a collection #1052

Merged
merged 1 commit into from
Aug 17, 2015

Conversation

whatthewhat
Copy link
Contributor

The Issue

Assuming we're using the :json adapter with an activerecord model DataPoint currently we get this when serializing a collection:

GET http://localhost:3000/data_points

=>
{
  "datapoints": [...]
}

Expected key would be data_points.

The behaviour was changed in this commit and there are some great examples in this comment by @bolshakov.

I've included a high level test in test/adapter/json/collection_test.rb to better illustrate the point, changes to this file can be reverted before merging.

Appreciate any feedback, will gladly rewrite this multiple times, etc.

@manic
Copy link

manic commented Aug 14, 2015

+1

2 similar comments
@fernando-faubell
Copy link

+1

@dukex
Copy link

dukex commented Aug 14, 2015

+1

@joaomdmoura
Copy link
Member

I'm willing to merge it but there an open PR that might impact it as well, and will also solve this issue, can you check it out to see if we can close this one in favor of the other?
I''m looking forward to have it! 😄
link: #1029

@whatthewhat
Copy link
Contributor Author

Hey @joaomdmoura thanks for pointing out the PR, I somehow have missed it :)
The solution does seem a lot more complete, but there is a slight issue: it does not touch the json_key, meaning that changing the key_format still leaves us with "datapoints" from my example.

And looking at this comment currently this might be the intention of the PR:

Those should be converted wherever they are in the hash. Anything else:

  • top level schema keys like data, etc
  • values of attributes

should be left alone.

We might want to move the discussion to #1029 or I'll update this PR to use key_format after 1029 is merged. WDYT?

@joaomdmoura
Copy link
Member

@whatthewhat Indeed that was the @jfelchner approach, but I'm also in favor of doing the the to the root as you can see on my comment so it would impact the key_format as well.

I know that is a lot of ppl that needs it, so what I'm doing is, I'm merging this one so ppl can use master, and we can move the discussion to #1029, maybe even pair on it in other to make it faster 😄, it will probably override this implementation.

So I'm merging it for now.

joaomdmoura added a commit that referenced this pull request Aug 17, 2015
Use underscored json_root when serializing a collection
@joaomdmoura joaomdmoura merged commit 6aba260 into rails-api:master Aug 17, 2015
@whatthewhat
Copy link
Contributor Author

@joaomdmoura sounds great 👍

I'll try to take a closer look at #1029 during the week, for example json_key behaviour when key_format is set to :unaltered seems like an interesting problem :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants