diff --git a/lib/graphiti/util/link.rb b/lib/graphiti/util/link.rb index 9dc76b90..6a44b117 100644 --- a/lib/graphiti/util/link.rb +++ b/lib/graphiti/util/link.rb @@ -59,7 +59,7 @@ def on_demand_links(url) def params @params ||= {}.tap do |params| - if @sideload.type != :belongs_to + if @sideload.type != :belongs_to || @sideload.remote? params[:filter] = @sideload.base_filter([@model]) end @@ -70,7 +70,7 @@ def params def path @path ||= path = @sideload.resource.endpoint[:url].to_s - if @sideload.type == :belongs_to + if @sideload.type == :belongs_to && !@sideload.remote? path = "#{path}/#{@model.send(@sideload.foreign_key)}" end path diff --git a/spec/serialization_spec.rb b/spec/serialization_spec.rb index 1469f88b..71e01000 100644 --- a/spec/serialization_spec.rb +++ b/spec/serialization_spec.rb @@ -1185,7 +1185,7 @@ def define_relationship render credit_card = json["data"][0]["relationships"]["credit_card"] expect(credit_card["links"]["related"]) - .to eq("http://foo.com/mastercards/789") + .to eq("http://foo.com/mastercards?filter[id]=789") end end @@ -1351,7 +1351,25 @@ def classification remote: "http://foo.com/classifications" render expect(classification["links"]["related"]) - .to eq("http://foo.com/classifications/789") + .to eq("http://foo.com/classifications?filter[id]=789") + end + + # Special case because we hit index with a filter + context 'and params are customized' do + before do + resource.belongs_to :classification, + remote: "http://foo.com/classifications" do + params do |hash| + hash[:filter][:foo] = 'bar' + end + end + end + + it 'links correctly' do + render + expect(classification["links"]["related"]) + .to eq("http://foo.com/classifications?filter[foo]=bar&filter[id]=789") + end end end end