Skip to content

Commit bb530b0

Browse files
authored
Make raising on missing sideload configurable (#75)
It can be desirable to silently drop the relationship instead of raising an error. This is now configurable: ```ruby JsonapiCompliable.configure do |c| c.raise_on_missing_sideload = false end ```
1 parent 43dfcd5 commit bb530b0

File tree

4 files changed

+52
-7
lines changed

4 files changed

+52
-7
lines changed

lib/jsonapi_compliable.rb

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
require "jsonapi_compliable/version"
2+
require "jsonapi_compliable/configuration"
23
require "jsonapi_compliable/errors"
34
require "jsonapi_compliable/resource"
45
require "jsonapi_compliable/query"
@@ -64,4 +65,18 @@ def self.with_context(obj, namespace)
6465
self.context = prior
6566
end
6667
end
68+
69+
def self.config
70+
@config ||= Configuration.new
71+
end
72+
73+
# @example
74+
# JsonapiCompliable.configure do |c|
75+
# c.raise_on_missing_sideload = false
76+
# end
77+
#
78+
# @see Configuration
79+
def self.configure
80+
yield config
81+
end
6782
end
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# https://robots.thoughtbot.com/mygem-configure-block
2+
module JsonapiCompliable
3+
class Configuration
4+
# @return [Boolean] Should we raise when the client requests a relationship not defined on the server?
5+
# Defaults to true.
6+
attr_accessor :raise_on_missing_sideload
7+
8+
# Set defaults
9+
# @api private
10+
def initialize
11+
@raise_on_missing_sideload = true
12+
end
13+
end
14+
end

lib/jsonapi_compliable/scope.rb

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,12 +82,15 @@ def sideload(results, includes)
8282
includes.each_pair do |name, nested|
8383
sideload = @resource.sideload(name)
8484

85-
unless sideload
86-
raise JsonapiCompliable::Errors::InvalidInclude.new(name, @resource.type)
85+
if sideload.nil?
86+
if JsonapiCompliable.config.raise_on_missing_sideload
87+
raise JsonapiCompliable::Errors::InvalidInclude
88+
.new(name, @resource.type)
89+
end
90+
else
91+
namespace = Util::Sideload.namespace(@namespace, sideload.name)
92+
sideload.resolve(results, @query, namespace)
8793
end
88-
89-
namespace = Util::Sideload.namespace(@namespace, sideload.name)
90-
sideload.resolve(results, @query, namespace)
9194
end
9295
end
9396

spec/scope_spec.rb

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,9 +70,22 @@
7070
end
7171

7272
it 'raises a helpful error' do
73-
expect do
73+
expect {
7474
instance.resolve
75-
end.to raise_error(JsonapiCompliable::Errors::InvalidInclude, 'The requested included relationship "books" is not supported on resource "authors"')
75+
}.to raise_error(JsonapiCompliable::Errors::InvalidInclude, 'The requested included relationship "books" is not supported on resource "authors"')
76+
end
77+
78+
context 'but the config says not to raise errors' do
79+
before do
80+
allow(JsonapiCompliable.config)
81+
.to receive(:raise_on_missing_sideload)
82+
end
83+
84+
it 'does not raise an error' do
85+
expect {
86+
instance.resolve
87+
}.to_not raise_error
88+
end
7689
end
7790
end
7891
end

0 commit comments

Comments
 (0)