From 7c030314cbf10686c8b32279a77b6ca10ca83558 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Wed, 15 Oct 2014 18:56:49 -0500 Subject: [PATCH] Serialize ids as strings in JSON-API adapter --- .../serializer/adapter/json_api.rb | 18 +++++++++++++----- test/adapter/json_api/belongs_to_test.rb | 4 ++-- test/adapter/json_api/collection_test.rb | 4 ++-- test/adapter/json_api/has_many_embed_ids.rb | 2 +- test/adapter/json_api/has_many_test.rb | 6 +++--- 5 files changed, 21 insertions(+), 13 deletions(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 2d4977ea6..71327b3c8 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -14,7 +14,7 @@ def serializable_hash(options = {}) if serializer.respond_to?(:each) @hash[@root] = serializer.map{|s| self.class.new(s).serializable_hash[@root] } else - @hash[@root] = serializer.attributes + @hash[@root] = attributes_for_serializer(serializer, {}) serializer.each_association do |name, association, opts| @hash[@root][:links] ||= {} @@ -35,24 +35,32 @@ def serializable_hash(options = {}) def add_links(name, serializers, options) @hash[@root][:links][name] ||= [] - @hash[@root][:links][name] += serializers.map(&:id) + @hash[@root][:links][name] += serializers.map{|serializer| serializer.id.to_s } unless options[:embed] == :ids @hash[:linked][name] ||= [] - @hash[:linked][name] += serializers.map { |item| item.attributes(options) } + @hash[:linked][name] += serializers.map { |item| attributes_for_serializer(item, options) } end end def add_link(name, serializer, options) - @hash[@root][:links][name] = serializer.id + @hash[@root][:links][name] = serializer.id.to_s unless options[:embed] == :ids plural_name = name.to_s.pluralize.to_sym @hash[:linked][plural_name] ||= [] - @hash[:linked][plural_name].push serializer.attributes(options) + @hash[:linked][plural_name].push attributes_for_serializer(serializer, options) end end + + private + + def attributes_for_serializer(serializer, options) + attributes = serializer.attributes(options) + attributes[:id] = attributes[:id].to_s if attributes[:id] + attributes + end end end end diff --git a/test/adapter/json_api/belongs_to_test.rb b/test/adapter/json_api/belongs_to_test.rb index 5c1df9f98..0a43e5319 100644 --- a/test/adapter/json_api/belongs_to_test.rb +++ b/test/adapter/json_api/belongs_to_test.rb @@ -16,11 +16,11 @@ def setup end def test_includes_post_id - assert_equal(42, @adapter.serializable_hash[:comments][:links][:post]) + assert_equal("42", @adapter.serializable_hash[:comments][:links][:post]) end def test_includes_linked_post - assert_equal([{id: 42, title: 'New Post', body: 'Body'}], @adapter.serializable_hash[:linked][:posts]) + assert_equal([{id: "42", title: 'New Post', body: 'Body'}], @adapter.serializable_hash[:linked][:posts]) end end end diff --git a/test/adapter/json_api/collection_test.rb b/test/adapter/json_api/collection_test.rb index ee4626711..566581a84 100644 --- a/test/adapter/json_api/collection_test.rb +++ b/test/adapter/json_api/collection_test.rb @@ -17,8 +17,8 @@ def setup def test_include_multiple_posts assert_equal([ - {title: "Hello!!", body: "Hello, world!!", id: 1, links: {comments: []}}, - {title: "New Post", body: "Body", id: 2, links: {comments: []}} + {title: "Hello!!", body: "Hello, world!!", id: "1", links: {comments: []}}, + {title: "New Post", body: "Body", id: "2", links: {comments: []}} ], @adapter.serializable_hash[:posts]) end end diff --git a/test/adapter/json_api/has_many_embed_ids.rb b/test/adapter/json_api/has_many_embed_ids.rb index f7755b725..d5c448b51 100644 --- a/test/adapter/json_api/has_many_embed_ids.rb +++ b/test/adapter/json_api/has_many_embed_ids.rb @@ -18,7 +18,7 @@ def setup end def test_includes_comment_ids - assert_equal([1, 2], @adapter.serializable_hash[:authors][:links][:posts]) + assert_equal(["1", "2"], @adapter.serializable_hash[:authors][:links][:posts]) end def test_no_includes_linked_comments diff --git a/test/adapter/json_api/has_many_test.rb b/test/adapter/json_api/has_many_test.rb index 9fad0f8b5..f558c7d46 100644 --- a/test/adapter/json_api/has_many_test.rb +++ b/test/adapter/json_api/has_many_test.rb @@ -18,13 +18,13 @@ def setup end def test_includes_comment_ids - assert_equal([1, 2], @adapter.serializable_hash[:posts][:links][:comments]) + assert_equal(["1", "2"], @adapter.serializable_hash[:posts][:links][:comments]) end def test_includes_linked_comments assert_equal([ - {id: 1, body: 'ZOMG A COMMENT'}, - {id: 2, body: 'ZOMG ANOTHER COMMENT'} + {id: "1", body: 'ZOMG A COMMENT'}, + {id: "2", body: 'ZOMG ANOTHER COMMENT'} ], @adapter.serializable_hash[:linked][:comments]) end end