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 4b9d7c2
Show file tree
Hide file tree
Showing 2 changed files with 17 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
10 changes: 10 additions & 0 deletions test/unit/resource/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,16 @@ def test_updatable_fields_does_not_include_id
assert(!FelineResource.updatable_fields.include?(:id))
end

def test_verify_filter
resource_klass = PersonResource
context = nil
filter = :name
assert_equal([filter, ["true"]], resource_klass.verify_filter(filter, true, context))
assert_equal([filter, ["false"]], resource_klass.verify_filter(filter, false, context))
assert_equal([filter, ["true"]], resource_klass.verify_filter(filter, "true", context))
assert_equal([filter, ["false"]], resource_klass.verify_filter(filter, "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 4b9d7c2

Please sign in to comment.