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

Simplify Include Directives Hash #1311

Open
wants to merge 1 commit into
base: master
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
6 changes: 3 additions & 3 deletions lib/jsonapi/active_relation_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ def find_to_populate_by_keys(keys, options = {})
def find_fragments(filters, options = {})
include_directives = options[:include_directives] ? options[:include_directives].include_directives : {}
resource_klass = self
linkage_relationships = to_one_relationships_for_linkage(include_directives[:include_related])
linkage_relationships = to_one_relationships_for_linkage(include_directives)

sort_criteria = options.fetch(:sort_criteria) { [] }

Expand Down Expand Up @@ -381,7 +381,7 @@ def find_related_monomorphic_fragments(source_rids, relationship, options, conne

include_directives = options[:include_directives] ? options[:include_directives].include_directives : {}
resource_klass = relationship.resource_klass
linkage_relationships = resource_klass.to_one_relationships_for_linkage(include_directives[:include_related])
linkage_relationships = resource_klass.to_one_relationships_for_linkage(include_directives)

sort_criteria = []
options[:sort_criteria].try(:each) do |sort|
Expand Down Expand Up @@ -514,7 +514,7 @@ def find_related_polymorphic_fragments(source_rids, relationship, options, conne

resource_types.each do |resource_type|
related_resource_klass = resource_klass_for(resource_type)
relationships = related_resource_klass.to_one_relationships_for_linkage(include_directives[:include_related])
relationships = related_resource_klass.to_one_relationships_for_linkage(include_directives)
relationships.each do |r|
linkage_relationships << "##{resource_type}.#{r}"
end
Expand Down
16 changes: 5 additions & 11 deletions lib/jsonapi/include_directives.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,15 @@ class IncludeDirectives
# will transform into =>
# {
# posts: {
# include_related: {
# comments:{
# include_related: {
# tags: {
# include_related: {}
# }
# }
# }
# comments: {
# tags: {}
# }
# }
# }

def initialize(resource_klass, includes_array)
@resource_klass = resource_klass
@include_directives_hash = { include_related: {} }
@include_directives_hash = {}
includes_array.each do |include|
parse_include(include)
end
Expand All @@ -42,8 +36,8 @@ def parse_include(include)
path.segments.each do |segment|
relationship_name = segment.relationship.name.to_sym

current[:include_related][relationship_name] ||= { include_related: {} }
current = current[:include_related][relationship_name]
current[relationship_name] ||= {}
current = current[relationship_name]
end

rescue JSONAPI::Exceptions::InvalidRelationship => _e
Expand Down
8 changes: 4 additions & 4 deletions lib/jsonapi/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -367,15 +367,15 @@ def result_options
end

def find_resource_set(resource_klass, include_directives, options)
include_related = include_directives.include_directives[:include_related] if include_directives
include_related = include_directives.include_directives if include_directives

resource_id_tree = find_resource_id_tree(resource_klass, options, include_related)

JSONAPI::ResourceSet.new(resource_id_tree)
end

def find_related_resource_set(resource, relationship_name, include_directives, options)
include_related = include_directives.include_directives[:include_related] if include_directives
include_related = include_directives.include_directives if include_directives

resource_id_tree = find_resource_id_tree_from_resource_relationship(resource, relationship_name, options, include_related)

Expand Down Expand Up @@ -443,12 +443,12 @@ def load_included(resource_klass, source_resource_id_tree, include_related, opti
)

related_resource_id_tree = source_resource_id_tree.fetch_related_resource_id_tree(relationship)
related_resource_id_tree.add_resource_fragments(related_fragments, include_related[key][include_related])
related_resource_id_tree.add_resource_fragments(related_fragments, include_related[key])

# Now recursively get the related resources for the currently found resources
load_included(relationship.resource_klass,
related_resource_id_tree,
include_related[relationship_name][:include_related],
include_related[relationship_name],
options)
end
end
Expand Down
3 changes: 2 additions & 1 deletion test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4329,7 +4329,8 @@ def test_complex_includes_things_nested_things
"links" => {
"self" => "http://test.host/api/things/40/relationships/things",
"related" => "http://test.host/api/things/40/things"
}
},
"data" => []
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgebhardt I would like you to confirm something about this change.

@tommy-russoniello asked me why this would have changed but L4389 has another resource that did not change to include data. My explanation for this is that this resource is not a leaf in the things.things.things include, but the other one is, so we did the work of looking up the related things for this one, but not the other.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@scottgonzalez I believe you are correct.

}
}
},
Expand Down
4 changes: 2 additions & 2 deletions test/unit/processor/default_processor_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def setup
}
p = JSONAPI::Processor.new(PostResource, :find, params)

$id_tree_has_one_includes = p.send(:find_resource_id_tree, PostResource, find_options, directives[:include_related])
$id_tree_has_one_includes = p.send(:find_resource_id_tree, PostResource, find_options, directives)
$resource_set_has_one_includes = JSONAPI::ResourceSet.new($id_tree_has_one_includes)
$populated_resource_set_has_one_includes = JSONAPI::ResourceSet.new($id_tree_has_one_includes).populate!($serializer, nil,{})
end
Expand Down Expand Up @@ -111,4 +111,4 @@ def test_populated_resource_set_has_one_includes_relationships_are_resolved
assert_equal 12, $populated_resource_set_has_one_includes.resource_klasses[PersonResource][1004][:relationships][:posts].first.id
end

