Skip to content

Commit 0e12371

Browse files
author
Lee Richmond
committed
Properly escape singular filters
0e2c4dd caused singular filters to avoid parsing the comma into an array. However, it accidentally also prevented escaping special characters. As a side effect, a `single: true` filter wrapped in `{{}}` curlies would incorrectly see the value passed in containing `{{}}`. This fixes things so singular filters still get parsed correctly, they just avoid the comma/array scenario as originally intended.
1 parent 5f65c15 commit 0e12371

File tree

2 files changed

+17
-5
lines changed

2 files changed

+17
-5
lines changed

lib/graphiti/scoping/filter.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,6 @@ def parse_hash_value(filter, param_value, value, operator)
151151
# JSON of
152152
# {{{ "id": 1 }}} becomes { 'id' => 1 }
153153
def parse_string_value(filter, value)
154-
return value if !!filter[:single]
155-
156154
type = Graphiti::Types[filter[:type]]
157155
array_or_string = [:string, :array].include?(type[:canonical_name])
158156
if (arr = value.scan(/\[.*?\]/)).present? && array_or_string
@@ -165,20 +163,24 @@ def parse_string_value(filter, value)
165163
}
166164
value = value[0] if value.length == 1
167165
else
168-
value = parse_string_arrays(value)
166+
value = parse_string_arrays(value, !!filter[:single])
169167
end
170168
value
171169
end
172170

173-
def parse_string_arrays(value)
171+
def parse_string_arrays(value, singular_filter)
174172
# Find the quoted strings
175173
quotes = value.scan(/{{.*?}}/)
176174
# remove them from the rest
177175
quotes.each { |q| value.gsub!(q, "") }
178176
# remove the quote characters from the quoted strings
179177
quotes.each { |q| q.gsub!("{{", "").gsub!("}}", "") }
180178
# merge everything back together into an array
181-
value = Array(value.split(",")) + quotes
179+
if singular_filter
180+
value = Array(value) + quotes
181+
else
182+
value = Array(value.split(",")) + quotes
183+
end
182184
# remove any blanks that are left
183185
value.reject! { |v| v.length.zero? }
184186
value = value[0] if value.length == 1

spec/filtering_spec.rb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,16 @@ def self.name
200200
expect(records.map(&:id)).to eq([employee2.id])
201201
end
202202
end
203+
204+
context 'and the filter is marked single: true' do
205+
before do
206+
resource.filter :first_name, :string, single: true
207+
end
208+
209+
it 'works' do
210+
expect(records.map(&:id)).to eq([employee2.id])
211+
end
212+
end
203213
end
204214

205215
context "when passed null and filter marked allow_nil: true" do

0 commit comments

Comments
 (0)