Skip to content

Commit f30a12f

Browse files
authored
Merge pull request #1186 from hatchloyalty/pr-fix-inconsistent-filter-application
Fix inconsistent type of filters
2 parents a925eff + 850f9f2 commit f30a12f

6 files changed

Lines changed: 34 additions & 2 deletions

File tree

lib/jsonapi/processor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ def show_related_resources
197197
(paginator && paginator.class.requires_record_count) ||
198198
(JSONAPI.configuration.top_level_meta_include_page_count))
199199
related_resource_records = source_resource.public_send("records_for_" + relationship_type)
200-
records = resource_klass.filter_records(filters, {},
200+
records = resource_klass.filter_records(verified_filters, {},
201201
related_resource_records)
202202

203203
record_count = resource_klass.count_records(records)

test/controllers/controller_test.rb

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2734,6 +2734,26 @@ def meta(options)
27342734
end
27352735
end
27362736

2737+
class Api::V2::BooksControllerTest < ActionController::TestCase
2738+
def test_get_related_resources_with_filters
2739+
$test_user = Person.find(5)
2740+
original_config = JSONAPI.configuration.dup
2741+
JSONAPI.configuration.top_level_meta_include_record_count = true
2742+
JSONAPI.configuration.json_key_format = :dasherized_key
2743+
assert_cacheable_get :get_related_resources,
2744+
params: {
2745+
author_id: '1',
2746+
relationship: 'books',
2747+
source: 'api/v2/authors',
2748+
filter: { fiction: 'true' }
2749+
}
2750+
assert_response :success
2751+
assert_equal 1, json_response['meta']['record-count']
2752+
ensure
2753+
JSONAPI.configuration = original_config
2754+
end
2755+
end
2756+
27372757
class BreedsControllerTest < ActionController::TestCase
27382758
# Note: Breed names go through the TitleValueFormatter
27392759

test/fixtures/active_record.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@
141141
t.string :title
142142
t.string :isbn
143143
t.boolean :banned, default: false
144+
t.boolean :fiction, default: false
144145
t.timestamps null: false
145146
end
146147

@@ -1494,10 +1495,11 @@ module V2
14941495
class PreferencesResource < PreferencesResource; end
14951496
class PersonResource < PersonResource; end
14961497
class PostResource < PostResource; end
1498+
class AuthorResource < AuthorResource; end
14971499

14981500
class BookResource < JSONAPI::Resource
14991501
attribute "title"
1500-
attributes :isbn, :banned
1502+
attributes :isbn, :banned, :fiction
15011503

15021504
paginator :offset
15031505

@@ -1518,6 +1520,7 @@ class BookResource < JSONAPI::Resource
15181520

15191521
filters :book_comments
15201522
filter :banned, apply: :apply_filter_banned
1523+
filter :fiction, apply: :apply_filter_fiction
15211524

15221525
class << self
15231526
def books
@@ -1550,6 +1553,9 @@ def apply_filter_banned(records, value, options)
15501553
end
15511554
end
15521555

1556+
def apply_filter_fiction(records, value, _options)
1557+
records.where('books.fiction = ?', value[0] == 'true')
1558+
end
15531559
end
15541560
end
15551561

test/fixtures/book_authors.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,7 @@ book_author_2_1:
99
book_author_2_2:
1010
book_id: 2
1111
person_id: 2
12+
13+
book_author_601_1:
14+
book_id: 601
15+
person_id: 1

test/fixtures/books.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,5 @@ book_<%= book_num %>:
44
title: Book <%= book_num %>
55
isbn: 12345-<%= book_num %>-6789
66
banned: <%= book_num > 600 && book_num < 700 %>
7+
fiction: <%= book_num > 600 && book_num < 700 %>
78
<% end %>

test/test_helper.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -292,6 +292,7 @@ class CatResource < JSONAPI::Resource
292292

293293
jsonapi_resource :preferences, except: [:create, :destroy]
294294

295+
jsonapi_resources :authors
295296
jsonapi_resources :books
296297
jsonapi_resources :book_comments
297298
end

0 commit comments

Comments
 (0)