Skip to content

Commit 4e58e12

Browse files
l46kokcopybara-github
authored andcommitted
Safely ignore enums in native type extensions
Ref: #1077 PiperOrigin-RevId: 927414521
1 parent a6f2930 commit 4e58e12

3 files changed

Lines changed: 29 additions & 3 deletions

File tree

extensions/src/main/java/dev/cel/extensions/CelNativeTypesExtensions.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,10 +78,12 @@ public final class CelNativeTypesExtensions implements CelCompilerLibrary, CelRu
7878

7979
private final NativeTypeRegistry registry;
8080

81-
// Set of all standard java.lang.Object method names.
8281
private static final ImmutableSet<String> OBJECT_METHOD_NAMES =
8382
stream(Object.class.getDeclaredMethods()).map(Method::getName).collect(toImmutableSet());
8483

84+
private static final ImmutableSet<String> ENUM_METHOD_NAMES =
85+
stream(Enum.class.getDeclaredMethods()).map(Method::getName).collect(toImmutableSet());
86+
8587
private static final ImmutableMap<Class<?>, CelType> JAVA_TO_CEL_TYPE_MAP =
8688
ImmutableMap.<Class<?>, CelType>builder()
8789
.put(boolean.class, SimpleType.BOOL)
@@ -603,7 +605,7 @@ private static boolean isGetter(Method method) {
603605
return false;
604606
}
605607
String name = method.getName();
606-
if (OBJECT_METHOD_NAMES.contains(name)) {
608+
if (OBJECT_METHOD_NAMES.contains(name) || ENUM_METHOD_NAMES.contains(name)) {
607609
return false;
608610
}
609611
if (name.startsWith("get")) {

extensions/src/main/java/dev/cel/extensions/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,6 +1122,7 @@ The type-mapping between Java and CEL is as follows:
11221122

11231123
* This is only supported for the planner runtime (e.g., `CelRuntimeFactory.plannerRuntimeBuilder()`).
11241124
* Native Java arrays (except `byte[]`) are not supported. Use `java.util.List` instead.
1125+
* Java `enum` properties are not currently supported and will be safely ignored during scanning.
11251126
* If there is a name collision with a Protobuf type, the protobuf type will take precedence.
11261127
* Instantiating new struct values (e.g., `Account{id: 1234}`) requires the class to have a no-argument constructor (public, protected, package-private, or private).
11271128
* Final fields are supported only in a **read-only** capacity; they cannot be populated when instantiating new struct values.

extensions/src/test/java/dev/cel/extensions/CelNativeTypesExtensionsTest.java

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,8 @@ public final class CelNativeTypesExtensionsTest {
8989
TestNestedSimplePojo.class,
9090
TestGetterFieldTypeMismatchPojo.class,
9191
TestAbstractPojo.class,
92-
TestURLPojo.class);
92+
TestURLPojo.class,
93+
PojoWithEnum.class);
9394

9495
private static final Cel CEL =
9596
CelFactory.plannerCelBuilder()
@@ -1346,4 +1347,26 @@ public String getMismatchField() {
13461347
return "mismatch";
13471348
}
13481349
}
1350+
1351+
public enum TestEnum {
1352+
FOO,
1353+
BAR;
1354+
}
1355+
1356+
public static class PojoWithEnum {
1357+
private TestEnum enumVal = TestEnum.FOO;
1358+
1359+
public TestEnum getEnumVal() {
1360+
return enumVal;
1361+
}
1362+
1363+
public void setEnumVal(TestEnum val) {
1364+
this.enumVal = val;
1365+
}
1366+
}
1367+
1368+
@Test
1369+
public void nativeTypes_enumSafelyIgnored() throws Exception {
1370+
assertThat(eval("PojoWithEnum{}.enumVal")).isNotNull();
1371+
}
13491372
}

0 commit comments

Comments
 (0)