Skip to content

Commit 5103b30

Browse files
l46kokcopybara-github
authored andcommitted
Fix optionals to properly error on invalid qualification
PiperOrigin-RevId: 875937520
1 parent d499ba8 commit 5103b30

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

conformance/src/test/java/dev/cel/conformance/BUILD.bazel

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,8 +175,6 @@ _TESTS_TO_SKIP_PLANNER = [
175175
"timestamps/timestamp_range/sub_time_duration_under",
176176

177177
# Skip until fixed.
178-
"optionals/optionals/map_null_entry_no_such_key",
179-
"optionals/optionals/map_present_key_invalid_field",
180178
"parse/receiver_function_names",
181179
"proto2/extensions_get/package_scoped_test_all_types_ext",
182180
"proto2/extensions_get/package_scoped_repeated_test_all_types",

runtime/src/main/java/dev/cel/runtime/planner/StringQualifier.java

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
package dev.cel.runtime.planner;
1616

1717
import dev.cel.common.exceptions.CelAttributeNotFoundException;
18+
import dev.cel.common.values.OptionalValue;
1819
import dev.cel.common.values.SelectableValue;
1920
import java.util.Map;
2021

@@ -31,21 +32,34 @@ public String value() {
3132
@Override
3233
@SuppressWarnings("unchecked") // Qualifications on maps/structs must be a string
3334
public Object qualify(Object obj) {
35+
if (obj instanceof OptionalValue) {
36+
OptionalValue<?, ?> opt = (OptionalValue<?, ?>) obj;
37+
if (!opt.isZeroValue()) {
38+
Object inner = opt.value();
39+
if (!(inner instanceof SelectableValue) && !(inner instanceof Map)) {
40+
throw CelAttributeNotFoundException.forFieldResolution(value);
41+
}
42+
}
43+
}
44+
3445
if (obj instanceof SelectableValue) {
3546
return ((SelectableValue<String>) obj).select(value);
36-
} else if (obj instanceof Map) {
37-
Map<String, Object> map = (Map<String, Object>) obj;
38-
if (!map.containsKey(value)) {
39-
throw CelAttributeNotFoundException.forMissingMapKey(value);
40-
}
47+
}
4148

49+
if (obj instanceof Map) {
50+
Map<?, ?> map = (Map<?, ?>) obj;
4251
Object mapVal = map.get(value);
4352

44-
if (mapVal == null) {
45-
throw CelAttributeNotFoundException.of(
46-
String.format("Map value cannot be null for key: %s", value));
53+
if (mapVal != null) {
54+
return mapVal;
55+
}
56+
57+
if (!map.containsKey(value)) {
58+
throw CelAttributeNotFoundException.forMissingMapKey(value);
4759
}
48-
return map.get(value);
60+
61+
throw CelAttributeNotFoundException.of(
62+
String.format("Map value cannot be null for key: %s", value));
4963
}
5064

5165
throw CelAttributeNotFoundException.forFieldResolution(value);

0 commit comments

Comments
 (0)