Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 4b9d7c2

Browse files
committedApr 20, 2022
Co-erce non-present values like false to string
1 parent dc96280 commit 4b9d7c2

File tree

2 files changed

+17
-0
lines changed

2 files changed

+17
-0
lines changed
 

‎lib/jsonapi/resource.rb

+7
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,13 @@ def is_filter_relationship?(filter)
895895

896896
def verify_filter(filter, raw, context = nil)
897897
filter_values = []
898+
if raw == true
899+
raw = "true"
900+
elsif raw == false
901+
raw = "false"
902+
else
903+
nil # no-op
904+
end
898905
if raw.present?
899906
begin
900907
filter_values += raw.is_a?(String) ? CSV.parse_line(raw) : [raw]

‎test/unit/resource/resource_test.rb

+10
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,16 @@ def test_updatable_fields_does_not_include_id
321321
assert(!FelineResource.updatable_fields.include?(:id))
322322
end
323323

324+
def test_verify_filter
325+
resource_klass = PersonResource
326+
context = nil
327+
filter = :name
328+
assert_equal([filter, ["true"]], resource_klass.verify_filter(filter, true, context))
329+
assert_equal([filter, ["false"]], resource_klass.verify_filter(filter, false, context))
330+
assert_equal([filter, ["true"]], resource_klass.verify_filter(filter, "true", context))
331+
assert_equal([filter, ["false"]], resource_klass.verify_filter(filter, "false", context))
332+
end
333+
324334
def test_filter_on_to_many_relationship_id
325335
posts = PostResource.find(:comments => 3)
326336
assert_equal([2], posts.map(&:id))

0 commit comments

Comments
 (0)
Please sign in to comment.