Skip to content

Commit e442a3c

Browse files
committed
Escape query used in opportunity search. This addresses the reported issue "[Bug] Searching "C++" returns 500 error on /jobs".
Added the relevant tests to opportunity controller spec.
1 parent c66f950 commit e442a3c

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

app/controllers/opportunities_controller.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -166,14 +166,16 @@ def geocode_location(location)
166166
def get_jobs_for(chosen_location, tag, page, query = nil, remote_allowed = false)
167167
scope = Opportunity
168168

169+
escaped_query = query.nil? ? query : Regexp.escape(query)
170+
169171
if remote_allowed
170172
scope = scope.where(remote: true)
171173
else
172174
scope = scope.by_city(chosen_location) if chosen_location && chosen_location.length > 0
173175
end
174176

175177
scope = scope.by_tag(tag) unless tag.nil?
176-
scope = scope.by_query(query) if query
178+
scope = scope.by_query(escaped_query) if escaped_query
177179
# TODO: Verify that there are no unmigrated teams
178180
scope = scope.where('team_id is not null')
179181
scope.offset((page-1) * 20)

spec/controllers/opportunity_controlller_spec.rb

+11
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,17 @@
4646
expect(assigns(:jobs)).to_not be_include(@opportunity2)
4747
end
4848
end
49+
50+
context "by query with keywords containing regexp special characters" do
51+
it "should NOT fail when querying with keywords containing '+'" do
52+
get :index, location: nil, q: 'C++'
53+
expect(assigns(:jobs))
54+
end
55+
it "should NOT fail when querying with keywords containing '.^$*+?()[{\|'" do
56+
get :index, location: nil, q: 'java .^$*+?()[{\|'
57+
expect(assigns(:jobs))
58+
end
59+
end
4960
end
5061
end
5162
end

0 commit comments

Comments
 (0)