Skip to content

Commit

Permalink
Allow operator characters on the RHS of filter
Browse files Browse the repository at this point in the history
Fixes an issue where filters with operator characters on the RHS would
return 400s. This is because it parses the operator as the first one
found anywhere in the filter. By taking a more LALR-ish approach, we
can avoid this problem by parsing it as the first operator found in
the string.

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=1365013
  • Loading branch information
imtayadeway committed Jul 10, 2017
1 parent 35e4d83 commit 1a21138
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 1 deletion.
11 changes: 10 additions & 1 deletion lib/api/filter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,21 @@ def parse

def parse_filter(filter)
logical_or = filter.gsub!(/^or /i, '').present?
operator, methods = OPERATORS.find { |op, _methods| filter.partition(op).second == op }

operator = nil
operators_from_longest_to_shortest = OPERATORS.keys.sort_by(&:size).reverse
filter.size.times do |i|
operator = operators_from_longest_to_shortest.detect do |o|
o == filter[(i..(i + o.size - 1))]
end
break if operator
end

if operator.blank?
raise BadRequestError, "Unknown operator specified in filter #{filter}"
end

methods = OPERATORS[operator]
filter_attr, _, filter_value = filter.partition(operator)
filter_attr.strip!
filter_value.strip!
Expand Down
9 changes: 9 additions & 0 deletions spec/lib/api/filter_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -346,5 +346,14 @@
expected = {"=" => {"field" => "Vm-ems_id", "value" => 1_000_000_000_123}}
expect(actual.exp).to eq(expected)
end

it "can handle operator characters on the right hand side" do
filters = ["name=Vms with free space > 50 percent"]

actual = described_class.parse(filters, MiqReport)

expected = {"=" => {"field" => "MiqReport-name", "value" => "Vms with free space > 50 percent"}}
expect(actual.exp).to eq(expected)
end
end
end

0 comments on commit 1a21138

Please sign in to comment.