Skip to content

Commit 850f9f2

Browse files
author
Carl Thuringer
committed
Fix inconsistent type of filters
1 parent ec118b3 commit 850f9f2

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
@@ -2674,6 +2674,26 @@ def meta(options)
26742674
end
26752675
end
26762676

2677+
class Api::V2::BooksControllerTest < ActionController::TestCase
2678+
def test_get_related_resources_with_filters
2679+
$test_user = Person.find(5)
2680+
original_config = JSONAPI.configuration.dup
2681+
JSONAPI.configuration.top_level_meta_include_record_count = true
2682+
JSONAPI.configuration.json_key_format = :dasherized_key
2683+
assert_cacheable_get :get_related_resources,
2684+
params: {
2685+
author_id: '1',
2686+
relationship: 'books',
2687+
source: 'api/v2/authors',
2688+
filter: { fiction: 'true' }
2689+
}
2690+
assert_response :success
2691+
assert_equal 1, json_response['meta']['record-count']
2692+
ensure
2693+
JSONAPI.configuration = original_config
2694+
end
2695+
end
2696+
26772697
class BreedsControllerTest < ActionController::TestCase
26782698
# Note: Breed names go through the TitleValueFormatter
26792699

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

@@ -1460,10 +1461,11 @@ module V2
14601461
class PreferencesResource < PreferencesResource; end
14611462
class PersonResource < PersonResource; end
14621463
class PostResource < PostResource; end
1464+
class AuthorResource < AuthorResource; end
14631465

14641466
class BookResource < JSONAPI::Resource
14651467
attribute "title"
1466-
attributes :isbn, :banned
1468+
attributes :isbn, :banned, :fiction
14671469

14681470
paginator :offset
14691471

@@ -1484,6 +1486,7 @@ class BookResource < JSONAPI::Resource
14841486

14851487
filters :book_comments
14861488
filter :banned, apply: :apply_filter_banned
1489+
filter :fiction, apply: :apply_filter_fiction
14871490

14881491
class << self
14891492
def books
@@ -1516,6 +1519,9 @@ def apply_filter_banned(records, value, options)
15161519
end
15171520
end
15181521

1522+
def apply_filter_fiction(records, value, _options)
1523+
records.where('books.fiction = ?', value[0] == 'true')
1524+
end
15191525
end
15201526
end
15211527

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
@@ -289,6 +289,7 @@ class CatResource < JSONAPI::Resource
289289

290290
jsonapi_resource :preferences, except: [:create, :destroy]
291291

292+
jsonapi_resources :authors
292293
jsonapi_resources :books
293294
jsonapi_resources :book_comments
294295
end

0 commit comments

Comments
 (0)