-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: clamp out-of-range long values to prevent indexing errors
Some clients send extremely large or small numeric values that Jackson deserializes as `BigInteger`. This causes a parsing error since OpenSearch expects them to be in the long range. To prevent indexing failures, this commit introduces a `ClampingLongConverter` that converts out-of-range `BigInteger` values to `Long.MAX_VALUE` or `Long.MIN_VALUE`, ensuring compatibility while preserving meaningful data.
- Loading branch information
Showing
7 changed files
with
80 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
src/main/kotlin/ch/srgssr/pillarbox/monitoring/event/repository/ClampingLongConverter.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package ch.srgssr.pillarbox.monitoring.event.repository | ||
|
||
import org.springframework.core.convert.converter.Converter | ||
import java.math.BigInteger | ||
|
||
/** | ||
* A converter that transforms a [BigInteger] into a [Long], ensuring that values | ||
* outside the range of [Long.MIN_VALUE] to [Long.MAX_VALUE] are clamped. | ||
*/ | ||
class ClampingLongConverter : Converter<BigInteger, Long> { | ||
companion object { | ||
private val MAX_LONG_AS_BIGINT = BigInteger.valueOf(Long.MAX_VALUE) | ||
private val MIN_LONG_AS_BIGINT = BigInteger.valueOf(Long.MIN_VALUE) | ||
} | ||
|
||
/** | ||
* Converts a given [BigInteger] to a [Long], clamping values that exceed the range of a `Long` type. | ||
* | ||
* @param value The [BigInteger] to convert. | ||
* | ||
* @return The equivalent [Long] value, clamped to [Long.MIN_VALUE] or [Long.MAX_VALUE] | ||
* if the input exceeds the representable range. | ||
*/ | ||
override fun convert(value: BigInteger): Long = | ||
when { | ||
value > MAX_LONG_AS_BIGINT -> Long.MAX_VALUE | ||
value < MIN_LONG_AS_BIGINT -> Long.MIN_VALUE | ||
else -> value.toLong() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
27 changes: 27 additions & 0 deletions
27
src/test/kotlin/ch/srgssr/pillarbox/monitoring/json/ClampingLongConverterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package ch.srgssr.pillarbox.monitoring.json | ||
|
||
import ch.srgssr.pillarbox.monitoring.event.repository.ClampingLongConverter | ||
import io.kotest.core.spec.style.ShouldSpec | ||
import io.kotest.matchers.shouldBe | ||
import java.math.BigInteger | ||
|
||
class ClampingLongConverterTest : | ||
ShouldSpec({ | ||
val converter = ClampingLongConverter() | ||
should("serialize BigInteger within Long range correctly") { | ||
val result = converter.convert(BigInteger.valueOf(123456789L)) | ||
result shouldBe 123456789 | ||
} | ||
|
||
should("clamp BigInteger larger than Long.MAX_VALUE to Long.MAX_VALUE") { | ||
val bigIntAboveMax = BigInteger.valueOf(Long.MAX_VALUE).add(BigInteger.ONE) | ||
val result = converter.convert(bigIntAboveMax) | ||
result shouldBe Long.MAX_VALUE | ||
} | ||
|
||
should("clamp BigInteger smaller than Long.MIN_VALUE to Long.MIN_VALUE") { | ||
val bigIntBelowMin = BigInteger.valueOf(Long.MIN_VALUE).subtract(BigInteger.ONE) | ||
val result = converter.convert(bigIntBelowMin) | ||
result shouldBe Long.MIN_VALUE | ||
} | ||
}) |