Skip to content

Commit 015a0bb

Browse files
Dishwasharichmolj
authored andcommitted
Sanitize single value filters with curlies (#134)
1 parent 048c95e commit 015a0bb

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

lib/jsonapi_compliable/scoping/filter.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -59,17 +59,18 @@ def each_filter
5959
end
6060

6161
# foo,bar,baz becomes ["foo", "bar", "baz"]
62+
# {{foo}} becomes ["foo"]
6263
# {{foo,bar}},baz becomes ["foo,bar", "baz"]
6364
def parse_string_arrays(value)
64-
if value.is_a?(String) && value.include?(',')
65-
# Fine the quoted strings
65+
if value.is_a?(String)
66+
# Find the quoted strings
6667
quotes = value.scan(/{{.*?}}/)
6768
# remove them from the rest
6869
quotes.each { |q| value.gsub!(q, '') }
6970
# remove the quote characters from the quoted strings
7071
quotes.each { |q| q.gsub!('{{', '').gsub!('}}', '') }
7172
# merge everything back together into an array
72-
value = value.split(',') + quotes
73+
value = Array(value.split(',')) + quotes
7374
# remove any blanks that are left
7475
value.reject! { |v| v.length.zero? }
7576
value = value[0] if value.length == 1

spec/filtering_spec.rb

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@
9393
author2.update_attribute(:first_name, 'foo,bar')
9494
end
9595

96-
# todo test dont convert to single el array
9796
it 'does not convert to array' do
9897
ids = scope.resolve.map(&:id)
9998
expect(ids).to eq([author2.id])
@@ -132,6 +131,38 @@
132131
end
133132
end
134133

134+
context 'when filter is a {{string}} without a comma' do
135+
before do
136+
params[:filter] = { first_name: '{{foo}}' }
137+
author2.update_attribute(:first_name, 'foo')
138+
end
139+
140+
it 'does not convert to array' do
141+
ids = scope.resolve.map(&:id)
142+
expect(ids).to eq([author2.id])
143+
end
144+
145+
it 'yields single element, not array' do
146+
query = Author.all
147+
expect(query).to receive(:where)
148+
.with(first_name: "foo").and_call_original
149+
allow(Author).to receive(:all) { query }
150+
scope.resolve
151+
end
152+
153+
context 'when an escaped string contains quoted strings' do
154+
before do
155+
params[:filter] = { first_name: '{{"foo"}}' }
156+
author2.update_attribute(:first_name, '"foo"')
157+
end
158+
159+
it 'works correctly' do
160+
ids = scope.resolve.map(&:id)
161+
expect(ids).to eq([author2.id])
162+
end
163+
end
164+
end
165+
135166
context 'when filter is an integer' do
136167
before do
137168
params[:filter] = { id: author1.id }

0 commit comments

Comments
 (0)