Merge pull request #2119 from bf4/exclude_empty_relationships

Return null resource object identifier for blank id
This commit is contained in:
Benjamin Fleischer 2017-04-30 21:45:10 -07:00 committed by GitHub
commit 004e0dc951
3 changed files with 32 additions and 18 deletions

View File

@ -295,20 +295,8 @@ module ActiveModelSerializers
# {http://jsonapi.org/format/#document-resource-objects Document Resource Objects} # {http://jsonapi.org/format/#document-resource-objects Document Resource Objects}
def resource_object_for(serializer, include_slice = {}) def resource_object_for(serializer, include_slice = {})
resource_object = serializer.fetch(self) do resource_object = data_for(serializer, include_slice)
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
attributes = attributes_for(serializer, requested_fields)
resource_object[:attributes] = attributes if attributes.any?
resource_object
end
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
relationships = relationships_for(serializer, requested_associations, include_slice)
resource_object[:relationships] = relationships if relationships.any?
links = links_for(serializer)
# toplevel_links # toplevel_links
# definition: # definition:
# allOf # allOf
@ -322,7 +310,10 @@ module ActiveModelSerializers
# prs: # prs:
# https://github.com/rails-api/active_model_serializers/pull/1247 # https://github.com/rails-api/active_model_serializers/pull/1247
# https://github.com/rails-api/active_model_serializers/pull/1018 # https://github.com/rails-api/active_model_serializers/pull/1018
resource_object[:links] = links if links.any? if (links = links_for(serializer)).any?
resource_object ||= {}
resource_object[:links] = links
end
# toplevel_meta # toplevel_meta
# alias meta # alias meta
@ -332,12 +323,33 @@ module ActiveModelSerializers
# { # {
# :'git-ref' => 'abc123' # :'git-ref' => 'abc123'
# } # }
meta = meta_for(serializer) if (meta = meta_for(serializer)).present?
resource_object[:meta] = meta unless meta.blank? resource_object ||= {}
resource_object[:meta] = meta
end
resource_object resource_object
end end
def data_for(serializer, include_slice)
data = serializer.fetch(self) do
resource_object = ResourceIdentifier.new(serializer, instance_options).as_json
break nil if resource_object.nil?
requested_fields = fieldset && fieldset.fields_for(resource_object[:type])
attributes = attributes_for(serializer, requested_fields)
resource_object[:attributes] = attributes if attributes.any?
resource_object
end
data.tap do |resource_object|
next if resource_object.nil?
# NOTE(BF): the attributes are cached above, separately from the relationships, below.
requested_associations = fieldset.fields_for(resource_object[:type]) || '*'
relationships = relationships_for(serializer, requested_associations, include_slice)
resource_object[:relationships] = relationships if relationships.any?
end
end
# {http://jsonapi.org/format/#document-resource-object-relationships Document Resource Object Relationship} # {http://jsonapi.org/format/#document-resource-object-relationships Document Resource Object Relationship}
# relationships # relationships
# definition: # definition:

View File

@ -23,6 +23,7 @@ module ActiveModelSerializers
end end
def self.for_type_with_id(type, id, options) def self.for_type_with_id(type, id, options)
return nil if id.blank?
{ {
id: id.to_s, id: id.to_s,
type: type_for(:no_class_needed, type, options) type: type_for(:no_class_needed, type, options)
@ -36,6 +37,7 @@ module ActiveModelSerializers
end end
def as_json def as_json
return nil if id.blank?
{ id: id, type: type } { id: id, type: type }
end end

View File

@ -19,7 +19,7 @@ module ActionController
end end
def render_using_adapter_override def render_using_adapter_override
@profile = Profile.new(name: 'Name 1', description: 'Description 1', comments: 'Comments 1') @profile = Profile.new(id: 'render_using_adapter_override', name: 'Name 1', description: 'Description 1', comments: 'Comments 1')
render json: @profile, adapter: :json_api render json: @profile, adapter: :json_api
end end
@ -41,7 +41,7 @@ module ActionController
expected = { expected = {
data: { data: {
id: @controller.instance_variable_get(:@profile).id.to_s, id: 'render_using_adapter_override',
type: 'profiles', type: 'profiles',
attributes: { attributes: {
name: 'Name 1', name: 'Name 1',