Skip to content

Commit

Permalink
Numerics shouldn't be tested for ".bytes" like suffix
Browse files Browse the repository at this point in the history
Symbols or other non-strings should be converted to strings before testing the regex.

Fixes ManageIQ/manageiq-ui-classic#9357
  • Loading branch information
jrafanie committed Feb 20, 2025
1 parent 99489ca commit 11a32fb
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
3 changes: 2 additions & 1 deletion lib/miq_expression.rb
Original file line number Diff line number Diff line change
Expand Up @@ -804,9 +804,10 @@ def self.quote(val, typ)
def self.quote_human(val, typ)
case typ&.to_sym
when :integer, :decimal, :fixnum, :float
return val if val.kind_of?(Numeric)
return val.to_i unless val.to_s.number_with_method? || typ == :float

if val =~ /^([0-9.,]+)\.([a-z]+)$/
if val.to_s =~ /^([0-9.,]+)\.([a-z]+)$/
val, sfx = $1, $2
if sfx.ends_with?("bytes") && FORMAT_BYTE_SUFFIXES.key?(sfx.to_sym)
"#{val} #{FORMAT_BYTE_SUFFIXES[sfx.to_sym]}"
Expand Down
10 changes: 10 additions & 0 deletions spec/lib/miq_expression_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2423,6 +2423,16 @@
expect(exp.to_human).to eq("COUNT OF Snaps > 1")
end

it "generates a human readable string for a numeric FIELD value expression" do
exp = MiqExpression.new(">" => {"field" => "Vm.hardware.volumes-free_space_percent", "value" => 75})
expect(exp.to_human).to eq("VM and Instance.Hardware.Volumes : Free Space Percent > 75")
end

it "generates a human readable string for a symbol FIELD value expression" do
exp = MiqExpression.new(">" => {"field" => "Vm-allocated_disk_storage", "value" => :"5.megabytes"})
expect(exp.to_human).to eq("VM and Instance : Allocated Disk Storage > 5 MB")
end

context "TAG type expression" do
before do
# tags contain the root tenant's name
Expand Down

0 comments on commit 11a32fb

Please sign in to comment.