Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 36 additions & 13 deletions common/src/main/java/dev/cel/common/internal/ProtoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.<Object, Object>of(
Expand All @@ -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.<Object, Long>of(
return BidiConverter.<Object, Object>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.<MessageOrBuilder, Object>of(
this::adaptProtoToValue,
Expand Down Expand Up @@ -434,9 +435,31 @@ private Object maybeUnwrap(Object value) {
}

private BidiConverter<Number, Object> unwrapAndConvert(
final BidiConverter<Number, Number> original) {
FieldDescriptor fieldDescriptor, Class<?> celType, BidiConverter<Number, Number> 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<Object, Object> 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;
}
}
16 changes: 16 additions & 0 deletions common/src/test/java/dev/cel/common/internal/ProtoAdapterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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<String, Object> 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();
Expand Down
Loading