Skip to content

Commit

Permalink
Co-erce non-present values like false to string
Browse files Browse the repository at this point in the history
  • Loading branch information
bf4 committed Apr 20, 2022
1 parent dc96280 commit f181f59
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/jsonapi/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,13 @@ def is_filter_relationship?(filter)

def verify_filter(filter, raw, context = nil)
filter_values = []
if raw == true
raw = "true"
elsif raw == false
raw = "false"
else
nil # no-op
end
if raw.present?
begin
filter_values += raw.is_a?(String) ? CSV.parse_line(raw) : [raw]
Expand Down
9 changes: 9 additions & 0 deletions test/unit/resource/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,15 @@ def test_updatable_fields_does_not_include_id
assert(!FelineResource.updatable_fields.include?(:id))
end

def test_verify_filter
resource_klass = PersonResource
context = nil
assert_equal([:name, ["true"]], resource_klass.verify_filter(:name, true, context))
assert_equal([:name, ["false"]], resource_klass.verify_filter(:name, false, context))
assert_equal([:name, ["true"]], resource_klass.verify_filter(:name, "true", context))
assert_equal([:name, ["false"]], resource_klass.verify_filter(:name, "false", context))
end

def test_filter_on_to_many_relationship_id
posts = PostResource.find(:comments => 3)
assert_equal([2], posts.map(&:id))
Expand Down

0 comments on commit f181f59

Please sign in to comment.