Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Default nil associations to [] when embedding in root #502

Closed
wants to merge 1 commit into from

4 participants

Joe Fiorini Martin Schürrer Steve Klabnik Dylan Markow
Dylan Markow dmarkow commented on the diff
lib/active_model/serializer.rb
@@ -157,7 +157,7 @@ def embedded_in_root_associations
if hash.has_key?(key)
hash[key].concat(serialized_data).uniq!
Dylan Markow
dmarkow added a note

If you have multiple associations using the same root element, there's a chance you would run into serialized_data being nil here too; it should probably get the same serialized_data || [] treatment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Martin Schürrer

I think that's fixed by 8ca4d4f

Steve Klabnik

Yes, it seems that commit fixes this. Please correct me if I'm wrong.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
2  lib/active_model/serializer.rb
View
@@ -157,7 +157,7 @@ def embedded_in_root_associations
if hash.has_key?(key)
hash[key].concat(serialized_data).uniq!
Dylan Markow
dmarkow added a note

If you have multiple associations using the same root element, there's a chance you would run into serialized_data being nil here too; it should probably get the same serialized_data || [] treatment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
else
- hash[key] = serialized_data
+ hash[key] = serialized_data || []
end
end
end
15 test/unit/active_model/serializer/has_one_test.rb
View
@@ -75,6 +75,21 @@ def profile
}, @user_serializer.as_json)
end
+ def test_associations_embedding_nil_ids_in_root_serialization_using_as_json
+ @association.embed = :ids
+ @association.embed_in_root = true
+ @user.instance_eval do
+ def profile
+ nil
+ end
+ end
+
+ assert_equal({
+ 'user' => { name: 'Name 1', email: 'mail@server.com', 'profile_id' => nil },
+ 'profiles' => []
+ }, @user_serializer.as_json)
+ end
+
def test_associations_embedding_nil_objects_serialization_using_as_json
@association.embed = :objects
@user.instance_eval do
Something went wrong with that request. Please try again.