Skip to content

Commit

Permalink
Merge pull request #15534 from imtayadeway/bug/api-filter-operators-o…
Browse files Browse the repository at this point in the history
…n-rhs

Allow operator characters on the RHS of filter
(cherry picked from commit d9cd967)

https://bugzilla.redhat.com/show_bug.cgi?id=1481296
  • Loading branch information
abellotti authored and simaishi committed Aug 14, 2017
1 parent 070c246 commit f5a310f
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 @@ -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!
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 f5a310f

Please sign in to comment.