Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Co-erce non-present values like false to string #1387

Open
wants to merge 1 commit into
base: release-0-9
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lgebhardt any objection to this? otherwise false is treated as not present

if raw.present?
begin
filter_values += raw.is_a?(String) ? CSV.parse_line(raw) : [raw]
Expand Down
12 changes: 12 additions & 0 deletions test/unit/resource/resource_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,18 @@ 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))
assert_equal([filter, []], resource_klass.verify_filter(filter, nil, context))
assert_equal([filter, []], resource_klass.verify_filter(filter, "", context))
end

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