Skip to content

Commit c145109

Browse files
authored
Merge pull request #1047 from cerebris/includes_error_handling
Tightens the error handling on include parsing.
2 parents ff43ac0 + e2fd4bd commit c145109

File tree

4 files changed

+42
-9
lines changed

4 files changed

+42
-9
lines changed

lib/jsonapi/include_directives.rb

Lines changed: 1 addition & 1 deletion
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

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -331,8 +331,7 @@ def check_include(resource_klass, include_parts)
331331
include_parts.last.partition('.'))
332332
end
333333
else
334-
@errors.concat(JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type),
335-
include_parts.first).errors)
334+
fail JSONAPI::Exceptions::InvalidInclude.new(format_key(resource_klass._type), include_parts.first)
336335
end
337336
end
338337

@@ -352,12 +351,17 @@ def parse_include_directives(resource_klass, raw_include)
352351

353352
return if included_resources.nil?
354353

355-
result = included_resources.compact.map do |included_resource|
356-
check_include(resource_klass, included_resource.partition('.'))
357-
unformat_key(included_resource).to_s
358-
end
354+
begin
355+
result = included_resources.compact.map do |included_resource|
356+
check_include(resource_klass, included_resource.partition('.'))
357+
unformat_key(included_resource).to_s
358+
end
359359

360-
JSONAPI::IncludeDirectives.new(resource_klass, result)
360+
return JSONAPI::IncludeDirectives.new(resource_klass, result)
361+
rescue JSONAPI::Exceptions::InvalidInclude => e
362+
@errors.concat(e.errors)
363+
return {}
364+
end
361365
end
362366

363367
def parse_filters(resource_klass, filters)

test/controllers/controller_test.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2043,7 +2043,6 @@ def test_expense_entries_show_bad_include_missing_relationship
20432043
assert_cacheable_get :show, params: {id: 1, include: 'isoCurrencies,employees'}
20442044
assert_response :bad_request
20452045
assert_match /isoCurrencies is not a valid relationship of expenseEntries/, json_response['errors'][0]['detail']
2046-
assert_match /employees is not a valid relationship of expenseEntries/, json_response['errors'][1]['detail']
20472046
end
20482047

20492048
def test_expense_entries_show_bad_include_missing_sub_relationship
@@ -2052,6 +2051,18 @@ def test_expense_entries_show_bad_include_missing_sub_relationship
20522051
assert_match /post is not a valid relationship of people/, json_response['errors'][0]['detail']
20532052
end
20542053

2054+
def test_invalid_include
2055+
assert_cacheable_get :index, params: {include: 'invalid../../../../'}
2056+
assert_response :bad_request
2057+
assert_match /invalid is not a valid relationship of expenseEntries/, json_response['errors'][0]['detail']
2058+
end
2059+
2060+
def test_invalid_include_long_garbage_string
2061+
assert_cacheable_get :index, params: {include: 'invalid.foo.bar.dfsdfs,dfsdfs.sdfwe.ewrerw.erwrewrew'}
2062+
assert_response :bad_request
2063+
assert_match /invalid is not a valid relationship of expenseEntries/, json_response['errors'][0]['detail']
2064+
end
2065+
20552066
def test_expense_entries_show_fields
20562067
assert_cacheable_get :show, params: {id: 1, include: 'isoCurrency,employee', 'fields' => {'expenseEntries' => 'transactionDate'}}
20572068
assert_response :success

test/unit/serializer/include_directives_test.rb

Lines changed: 18 additions & 0 deletions
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)