From eacfdb0adbe0f779f860f43c8574a2f3381e6f61 Mon Sep 17 00:00:00 2001 From: Guillermo Iguaran Date: Tue, 14 Oct 2014 17:41:17 -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/has_many_embed_ids.rb | 2 +- test/adapter/json_api/has_many_test.rb | 6 +++--- 4 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/active_model/serializer/adapter/json_api.rb b/lib/active_model/serializer/adapter/json_api.rb index 76340613b..c7eec6c4e 100644 --- a/lib/active_model/serializer/adapter/json_api.rb +++ b/lib/active_model/serializer/adapter/json_api.rb @@ -8,7 +8,7 @@ def initialize(serializer, options = {}) end def serializable_hash(opts = {}) - @hash = serializer.attributes + @hash = attributes_for_serializer(serializer, {}) serializer.each_association do |name, association, options| @hash[:links] ||= {} @@ -28,24 +28,32 @@ def serializable_hash(opts = {}) def add_links(name, serializers, options) @hash[:links][name] ||= [] - @hash[:links][name] += serializers.map(&:id) + @hash[: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[:links][name] = serializer.id + @hash[: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 151fd8cce..af11b8f77 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[:links][:post]) + assert_equal("42", @adapter.serializable_hash[: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/has_many_embed_ids.rb b/test/adapter/json_api/has_many_embed_ids.rb index d3e42154e..75117f896 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[:links][:posts]) + assert_equal(["1", "2"], @adapter.serializable_hash[: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 435079d7c..3eac47153 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[:links][:comments]) + assert_equal(["1", "2"], @adapter.serializable_hash[: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