Skip to content

Commit 4499d16

Browse files
committed
Tightens the error handling on include parsing.
Now fails on the first error encountered. (cherry picked from commit e2fd4bd)
1 parent 13f6423 commit 4499d16

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

lib/jsonapi/include_directives.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def get_related(current_path)
5252
current_relationship = current_resource_klass._relationships[fragment]
5353
current_resource_klass = current_relationship.try(:resource_klass)
5454
else
55-
warn "[RELATIONSHIP NOT FOUND] Relationship could not be found for #{current_path}."
55+
raise JSONAPI::Exceptions::InvalidInclude.new(current_resource_klass, current_path)
5656
end
5757

5858
include_in_join = @force_eager_load || !current_relationship || current_relationship.eager_load_on_include

lib/jsonapi/request_parser.rb

+11-7
Original file line numberDiff line numberDiff line change
@@ -205,8 +205,7 @@ def check_include(resource_klass, include_parts)
205205
check_include(Resource.resource_for(resource_klass.module_path + relationship.class_name.to_s.underscore), include_parts.last.partition('.'))
206206
end
207207
else
208-
@errors.concat(JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type),
209-
include_parts.first).errors)
208+
fail JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type), include_parts.first)
210209
end
211210
end
212211

@@ -226,12 +225,17 @@ def parse_include_directives(raw_include)
226225

227226
return if included_resources.empty?
228227

229-
result = included_resources.compact.map do |included_resource|
230-
check_include(@resource_klass, included_resource.partition('.'))
231-
unformat_key(included_resource).to_s
232-
end
228+
begin
229+
result = included_resources.compact.map do |included_resource|
230+
check_include(@resource_klass, included_resource.partition('.'))
231+
unformat_key(included_resource).to_s
232+
end
233233

234-
@include_directives = JSONAPI::IncludeDirectives.new(@resource_klass, result)
234+
@include_directives = JSONAPI::IncludeDirectives.new(@resource_klass, result)
235+
rescue JSONAPI::Exceptions::InvalidInclude => e
236+
@errors.concat(e.errors)
237+
@include_directives = {}
238+
end
235239
end
236240

237241
def parse_filters(filters)

test/controllers/controller_test.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -2058,7 +2058,6 @@ def test_expense_entries_show_bad_include_missing_relationship
20582058
assert_cacheable_get :show, params: {id: 1, include: 'isoCurrencies,employees'}
20592059
assert_response :bad_request
20602060
assert_match /isoCurrencies is not a valid relationship of expenseEntries/, json_response['errors'][0]['detail']
2061-
assert_match /employees is not a valid relationship of expenseEntries/, json_response['errors'][1]['detail']
20622061
end
20632062

20642063
def test_expense_entries_show_bad_include_missing_sub_relationship
@@ -2067,6 +2066,18 @@ def test_expense_entries_show_bad_include_missing_sub_relationship
20672066
assert_match /post is not a valid relationship of people/, json_response['errors'][0]['detail']
20682067
end
20692068

2069+
def test_invalid_include
2070+
assert_cacheable_get :index, params: {include: 'invalid../../../../'}
2071+
assert_response :bad_request
2072+
assert_match /invalid is not a valid relationship of expenseEntries/, json_response['errors'][0]['detail']
2073+
end
2074+
2075+
def test_invalid_include_long_garbage_string
2076+
assert_cacheable_get :index, params: {include: 'invalid.foo.bar.dfsdfs,dfsdfs.sdfwe.ewrerw.erwrewrew'}
2077+
assert_response :bad_request
2078+
assert_match /invalid is not a valid relationship of expenseEntries/, json_response['errors'][0]['detail']
2079+
end
2080+
20702081
def test_expense_entries_show_fields
20712082
assert_cacheable_get :show, params: {id: 1, include: 'isoCurrency,employee', 'fields' => {'expenseEntries' => 'transactionDate'}}
20722083
assert_response :success

test/unit/serializer/include_directives_test.rb

+18
Original file line numberDiff line numberDiff line change
@@ -143,4 +143,22 @@ def test_three_levels_include_full_model_includes
143143
directives = JSONAPI::IncludeDirectives.new(PersonResource, ['posts.comments.tags'])
144144
assert_array_equals([{:posts=>[{:comments=>[:tags]}]}], directives.model_includes)
145145
end
146+
147+
def test_invalid_includes_1
148+
assert_raises JSONAPI::Exceptions::InvalidInclude do
149+
JSONAPI::IncludeDirectives.new(PersonResource, ['../../../../']).include_directives
150+
end
151+
end
152+
153+
def test_invalid_includes_2
154+
assert_raises JSONAPI::Exceptions::InvalidInclude do
155+
JSONAPI::IncludeDirectives.new(PersonResource, ['posts./sdaa./........']).include_directives
156+
end
157+
end
158+
159+
def test_invalid_includes_3
160+
assert_raises JSONAPI::Exceptions::InvalidInclude do
161+
JSONAPI::IncludeDirectives.new(PersonResource, ['invalid../../../../']).include_directives
162+
end
163+
end
146164
end

0 commit comments

Comments
 (0)