From 0e0c4e583e47f5ec71e06550882fad0b2a07b66d Mon Sep 17 00:00:00 2001 From: Benjamin Fleischer Date: Wed, 20 Apr 2022 09:25:40 -0500 Subject: [PATCH] Co-erce non-present values like false to string --- lib/jsonapi/resource.rb | 7 +++++++ test/unit/resource/resource_test.rb | 12 ++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/jsonapi/resource.rb b/lib/jsonapi/resource.rb index 6fa78dffb..ff1acc54f 100644 --- a/lib/jsonapi/resource.rb +++ b/lib/jsonapi/resource.rb @@ -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] diff --git a/test/unit/resource/resource_test.rb b/test/unit/resource/resource_test.rb index 7fe44bbd8..69afaf384 100644 --- a/test/unit/resource/resource_test.rb +++ b/test/unit/resource/resource_test.rb @@ -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))