From 68b358cf5dd37cccf7541a8c5ba80e062816d674 Mon Sep 17 00:00:00 2001 From: Spencer Ellinor Date: Fri, 25 Oct 2019 14:56:38 -0700 Subject: [PATCH 1/3] Add failing sideloading across context test --- spec/sideloading_spec.rb | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/spec/sideloading_spec.rb b/spec/sideloading_spec.rb index 093f9a23..e676d7ef 100644 --- a/spec/sideloading_spec.rb +++ b/spec/sideloading_spec.rb @@ -1044,6 +1044,21 @@ def department render }.to_not raise_error end + + describe "across requests" do + it "uses a different sideloaded resource" do + ctx = double(current_user: :admin) + sl1 = Graphiti.with_context ctx do + resource.all(params).query.sideloads.values[0].resource + end + + sl2 = Graphiti.with_context ctx do + resource.all(params).query.sideloads.values[0].resource + end + + expect(sl1).to_not be sl2 + end + end end context "when a required filter on the sideloaded resource" do From 03d2340882836ad218966fa25fe3534fc313c4ff Mon Sep 17 00:00:00 2001 From: Spencer Ellinor Date: Fri, 25 Oct 2019 15:00:26 -0700 Subject: [PATCH 2/3] Add Sideload#clear_resources after context block returns Fixes #186 --- lib/graphiti.rb | 4 ++++ lib/graphiti/sideload.rb | 5 +++++ 2 files changed, 9 insertions(+) diff --git a/lib/graphiti.rb b/lib/graphiti.rb index c4806b27..fc5659d8 100644 --- a/lib/graphiti.rb +++ b/lib/graphiti.rb @@ -32,6 +32,10 @@ def self.with_context(obj, namespace = nil) yield ensure self.context = prior + + resources.each { |resource_class| + resource_class.sideloads.values.each(&:clear_resources) + } end def self.config diff --git a/lib/graphiti/sideload.rb b/lib/graphiti/sideload.rb index 62bfa28e..a1157060 100644 --- a/lib/graphiti/sideload.rb +++ b/lib/graphiti/sideload.rb @@ -224,6 +224,11 @@ def parent_resource @parent_resource ||= parent_resource_class.new end + def clear_resources + @resource = nil + @parent_resource = nil + end + def assign(parents, children) track_associated = type == :has_one associated = [] if track_associated From 46453e71f33592b50936c88e3db65645d357f1bd Mon Sep 17 00:00:00 2001 From: Spencer Ellinor Date: Mon, 28 Oct 2019 14:04:25 -0700 Subject: [PATCH 3/3] Add URL of issue to aid with future refactoring --- lib/graphiti/sideload.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/lib/graphiti/sideload.rb b/lib/graphiti/sideload.rb index a1157060..2d99444e 100644 --- a/lib/graphiti/sideload.rb +++ b/lib/graphiti/sideload.rb @@ -224,6 +224,7 @@ def parent_resource @parent_resource ||= parent_resource_class.new end + # See https://github.com/graphiti-api/graphiti/issues/186 def clear_resources @resource = nil @parent_resource = nil