Skip to content
Open
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
31 changes: 27 additions & 4 deletions common/src/main/java/dev/cel/common/internal/ProtoAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -222,9 +222,7 @@ public Optional<Object> adaptValueToFieldType(
throw new IllegalArgumentException("Unsupported field type");
}

String typeFullName = fieldDescriptor.getMessageType().getFullName();
if (!WellKnownProto.ANY_VALUE.typeName().equals(typeFullName)
&& !WellKnownProto.JSON_VALUE.typeName().equals(typeFullName)) {
if (!isAnyOrJsonValue(fieldDescriptor)) {
return Optional.empty();
}
}
Expand All @@ -242,7 +240,11 @@ public Optional<Object> adaptValueToFieldType(
getDefaultValueForMaybeMessage(keyDescriptor),
valueDescriptor.getLiteType(),
getDefaultValueForMaybeMessage(valueDescriptor));
boolean isValueAnyOrJson = isAnyOrJsonValue(valueDescriptor);
for (Map.Entry entry : ((Map<?, ?>) fieldValue).entrySet()) {
if (!isValueAnyOrJson && entry.getValue() instanceof NullValue) {
continue;
}
mapEntries.add(
protoMapEntry.toBuilder()
.setKey(keyConverter.backwardConverter().convert(entry.getKey()))
Expand All @@ -252,15 +254,36 @@ public Optional<Object> adaptValueToFieldType(
return Optional.of(mapEntries);
}
if (fieldDescriptor.isRepeated()) {
List<?> listValue = (List<?>) fieldValue;
if (listValue.contains(NullValue.NULL_VALUE)) {
if (!isAnyOrJsonValue(fieldDescriptor)) {
List<Object> filteredList = new ArrayList<>(listValue.size());
for (Object elem : listValue) {
if (!(elem instanceof NullValue)) {
filteredList.add(elem);
}
}
listValue = filteredList;
}
}
return Optional.of(
AdaptingTypes.adaptingList(
(List<?>) fieldValue, fieldToValueConverter(fieldDescriptor).reverse()));
listValue, fieldToValueConverter(fieldDescriptor).reverse()));
}

return Optional.of(
fieldToValueConverter(fieldDescriptor).backwardConverter().convert(fieldValue));
}

private boolean isAnyOrJsonValue(FieldDescriptor fieldDescriptor) {
String typeFullName =
fieldDescriptor.getJavaType() == FieldDescriptor.JavaType.MESSAGE
? fieldDescriptor.getMessageType().getFullName()
: "";
return WellKnownProto.ANY_VALUE.typeName().equals(typeFullName)
|| WellKnownProto.JSON_VALUE.typeName().equals(typeFullName);
}

@SuppressWarnings("rawtypes")
private BidiConverter fieldToValueConverter(FieldDescriptor fieldDescriptor) {
switch (fieldDescriptor.getType()) {
Expand Down
29 changes: 29 additions & 0 deletions runtime/src/test/resources/nullAssignability.baseline
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,32 @@ Source: has(TestAllTypes{single_timestamp: null}.single_timestamp)
bindings: {}
result: false

Source: TestAllTypes{repeated_timestamp: [timestamp(1), null]}.repeated_timestamp == [timestamp(1)]
=====>
bindings: {}
result: true

Source: TestAllTypes{map_bool_timestamp: {true: null, false: timestamp(1)}}.map_bool_timestamp == {false: timestamp(1)}
=====>
bindings: {}
result: true

Source: TestAllTypes{repeated_any: [1, null]}.repeated_any == [1, null]
=====>
bindings: {}
result: true

Source: TestAllTypes{map_bool_any: {true: null, false: 1}}.map_bool_any == {true: null, false: 1}
=====>
bindings: {}
result: true

Source: TestAllTypes{repeated_value: [google.protobuf.Value{bool_value: true}, null]}.repeated_value == [true, null]
=====>
bindings: {}
result: true

Source: TestAllTypes{map_bool_value: {true: null, false: google.protobuf.Value{bool_value: true}}}.map_bool_value == {true: null, false: true}
=====>
bindings: {}
result: true
27 changes: 27 additions & 0 deletions testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2145,6 +2145,33 @@ public void nullAssignability() throws Exception {

source = "has(TestAllTypes{single_timestamp: null}.single_timestamp)";
runTest();

source =
"TestAllTypes{repeated_timestamp: [timestamp(1), null]}.repeated_timestamp =="
+ " [timestamp(1)]";
runTest();

source =
"TestAllTypes{map_bool_timestamp: {true: null, false: timestamp(1)}}.map_bool_timestamp =="
+ " {false: timestamp(1)}";
runTest();

source = "TestAllTypes{repeated_any: [1, null]}.repeated_any == [1, null]";
runTest();

source =
"TestAllTypes{map_bool_any: {true: null, false: 1}}.map_bool_any == {true: null, false: 1}";
runTest();

source =
"TestAllTypes{repeated_value: [google.protobuf.Value{bool_value: true},"
+ " null]}.repeated_value == [true, null]";
runTest();

source =
"TestAllTypes{map_bool_value: {true: null, false: google.protobuf.Value{bool_value:"
+ " true}}}.map_bool_value == {true: null, false: true}";
runTest();
}

@Test
Expand Down
Loading