Merge 3e4c82f2066bb789b0399de878872ee39f63c9ed into 7db80f673d037ed841bef8e8a93dcda21380e135

This commit is contained in:
Antoine Braconnier 2022-04-14 14:21:27 +05:30 committed by GitHub
commit 8c7de132f3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 29 additions and 5 deletions

View File

@ -53,9 +53,10 @@ module FastJsonapi
def relationships_hash(record, relationships = nil, fieldset = nil, includes_list = nil, params = {})
relationships = relationships_to_serialize if relationships.nil?
relationships = relationships.slice(*fieldset) if fieldset.present?
relationships = {} if fieldset == []
relationship_fieldset = fieldset.present? ? fieldset & relationships.keys : []
return nil if fieldset.present? && relationship_fieldset.empty?
relationships = relationships.slice(*relationship_fieldset) if fieldset.present?
relationships.each_with_object({}) do |(key, relationship), hash|
included = includes_list.present? && includes_list.include?(key)
relationship.serialize(record, included, params, hash)
@ -72,15 +73,15 @@ module FastJsonapi
record_hash = cache_store_instance.fetch(record, **cache_opts) do
temp_hash = id_hash(id_from_record(record, params), record_type, true)
temp_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?
temp_hash[:relationships] = relationships_hash(record, cachable_relationships_to_serialize, fieldset, includes_list, params) if cachable_relationships_to_serialize.present?
temp_hash[:relationships] = relationships_hash(record, cachable_relationships_to_serialize, fieldset, includes_list, params) if cachable_relationships_to_serialize.present? && relationships_hash(record, cachable_relationships_to_serialize, fieldset, includes_list, params).present?
temp_hash[:links] = links_hash(record, params) if data_links.present?
temp_hash
end
record_hash[:relationships] = (record_hash[:relationships] || {}).merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, includes_list, params)) if uncachable_relationships_to_serialize.present?
record_hash[:relationships] = (record_hash[:relationships] || {}).merge(relationships_hash(record, uncachable_relationships_to_serialize, fieldset, includes_list, params)) if uncachable_relationships_to_serialize.present? && relationships_hash(record, nil, fieldset, includes_list, params).present?
else
record_hash = id_hash(id_from_record(record, params), record_type, true)
record_hash[:attributes] = attributes_hash(record, fieldset, params) if attributes_to_serialize.present?
record_hash[:relationships] = relationships_hash(record, nil, fieldset, includes_list, params) if relationships_to_serialize.present?
record_hash[:relationships] = relationships_hash(record, nil, fieldset, includes_list, params) if relationships_to_serialize.present? && relationships_hash(record, nil, fieldset, includes_list, params).present?
record_hash[:links] = links_hash(record, params) if data_links.present?
end

View File

@ -57,6 +57,29 @@ RSpec.describe JSONAPI::Serializer do
.and(have_id(actor.movies[0].id))
.and(have_jsonapi_attributes('release_year').exactly)
)
expect(serialized['data'])
.to_not have_relationship('played_movies')
end
end
context 'with fields for the relationship' do
let(:params) do
{
fields: { actor: [:first_name, :played_movies] }
}
end
it do
expect(serialized['data'])
.to have_jsonapi_attributes(:first_name).exactly
expect(serialized['data'])
.to have_relationship('played_movies')
.with_data(
[
{ 'id' => actor.movies[0].id, 'type' => 'movie' },
]
)
end
end
end