diff --git a/lib/jsonapi/processor.rb b/lib/jsonapi/processor.rb index 7cb527ed7..8e9f3ed57 100644 --- a/lib/jsonapi/processor.rb +++ b/lib/jsonapi/processor.rb @@ -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, diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index efaae4635..3f673063a 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -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] diff --git a/test/controllers/controller_test.rb b/test/controllers/controller_test.rb index 0e7f7ed42..9db02d6c6 100644 --- a/test/controllers/controller_test.rb +++ b/test/controllers/controller_test.rb @@ -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 diff --git a/test/fixtures/active_record.rb b/test/fixtures/active_record.rb index da68b5fe4..3a864c4fc 100644 --- a/test/fixtures/active_record.rb +++ b/test/fixtures/active_record.rb @@ -394,6 +394,7 @@ class Section < ActiveRecord::Base end class HairCut < ActiveRecord::Base + has_many :people end class Property < ActiveRecord::Base diff --git a/test/test_helper.rb b/test/test_helper.rb index 51ba0459d..cb1d47991 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -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