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

Serialize ids as strings in JSON-API adapter #683

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions lib/active_model/serializer/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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] ||= {}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions test/adapter/json_api/belongs_to_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test/adapter/json_api/has_many_embed_ids.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions test/adapter/json_api/has_many_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down