diff --git a/lib/api/filter.rb b/lib/api/filter.rb index 4faa4496052b..f252557b0f9d 100644 --- a/lib/api/filter.rb +++ b/lib/api/filter.rb @@ -52,12 +52,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! diff --git a/spec/lib/api/filter_spec.rb b/spec/lib/api/filter_spec.rb index da5315ecad54..71019286129e 100644 --- a/spec/lib/api/filter_spec.rb +++ b/spec/lib/api/filter_spec.rb @@ -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