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

Adjusts Deserializer relationship parsing to allow links/meta keys #2193

Open
wants to merge 1 commit into
base: 0-10-stable
Choose a base branch
from
Open
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_serializers/adapter/json_api/deserialization.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ class JsonApi
module Deserialization
InvalidDocument = Class.new(ArgumentError)

# http://jsonapi.org/format/#document-resource-object-relationships
VALID_RELATIONSHIP_KEYS = %w(links data meta).freeze

module_function

# Transform a JSON API document, containing a single data object,
Expand Down Expand Up @@ -132,8 +135,8 @@ def validate_payload(payload)
end

relationships.each do |(key, value)|
unless value.is_a?(Hash) && value.key?('data')
yield payload, { data: { relationships: { key => 'Expected hash with :data key' } } }
unless value.is_a?(Hash) && value.keys.any? { |k| VALID_RELATIONSHIP_KEYS.include?(k) }
yield payload, { data: { relationships: { key => "Expected hash with at least a #{VALID_RELATIONSHIP_KEYS.to_sentence(last_word_connector: 'or')} key" } } }
end
end
end
Expand Down Expand Up @@ -163,6 +166,8 @@ def parse_attributes(attributes, options)
# Given an association name, and a relationship data attribute, build a hash
# mapping the corresponding ActiveRecord attribute to the corresponding value.
#
# Ignores relationships specifying 'link' or 'meta' keys.
#
# @example
# parse_relationship(:comments, [{ 'id' => '1', 'type' => 'comments' },
# { 'id' => '2', 'type' => 'comments' }],
Expand All @@ -173,12 +178,15 @@ def parse_attributes(attributes, options)
# parse_relationship(:author, nil, {})
# # => { :author_id => nil }
# @param [Symbol] assoc_name
# @param [Hash] assoc_data
# @param [Hash] association
# @param [Hash] options
# @return [Hash{Symbol, Object}]
#
# @api private
def parse_relationship(assoc_name, assoc_data, options)
def parse_relationship(assoc_name, association, options)
return {} unless association.key?('data')
assoc_data = association['data']

prefix_key = field_key(assoc_name, options).to_s.singularize
hash =
if assoc_data.is_a?(Array)
Expand All @@ -198,7 +206,7 @@ def parse_relationship(assoc_name, assoc_data, options)
# @api private
def parse_relationships(relationships, options)
transform_keys(relationships, options)
.map { |(k, v)| parse_relationship(k, v['data'], options) }
.map { |(k, v)| parse_relationship(k, v, options) }
.reduce({}, :merge)
end

Expand Down
37 changes: 37 additions & 0 deletions test/action_controller/json_api/deserialization_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,43 @@ def test_deserialization

assert_equal(expected, response)
end

def test_deserialization_with_links_relationship
hash = {
'data' => {
'type' => 'photos',
'id' => 'zorglub',
'attributes' => {
'title' => 'Ember Hamster',
'src' => 'http://example.com/images/productivity.png',
'image-width' => '200',
'imageHeight' => '200',
'ImageSize' => '1024'
},
'relationships' => {
'images' => {
'links' => {
'related' => 'https://example.com/images/tomster/related'
}
}
}
}
}

post :render_parsed_payload, params: hash

response = JSON.parse(@response.body)
expected = {
'id' => 'zorglub',
'title' => 'Ember Hamster',
'src' => 'http://example.com/images/productivity.png',
'image_width' => '200',
'image_height' => '200',
'image_size' => '1024'
}

assert_equal(expected, response)
end
end
end
end
Expand Down