From 32823e6341aec3e82a18499759eaeb7d2f1fad22 Mon Sep 17 00:00:00 2001 From: Rashen Anupama Date: Mon, 2 Nov 2026 00:40:32 +0530 Subject: [PATCH] refactor(core): share JSON integer coercion (#7213) --- .../floci/core/common/JsonNumberCoercion.java | 49 +++++++++++++ .../cloudtrail/CloudTrailJsonHandler.java | 12 +--- .../services/lambdaweb/LambdaWebJson.java | 11 +-- .../floci/services/ses/SesV2Json.java | 8 +-- .../services/ssm/SsmJsonDeserialization.java | 10 +-- .../core/common/JsonNumberCoercionTest.java | 72 +++++++++++++++++++ .../services/lambdaweb/LambdaWebJsonTest.java | 43 +++++++++++ 7 files changed, 175 insertions(+), 30 deletions(-) create mode 100644 src/main/java/io/github/hectorvent/floci/core/common/JsonNumberCoercion.java create mode 100644 src/test/java/io/github/hectorvent/floci/core/common/JsonNumberCoercionTest.java create mode 100644 src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebJsonTest.java diff --git a/src/main/java/io/github/hectorvent/floci/core/common/JsonNumberCoercion.java b/src/main/java/io/github/hectorvent/floci/core/common/JsonNumberCoercion.java new file mode 100644 index 0000000000..edaf472208 --- /dev/null +++ b/src/main/java/io/github/hectorvent/floci/core/common/JsonNumberCoercion.java @@ -0,0 +1,49 @@ +package io.github.hectorvent.floci.core.common; + +import com.fasterxml.jackson.databind.JsonNode; + +import java.math.BigDecimal; + +/** Numeric conversions shared by JSON protocol deserializers. */ +public final class JsonNumberCoercion { + + private static final BigDecimal INT_MAX = BigDecimal.valueOf(Integer.MAX_VALUE); + private static final BigDecimal INT_MIN = BigDecimal.valueOf(Integer.MIN_VALUE); + + private JsonNumberCoercion() { + } + + /** + * Narrows a JSON number to an {@code int}, truncating fractions toward zero and saturating at + * the integer bounds. This matches Coral's truncating integer deserializer. Non-finite Jackson + * number nodes are handled before {@link JsonNode#decimalValue()} because that conversion can + * throw for infinities produced by extremely large exponents. + * + *

Callers remain responsible for validating the member type and producing protocol-specific + * errors before calling this method. + * + * @throws IllegalArgumentException if {@code value} is not a numeric node + */ + public static int truncateAndClampToInt(JsonNode value) { + if (value == null || !value.isNumber()) { + throw new IllegalArgumentException("Expected a JSON number"); + } + + double doubleValue = value.doubleValue(); + if (!Double.isFinite(doubleValue)) { + if (Double.isNaN(doubleValue)) { + return 0; + } + return doubleValue > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE; + } + + BigDecimal decimalValue = value.decimalValue(); + if (decimalValue.compareTo(INT_MAX) >= 0) { + return Integer.MAX_VALUE; + } + if (decimalValue.compareTo(INT_MIN) <= 0) { + return Integer.MIN_VALUE; + } + return decimalValue.intValue(); + } +} diff --git a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailJsonHandler.java b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailJsonHandler.java index adb992c0b9..b366e11561 100644 --- a/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailJsonHandler.java +++ b/src/main/java/io/github/hectorvent/floci/services/cloudtrail/CloudTrailJsonHandler.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.databind.node.ArrayNode; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.EpochSeconds; +import io.github.hectorvent.floci.core.common.JsonNumberCoercion; import io.github.hectorvent.floci.services.cloudtrail.model.AdvancedEventSelector; import io.github.hectorvent.floci.services.cloudtrail.model.DataResource; import io.github.hectorvent.floci.services.cloudtrail.model.EventSelector; @@ -14,7 +15,6 @@ import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.core.Response; -import java.math.BigDecimal; import java.util.ArrayList; import java.util.LinkedHashMap; import java.util.List; @@ -256,15 +256,7 @@ public class CloudTrailJsonHandler { } int value; if (maxResults.isNumber()) { - double doubleValue = maxResults.doubleValue(); - if (maxResults.isFloatingPointNumber() && !Double.isFinite(doubleValue)) { - value = doubleValue > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE; - } else { - BigDecimal number = maxResults.decimalValue(); - value = number.compareTo(BigDecimal.valueOf(Integer.MIN_VALUE)) < 0 - ? Integer.MIN_VALUE : number.compareTo(BigDecimal.valueOf(Integer.MAX_VALUE)) > 0 - ? Integer.MAX_VALUE : number.intValue(); - } + value = JsonNumberCoercion.truncateAndClampToInt(maxResults); } else if (maxResults.isTextual()) { throw serialization("STRING_VALUE can not be converted to an Integer"); } else if (maxResults.isArray()) { diff --git a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebJson.java b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebJson.java index 9c73bf109d..0d6cc258f0 100644 --- a/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebJson.java +++ b/src/main/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebJson.java @@ -4,6 +4,7 @@ import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.JsonNumberCoercion; import java.util.ArrayList; import java.util.LinkedHashMap; @@ -53,14 +54,8 @@ final class LambdaWebJson { if (value == null) { return null; } - if (value.isIntegralNumber()) { - if (value.canConvertToInt()) { - return value.intValue(); - } - return value.bigIntegerValue().signum() > 0 ? Integer.MAX_VALUE : Integer.MIN_VALUE; - } - if (value.isFloatingPointNumber()) { - return (int) value.doubleValue(); + if (value.isNumber()) { + return JsonNumberCoercion.truncateAndClampToInt(value); } throw serialization(scalarToken(value) + " can not be converted to an Integer"); } diff --git a/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java b/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java index 6be0fde66d..51d8a2d971 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java +++ b/src/main/java/io/github/hectorvent/floci/services/ses/SesV2Json.java @@ -7,6 +7,7 @@ import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.node.ObjectNode; import io.github.hectorvent.floci.core.common.AwsException; import io.github.hectorvent.floci.core.common.EpochSeconds; +import io.github.hectorvent.floci.core.common.JsonNumberCoercion; import io.github.hectorvent.floci.core.common.SmithyValidationErrors; import io.github.hectorvent.floci.services.ses.model.Tag; @@ -310,9 +311,8 @@ final class SesV2Json { } // Integer variant of stringMemberOrAbsent: absent/null returns null and a string, a boolean, an - // object or a list gets the deserializer's message for it. A number is narrowed to an int: AWS - // accepts 1.5 and range-checks 4294967296 as 2147483647, which a double-to-int cast reproduces - // where intValue would truncate 4294967296 to 0. + // object or a list gets the deserializer's message for it. Numeric narrowing is shared with + // other JSON protocol deserializers. static Integer intMemberOrAbsent(JsonNode parent, String field) { JsonNode n = parent.path(field); if (n.isMissingNode() || n.isNull()) { @@ -328,7 +328,7 @@ final class SesV2Json { if (n.isContainerNode()) { throw unexpectedStartError(n); } - return (int) n.doubleValue(); + return JsonNumberCoercion.truncateAndClampToInt(n); } static String readRequiredStringField(JsonNode request, String fieldName) { diff --git a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonDeserialization.java b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonDeserialization.java index df5c6db607..8f64910de9 100644 --- a/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonDeserialization.java +++ b/src/main/java/io/github/hectorvent/floci/services/ssm/SsmJsonDeserialization.java @@ -2,6 +2,7 @@ package io.github.hectorvent.floci.services.ssm; import com.fasterxml.jackson.databind.JsonNode; import io.github.hectorvent.floci.core.common.AwsException; +import io.github.hectorvent.floci.core.common.JsonNumberCoercion; final class SsmJsonDeserialization { private SsmJsonDeserialization() { @@ -14,14 +15,7 @@ final class SsmJsonDeserialization { if (!node.isNumber() || Double.isNaN(node.doubleValue())) { throw serializationException(node, "an Integer"); } - double value = node.doubleValue(); - if (value >= Integer.MAX_VALUE) { - return Integer.MAX_VALUE; - } - if (value <= Integer.MIN_VALUE) { - return Integer.MIN_VALUE; - } - return node.intValue(); + return JsonNumberCoercion.truncateAndClampToInt(node); } static AwsException serializationException(JsonNode node, String targetDescription) { diff --git a/src/test/java/io/github/hectorvent/floci/core/common/JsonNumberCoercionTest.java b/src/test/java/io/github/hectorvent/floci/core/common/JsonNumberCoercionTest.java new file mode 100644 index 0000000000..61ad0cb9d9 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/core/common/JsonNumberCoercionTest.java @@ -0,0 +1,72 @@ +package io.github.hectorvent.floci.core.common; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.node.DoubleNode; +import org.junit.jupiter.api.Test; + +import java.util.List; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class JsonNumberCoercionTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static JsonNode json(String value) { + try { + return MAPPER.readTree(value); + } catch (Exception e) { + throw new AssertionError(e); + } + } + + @Test + void truncatesFractionsTowardZero() { + assertEquals(1, JsonNumberCoercion.truncateAndClampToInt(json("1.9"))); + assertEquals(-1, JsonNumberCoercion.truncateAndClampToInt(json("-1.9"))); + assertEquals(0, JsonNumberCoercion.truncateAndClampToInt(json("-0.9"))); + } + + @Test + void preservesInRangeValuesAndClampsIntegerBounds() { + assertEquals(Integer.MAX_VALUE, + JsonNumberCoercion.truncateAndClampToInt(json("2147483647"))); + assertEquals(Integer.MIN_VALUE, + JsonNumberCoercion.truncateAndClampToInt(json("-2147483648"))); + assertEquals(Integer.MAX_VALUE, + JsonNumberCoercion.truncateAndClampToInt(json("2147483648"))); + assertEquals(Integer.MIN_VALUE, + JsonNumberCoercion.truncateAndClampToInt(json("-2147483649"))); + } + + @Test + void clampsHugeIntegerAndExponentValues() { + assertEquals(Integer.MAX_VALUE, + JsonNumberCoercion.truncateAndClampToInt(json("92233720368547758081234567890"))); + assertEquals(Integer.MIN_VALUE, + JsonNumberCoercion.truncateAndClampToInt(json("-92233720368547758081234567890"))); + assertEquals(Integer.MAX_VALUE, JsonNumberCoercion.truncateAndClampToInt(json("1e400"))); + assertEquals(Integer.MIN_VALUE, JsonNumberCoercion.truncateAndClampToInt(json("-1e400"))); + } + + @Test + void handlesNonFiniteNumericNodesWithoutDecimalConversion() { + assertEquals(Integer.MAX_VALUE, + JsonNumberCoercion.truncateAndClampToInt(DoubleNode.valueOf(Double.POSITIVE_INFINITY))); + assertEquals(Integer.MIN_VALUE, + JsonNumberCoercion.truncateAndClampToInt(DoubleNode.valueOf(Double.NEGATIVE_INFINITY))); + assertEquals(0, JsonNumberCoercion.truncateAndClampToInt(DoubleNode.valueOf(Double.NaN))); + } + + @Test + void rejectsNonNumericNodes() { + for (String input : List.of("\"1\"", "true", "null", "[]", "{}")) { + assertThrows(IllegalArgumentException.class, + () -> JsonNumberCoercion.truncateAndClampToInt(json(input)), input); + } + assertThrows(IllegalArgumentException.class, + () -> JsonNumberCoercion.truncateAndClampToInt(null)); + } +} diff --git a/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebJsonTest.java b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebJsonTest.java new file mode 100644 index 0000000000..a3e22256e4 --- /dev/null +++ b/src/test/java/io/github/hectorvent/floci/services/lambdaweb/LambdaWebJsonTest.java @@ -0,0 +1,43 @@ +package io.github.hectorvent.floci.services.lambdaweb; + +import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.ObjectMapper; +import io.github.hectorvent.floci.core.common.AwsException; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; +import static org.junit.jupiter.api.Assertions.assertThrows; + +class LambdaWebJsonTest { + + private static final ObjectMapper MAPPER = new ObjectMapper(); + + private static JsonNode json(String value) { + try { + return MAPPER.readTree(value); + } catch (Exception e) { + throw new AssertionError(e); + } + } + + @Test + void integerTruncatesAndSaturatesNumericMembers() { + JsonNode request = json("{\"fraction\":-1.9,\"large\":9223372036854775808123,\"null\":null}"); + + assertEquals(-1, LambdaWebJson.integer(request, "fraction")); + assertEquals(Integer.MAX_VALUE, LambdaWebJson.integer(request, "large")); + assertNull(LambdaWebJson.integer(request, "null")); + assertNull(LambdaWebJson.integer(request, "missing")); + } + + @Test + void integerKeepsSerializationErrorForNonNumbers() { + AwsException exception = assertThrows(AwsException.class, + () -> LambdaWebJson.integer(json("{\"count\":\"1\"}"), "count")); + + assertEquals("SerializationException", exception.getErrorCode()); + assertEquals(400, exception.getHttpStatus()); + assertEquals("STRING_VALUE can not be converted to an Integer", exception.getMessage()); + } +}