From 7ca02a323fea8b09b418d2b0939ad8b77710f2b0 Mon Sep 17 00:00:00 2001 From: Sean Huh Date: Thu, 12 Nov 2026 14:33:50 -0800 Subject: [PATCH] Adds conformance tests for setting numeric fields to numbers of another type. PiperOrigin-RevId: 1020449183 --- .../dev/cel/common/internal/ProtoAdapter.java | 49 ++++++++++++++----- .../cel/common/internal/ProtoAdapterTest.java | 16 ++++++ .../values/ProtoMessageValueProviderTest.java | 37 +++++++++++++- 3 files changed, 87 insertions(+), 15 deletions(-) diff --git a/common/src/main/java/dev/cel/common/internal/ProtoAdapter.java b/common/src/main/java/dev/cel/common/internal/ProtoAdapter.java index b872df831..73eb3d276 100644 --- a/common/src/main/java/dev/cel/common/internal/ProtoAdapter.java +++ b/common/src/main/java/dev/cel/common/internal/ProtoAdapter.java @@ -308,23 +308,21 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) { case SFIXED32: case SINT32: case INT32: - return unwrapAndConvert(INT_CONVERTER); + return unwrapAndConvert(fieldDescriptor, Long.class, INT_CONVERTER); case FIXED32: case UINT32: if (celOptions.enableUnsignedLongs()) { - return unwrapAndConvert(UNSIGNED_UINT32_CONVERTER); + return unwrapAndConvert(fieldDescriptor, UnsignedLong.class, UNSIGNED_UINT32_CONVERTER); } - return unwrapAndConvert(SIGNED_UINT32_CONVERTER); + return unwrapAndConvert(fieldDescriptor, Long.class, SIGNED_UINT32_CONVERTER); case FIXED64: case UINT64: if (celOptions.enableUnsignedLongs()) { - return unwrapAndConvert(UNSIGNED_UINT64_CONVERTER); + return unwrapAndConvert(fieldDescriptor, UnsignedLong.class, UNSIGNED_UINT64_CONVERTER); } - return BidiConverter.of( - BidiConverter.IDENTITY.forwardConverter(), - value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value))); + return unwrapAndCheck(fieldDescriptor, Long.class); case FLOAT: - return unwrapAndConvert(DOUBLE_CONVERTER); + return unwrapAndConvert(fieldDescriptor, Double.class, DOUBLE_CONVERTER); case BYTES: if (celOptions.evaluateCanonicalTypesToNativeValues()) { return BidiConverter.of( @@ -336,21 +334,24 @@ private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) { BidiConverter.IDENTITY.forwardConverter(), value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value))); case DOUBLE: + return unwrapAndCheck(fieldDescriptor, Double.class); case SFIXED64: case SINT64: case INT64: + return unwrapAndCheck(fieldDescriptor, Long.class); case STRING: case BOOL: return BidiConverter.of( BidiConverter.IDENTITY.forwardConverter(), value -> BidiConverter.IDENTITY.backwardConverter().convert(maybeUnwrap(value))); case ENUM: - return BidiConverter.of( + return BidiConverter.of( value -> (long) ((EnumValueDescriptor) value).getNumber(), - number -> + value -> fieldDescriptor .getEnumType() - .findValueByNumberCreatingIfUnknown(intCheckedCast(number))); + .findValueByNumberCreatingIfUnknown( + intCheckedCast((Long) unwrapNumber(fieldDescriptor, Long.class, value)))); case MESSAGE: return BidiConverter.of( this::adaptProtoToValue, @@ -434,9 +435,31 @@ private Object maybeUnwrap(Object value) { } private BidiConverter unwrapAndConvert( - final BidiConverter original) { + FieldDescriptor fieldDescriptor, Class celType, BidiConverter original) { return BidiConverter.of( original.forwardConverter()::convert, - value -> original.backwardConverter().convert((Number) maybeUnwrap(value))); + value -> + original + .backwardConverter() + .convert((Number) unwrapNumber(fieldDescriptor, celType, value))); + } + + private BidiConverter unwrapAndCheck( + FieldDescriptor fieldDescriptor, Class celType) { + return BidiConverter.of( + BidiConverter.IDENTITY.forwardConverter(), + value -> unwrapNumber(fieldDescriptor, celType, value)); + } + + private Object unwrapNumber(FieldDescriptor fieldDescriptor, Class celType, Object value) { + Object celValue = maybeUnwrap(value); + if ((celValue instanceof Long || celValue instanceof UnsignedLong || celValue instanceof Double) + && !celType.isInstance(celValue)) { + throw new IllegalArgumentException( + String.format( + "unexpected value type for field '%s' in message '%s'", + fieldDescriptor.getName(), fieldDescriptor.getContainingType().getFullName())); + } + return celValue; } } diff --git a/common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java b/common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java index be7e4f042..279787537 100644 --- a/common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java +++ b/common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java @@ -239,6 +239,22 @@ public void adaptValueToProto_unsignedLongsDisabled_acceptsSignedUintWrappers() .isEqualTo(UInt64Value.of(1)); } + @Test + @SuppressWarnings("deprecation") // Signed uints are only enabled through a deprecated option. + public void adaptValueToFieldType_unsignedLongsDisabled_acceptsSignedUints() { + ProtoAdapter protoAdapter = + new ProtoAdapter(DYNAMIC_PROTO, CelOptions.current().enableUnsignedLongs(false).build()); + + assertThat( + protoAdapter.adaptValueToFieldType( + UInt32Value.getDescriptor().findFieldByName("value"), 1L)) + .hasValue(1); + assertThat( + protoAdapter.adaptValueToFieldType( + UInt64Value.getDescriptor().findFieldByName("value"), 1L)) + .hasValue(1L); + } + @Test public void adaptValueToProto_asymmetricJsonConversion() { ProtoAdapter protoAdapter = new ProtoAdapter(DYNAMIC_PROTO, CelOptions.DEFAULT); diff --git a/common/src/test/java/dev/cel/common/values/ProtoMessageValueProviderTest.java b/common/src/test/java/dev/cel/common/values/ProtoMessageValueProviderTest.java index 106a6508a..00eca2635 100644 --- a/common/src/test/java/dev/cel/common/values/ProtoMessageValueProviderTest.java +++ b/common/src/test/java/dev/cel/common/values/ProtoMessageValueProviderTest.java @@ -20,6 +20,8 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.primitives.UnsignedLong; +import com.google.protobuf.UInt64Value; +import com.google.testing.junit.testparameterinjector.TestParameter; import com.google.testing.junit.testparameterinjector.TestParameterInjector; import com.google.testing.junit.testparameterinjector.TestParameters; import dev.cel.common.CelDescriptorUtil; @@ -113,8 +115,7 @@ public void newValue_createProtoMessage_unsignedLongFieldsPopulated() { VALUE_PROVIDER .newValue( TestAllTypes.getDescriptor().getFullName(), - ImmutableMap.of( - "single_uint32", 3, "single_uint64", UnsignedLong.MAX_VALUE.longValue())) + ImmutableMap.of("single_uint32", 3, "single_uint64", UnsignedLong.MAX_VALUE)) .get(); assertThat(protoMessageValue.isZeroValue()).isFalse(); @@ -194,6 +195,38 @@ public void newValue_invalidField_throws() { + " 'cel.expr.conformance.proto2.TestAllTypes'"); } + @SuppressWarnings("ImmutableEnumChecker") // Test only + private enum MismatchedNumericTypeTestCase { + INT_TO_UINT32("single_uint32", 1L), + INT_TO_FLOAT("single_float", 1L), + INT_TO_MAP_DOUBLE_VALUE("map_bool_double", ImmutableMap.of(true, 1L)), + UINT_TO_ENUM("standalone_enum", UnsignedLong.valueOf(1L)), + UINT_TO_MAP_INT32_KEY("map_int32_bool", ImmutableMap.of(UnsignedLong.valueOf(1L), true)), + UINT_WRAPPER_TO_INT64("single_int64", UInt64Value.of(1L)), + DOUBLE_TO_REPEATED_UINT64("repeated_uint64", ImmutableList.of(1.0d)); + + private final String fieldName; + private final Object value; + + MismatchedNumericTypeTestCase(String fieldName, Object value) { + this.fieldName = fieldName; + this.value = value; + } + } + + @Test + public void newValue_mismatchedNumericType_throws( + @TestParameter MismatchedNumericTypeTestCase testCase) { + String typeName = TestAllTypes.getDescriptor().getFullName(); + ImmutableMap fields = ImmutableMap.of(testCase.fieldName, testCase.value); + + IllegalArgumentException e = + assertThrows( + IllegalArgumentException.class, () -> VALUE_PROVIDER.newValue(typeName, fields)); + + assertThat(e).hasMessageThat().contains("unexpected value type for field"); + } + @Test public void newValue_onCombinedProvider() { CelValueProvider celValueProvider = (structType, fields) -> Optional.empty();