-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Fix json serialization of nested hash when using root elements with a Grape::Entity presenter #181
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8794387
Correctly serializes nested hash elements when a root element is used…
0ba3a31
Merge branch 'master' of https://github.com/intridea/grape
eb49952
update CHANGELOG with info from pull request 181 (fix JSON serializat…
a1de0e2
Grape::Entity#serializable_hash should call #serializable_hash if the…
goodwink 04149be
Grape::Entity#serializable_hash should call #serializable_hash elemen…
goodwink c050645
Merge branch 'master' of https://github.com/intridea/grape
67250db
cleanup recursive json serialization code per comments from dblock
18419c4
fix changelog info for #181 under wrong section
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -257,6 +257,28 @@ | |
fresh_class.expose :name | ||
expect{ fresh_class.new(nil).serializable_hash }.not_to raise_error | ||
end | ||
|
||
it 'should serialize embedded objects which respond to #serializable_hash' do | ||
class EmbeddedExample | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You should know that these definitions become global in Ruby, and notably pollute other tests. Either use |
||
def serializable_hash(opts = {}) | ||
{:abc => 'def'} | ||
end | ||
end | ||
|
||
class SimpleExample | ||
def name | ||
"abc" | ||
end | ||
|
||
def embedded | ||
EmbeddedExample.new | ||
end | ||
end | ||
|
||
fresh_class.expose :name, :embedded | ||
presenter = fresh_class.new(SimpleExample.new) | ||
presenter.serializable_hash.should == {:name => "abc", :embedded => {:abc => "def"}} | ||
end | ||
end | ||
|
||
describe '#value_for' do | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you explain the comparison here? I am also afraid that
seriazlie_recurse
, which is quite hefty for large JSONs, is going to run for every JSON output, then get thrown out.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that if the objects supports serializable_hash, or is an array or hash of those objects, then we need to build a new array or hash of the results of the serializable_hash calls. Otherwise, we want the results of to_json as our final output. The comparison checks if the unmodified object has been returned, and is an equivalent to the final condition in the previous code. You're right that this is unclear, and I'll refactor it to have everything inside a single serialize_object method or similar.
It is hefty for deeply nested hashes, but how else can we ensure they are properly serialized?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think my biggest problem with this is that we call
serialize_recurse
every time, then compare the object to the result of the serialization. So I am not arguing the purpose, but the implementation has to be unwrapped into something that looks less obscure. Maybe something in this genre?I can take a stab at this if you don't find anything workable.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I agree. Your implementation is much cleaner :) I'll try to find some time in the next day or two to tidy this up.