[Posts] Fix searches for huge ids erroring

This is because they can exceed Integer.MAX_VALUE on the opensearch side
This commit is contained in:
Earlopain 2024-02-28 17:25:54 +01:00
parent 7c03dddc56
commit 17b99821e3
No known key found for this signature in database
GPG Key ID: 48860312319ADF61
3 changed files with 22 additions and 4 deletions

View File

@ -1,6 +1,8 @@
# frozen_string_literal: true
module ParseValue
MAX_INT = 2_147_483_647
MIN_INT = -2_147_483_648
extend self
def date_range(target)
@ -23,8 +25,8 @@ module ParseValue
def range_fudged(range, type)
result = range(range, type)
if result[0] == :eq
new_min = [(result[1] * 0.95).to_i, -2_147_483_648].max
new_max = [(result[1] * 1.05).to_i, 2_147_483_647].min
new_min = [(result[1] * 0.95).to_i, MIN_INT].max
new_max = [(result[1] * 1.05).to_i, MAX_INT].min
[:between, new_min, new_max]
else
result
@ -83,10 +85,11 @@ module ParseValue
def cast(object, type)
case type
when :integer
object.to_i
object.to_i.clamp(MIN_INT, MAX_INT)
when :float
object.to_f
# Floats obviously have a different range but this is good enough
object.to_f.clamp(MIN_INT, MAX_INT)
when :date, :datetime
case object

View File

@ -21,6 +21,17 @@ class ParseValueTest < ActiveSupport::TestCase
assert_equal([:in, [8, 9, 10, 11, 12]], subject.range("8,9,10,11,12"))
end
should "parse negative values" do
assert_equal([:lt, -1], subject.range("<-1"))
end
should "clamp huge values" do
assert_equal(ParseValue::MAX_INT, eq_value("1234567890987654321", :integer))
assert_equal(ParseValue::MIN_INT, eq_value("-1234567890987654321", :integer))
assert_equal(ParseValue::MAX_INT, eq_value("123456789098765432.1", :float))
assert_equal(ParseValue::MIN_INT, eq_value("-123456789098765432.1", :float))
end
should "parse floats" do
assert_equal(10.0, eq_value("10", :float))
assert_equal(0.1, eq_value(".1", :float))

View File

@ -1977,6 +1977,10 @@ class PostTest < ActiveSupport::TestCase
assert_tag_match([], "pending_replacements:true")
assert_tag_match([promoted_post, post4, post3, post2, post1], "pending_replacements:false")
end
should "not error for values beyond Integer.MAX_VALUE" do
assert_tag_match([], "id:1234567890987654321")
end
end
context "Voting:" do