Skip to content

Commit

Permalink
Verify filters that are passed to show_related_resources (#971)
Browse files Browse the repository at this point in the history
(cherry picked from commit 2480458)
  • Loading branch information
hidde-jan authored and lgebhardt committed Feb 24, 2017
1 parent b1277ab commit 3a691b2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/jsonapi/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ def show_related_resources
include_directives = params[:include_directives]

source_resource ||= source_klass.find_by_key(source_id, context: context, fields: fields)
verified_filters = resource_klass.verify_filters(filters, context)

rel_opts = {
filters: filters,
filters: verified_filters,
sort_criteria: sort_criteria,
paginator: paginator,
fields: fields,
Expand Down
3 changes: 3 additions & 0 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -808,6 +808,9 @@ def records(_options = {})

def verify_filters(filters, context = nil)
verified_filters = {}

return verified_filters if filters.nil?

filters.each do |filter, raw_value|
verified_filter = verify_filter(filter, raw_value, context)
verified_filters[verified_filter[0]] = verified_filter[1]
Expand Down
11 changes: 11 additions & 0 deletions test/controllers/controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2438,6 +2438,17 @@ def test_invalid_filter_value
assert_response :bad_request
end

def test_invalid_filter_value_for_get_related_resources
assert_cacheable_get :get_related_resources, params: {
hair_cut_id: 1,
relationship: 'people',
source: 'hair_cuts',
filter: {name: 'L'}
}

assert_response :bad_request
end

def test_valid_filter_value
assert_cacheable_get :index, params: {filter: {name: 'Joe Author'}}
assert_response :success
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ class Section < ActiveRecord::Base
end

class HairCut < ActiveRecord::Base
has_many :people
end

class Property < ActiveRecord::Base
Expand Down
1 change: 1 addition & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ class CatResource < JSONAPI::Resource
jsonapi_resources :comments
jsonapi_resources :firms
jsonapi_resources :tags
jsonapi_resources :hair_cuts
jsonapi_resources :posts do
jsonapi_relationships
jsonapi_links :special_tags
Expand Down

0 comments on commit 3a691b2

Please sign in to comment.