From 4b9d7c2e9750ce1f45d7ec2495fddeef5bb5a73b 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 | 10 ++++++++++ 2 files changed, 17 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..5f386c020 100644 --- a/test/unit/resource/resource_test.rb +++ b/test/unit/resource/resource_test.rb @@ -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))