end
end
78 changes: 16 additions & 62 deletions test/unit/serializer/include_directives_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,33 +6,17 @@ class IncludeDirectivesTest < ActiveSupport::TestCase
def test_one_level_one_include
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts']).include_directives

assert_hash_equals(
{
include_related: {
posts: {
include_related: {}
}
}
},
directives)
assert_hash_equals({ posts: {} }, directives)
end

def test_one_level_multiple_includes
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts', 'comments', 'expense_entries']).include_directives

assert_hash_equals(
{
include_related: {
posts: {
include_related: {}
},
comments: {
include_related: {}
},
expense_entries: {
include_related: {}
}
}
posts: {},
comments: {},
expense_entries: {}
},
directives)
end
Expand All @@ -42,21 +26,11 @@ def test_multiple_level_multiple_includes

assert_hash_equals(
{
include_related: {
posts: {
include_related: {
comments: {
include_related: {}
}
}
},
comments: {
include_related: {}
},
expense_entries: {
include_related: {}
}
}
posts: {
comments: {}
},
comments: {},
expense_entries: {}
},
directives)
end
Expand All @@ -67,14 +41,8 @@ def test_two_levels_include_full_path

assert_hash_equals(
{
include_related: {
posts: {
include_related: {
comments: {
include_related: {}
}
}
}
posts: {
comments: {}
}
},
directives)
Expand All @@ -85,14 +53,8 @@ def test_two_levels_include_full_path_redundant

assert_hash_equals(
{
include_related: {
posts: {
include_related: {
comments: {
include_related: {}
}
}
}
posts: {
comments: {}
}
},
directives)
Expand All @@ -103,17 +65,9 @@ def test_three_levels_include_full

assert_hash_equals(
{
include_related: {
posts: {
include_related: {
comments: {
include_related: {
tags: {
include_related: {}
}
}
}
}
posts: {
comments: {
tags: {}
}
}
},
Expand Down
16 changes: 8 additions & 8 deletions test/unit/serializer/serializer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def test_serializer

directives = JSONAPI::IncludeDirectives.new(PersonResource, ['']).include_directives

id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives[:include_related])
id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives)
resource_set = JSONAPI::ResourceSet.new(id_tree)

serializer = JSONAPI::ResourceSerializer.new(
Expand Down Expand Up @@ -108,7 +108,7 @@ def test_serializer_namespaced_resource_with_custom_resource_links

directives = JSONAPI::IncludeDirectives.new(PersonResource, ['']).include_directives

id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives[:include_related])
id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives)
resource_set = JSONAPI::ResourceSet.new(id_tree)

serializer = JSONAPI::ResourceSerializer.new(
Expand Down Expand Up @@ -165,7 +165,7 @@ def test_serializer_limited_fieldset

directives = JSONAPI::IncludeDirectives.new(PersonResource, []).include_directives

id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives[:include_related])
id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives)
resource_set = JSONAPI::ResourceSet.new(id_tree)

serializer = JSONAPI::ResourceSerializer.new(
Expand Down Expand Up @@ -208,14 +208,14 @@ def test_serializer_include

directives = JSONAPI::IncludeDirectives.new(PostResource, ['author']).include_directives

id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives[:include_related])
id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives)

rel_id_tree = id_tree.fetch_related_resource_id_tree(PostResource._relationships[:author])

author_fragment = JSONAPI::ResourceFragment.new(person_1001_identity)
author_fragment.add_related_from(post_1_identity)
author_fragment.add_related_identity(:posts, post_1_identity)
rel_id_tree.add_resource_fragment(author_fragment, directives[:include_related][:author][:include_related])
rel_id_tree.add_resource_fragment(author_fragment, directives[:author])

resource_set = JSONAPI::ResourceSet.new(id_tree)

Expand Down Expand Up @@ -339,14 +339,14 @@ def test_serializer_key_format

directives = JSONAPI::IncludeDirectives.new(PostResource, ['author']).include_directives

id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives[:include_related])
id_tree.add_resource_fragment(JSONAPI::ResourceFragment.new(post_1_identity), directives)

rel_id_tree = id_tree.fetch_related_resource_id_tree(PostResource._relationships[:author])

author_fragment = JSONAPI::ResourceFragment.new(person_1001_identity)
author_fragment.add_related_from(post_1_identity)
author_fragment.add_related_identity(:posts, post_1_identity)
rel_id_tree.add_resource_fragment(author_fragment, directives[:include_related][:author][:include_related])
rel_id_tree.add_resource_fragment(author_fragment, directives[:author])

resource_set = JSONAPI::ResourceSet.new(id_tree)

Expand Down Expand Up @@ -479,7 +479,7 @@ def test_serializers_linkage_even_without_included_resource
fragment.initialize_related(:section)
fragment.initialize_related(:tags)

id_tree.add_resource_fragment(fragment, directives[:include_related])
id_tree.add_resource_fragment(fragment, directives)
resource_set = JSONAPI::ResourceSet.new(id_tree)

serializer = JSONAPI::ResourceSerializer.new(PostResource,
Expand Down