Allow fieldsets to specify no attributes/relationships
This commit is contained in:
parent
1a407c0030
commit
21ae4aaa0a
@ -44,6 +44,8 @@ module FastJsonapi
|
||||
def attributes_hash(record, fieldset = nil, params = {})
|
||||
attributes = attributes_to_serialize
|
||||
attributes = attributes.slice(*fieldset) if fieldset.present?
|
||||
attributes = {} if fieldset == []
|
||||
|
||||
attributes.each_with_object({}) do |(_k, attribute), hash|
|
||||
attribute.serialize(record, params, hash)
|
||||
end
|
||||
@ -52,6 +54,7 @@ module FastJsonapi
|
||||
def relationships_hash(record, relationships = nil, fieldset = nil, params = {})
|
||||
relationships = relationships_to_serialize if relationships.nil?
|
||||
relationships = relationships.slice(*fieldset) if fieldset.present?
|
||||
relationships = {} if fieldset == []
|
||||
|
||||
relationships.each_with_object({}) do |(_k, relationship), hash|
|
||||
relationship.serialize(record, params, hash)
|
||||
|
@ -22,6 +22,18 @@ describe FastJsonapi::ObjectSerializer do
|
||||
expect(hash[:data][:relationships].keys.sort).to eq %i[actors advertising_campaign]
|
||||
end
|
||||
|
||||
it 'returns no fields when none are specified' do
|
||||
hash = MovieSerializer.new(movie, fields: { movie: [] }).serializable_hash
|
||||
|
||||
expect(hash[:data][:attributes].keys).to eq []
|
||||
end
|
||||
|
||||
it 'returns no relationships when none are specified' do
|
||||
hash = MovieSerializer.new(movie, fields: { movie: [] }).serializable_hash
|
||||
|
||||
expect(hash[:data][:relationships].keys).to eq []
|
||||
end
|
||||
|
||||
it 'only returns specified fields for included relationships' do
|
||||
hash = MovieSerializer.new(movie, fields: fields, include: %i[actors]).serializable_hash
|
||||
|
||||
@ -45,4 +57,25 @@ describe FastJsonapi::ObjectSerializer do
|
||||
|
||||
expect(hash[:included][3][:relationships].keys.sort).to eq %i[movie]
|
||||
end
|
||||
|
||||
context 'with no included fields specified' do
|
||||
let(:fields) do
|
||||
{
|
||||
movie: %i[name actors advertising_campaign],
|
||||
actor: []
|
||||
}
|
||||
end
|
||||
|
||||
it 'returns no fields for included relationships when none are specified' do
|
||||
hash = MovieSerializer.new(movie, fields: fields, include: %i[actors advertising_campaign]).serializable_hash
|
||||
|
||||
expect(hash[:included][2][:attributes].keys).to eq []
|
||||
end
|
||||
|
||||
it 'returns no relationships when none are specified' do
|
||||
hash = MovieSerializer.new(movie, fields: fields, include: %i[actors advertising_campaign]).serializable_hash
|
||||
|
||||
expect(hash[:included][2][:relationships].keys).to eq []
|
||||
end
|
||||
end
|
||||
end
|
||||
|
Loading…
x
Reference in New Issue
Block a user