Skip to content

Commit

Permalink
Return null resource object identifier for blank id
Browse files Browse the repository at this point in the history
Also, fix test where attributes were included when id was ""

```
  1) Failure:
  ActionController::Serialization::AdapterSelectorTest#test_render_using_adapter_override
  [test/action_c$ntroller/adapter_selector_test.rb:53]:
  --- expected
  +++ actual
  @@ -1 +1 @@
  -"{\"data\":{\"id\":\"\",\"type\":\"profiles\",\"attributes\":{\"name\":\"Name 1\",\"description\":\"Description 1\"}}}"
  +"{\"data\":null}"
```
  • Loading branch information
bf4 committed May 1, 2017
1 parent afe0183 commit 73eae19
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 18 deletions.
44 changes: 28 additions & 16 deletions lib/active_model_serializers/adapter/json_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -295,20 +295,8 @@ def attributes_for(serializer, fields)

# {http://jsonapi.org/format/#document-resource-objects Document Resource Objects}
def resource_object_for(serializer, include_slice = {})
resource_object = serializer.fetch(self) do
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?
resource_object = data_for(serializer, include_slice)

links = links_for(serializer)
# toplevel_links
# definition:
# allOf
Expand All @@ -322,7 +310,10 @@ def resource_object_for(serializer, include_slice = {})
# prs:
# https://github.com/rails-api/active_model_serializers/pull/1247
# 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
# alias meta
Expand All @@ -332,12 +323,33 @@ def resource_object_for(serializer, include_slice = {})
# {
# :'git-ref' => 'abc123'
# }
meta = meta_for(serializer)
resource_object[:meta] = meta unless meta.blank?
if (meta = meta_for(serializer)).present?
resource_object ||= {}
resource_object[:meta] = meta
end

resource_object
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}
# relationships
# definition:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def self.type_for(class_name, serializer_type = nil, transform_options = {})
end

def self.for_type_with_id(type, id, options)
return nil if id.blank?
{
id: id.to_s,
type: type_for(:no_class_needed, type, options)
Expand All @@ -36,6 +37,7 @@ def initialize(serializer, options)
end

def as_json
return nil if id.blank?
{ id: id, type: type }
end

Expand Down
4 changes: 2 additions & 2 deletions test/action_controller/adapter_selector_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def render_using_default_adapter
end

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
end

Expand All @@ -41,7 +41,7 @@ def test_render_using_adapter_override

expected = {
data: {
id: @controller.instance_variable_get(:@profile).id.to_s,
id: 'render_using_adapter_override',
type: 'profiles',
attributes: {
name: 'Name 1',
Expand Down

0 comments on commit 73eae19

Please sign in to comment.