mirror of
https://github.com/floci-io/floci.git
synced 2026-11-27 07:39:24 -05:00
refactor(core): share JSON integer coercion (#7213)
This commit is contained in:
1 parent
0d6bbe9662
commit
32823e6341
7 files changed
+175
-30
No files matched your search
@@ -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.
|
||||
*
|
||||
* <p>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();
|
||||
}
|
||||
}
|
||||
+2
-10
@@ -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()) {
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in new issue
Block a user