Skip to content

Commit 6369ccf

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

3 files changed

Lines changed: 30 additions & 2 deletions

File tree

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,10 @@ public final class CelNativeTypesExtensions implements CelCompilerLibrary, CelRu
8282
private static final ImmutableSet<String> OBJECT_METHOD_NAMES =
8383
stream(Object.class.getDeclaredMethods()).map(Method::getName).collect(toImmutableSet());
8484

85+
// Set of all standard java.lang.Enum method names.
86+
private static final ImmutableSet<String> ENUM_METHOD_NAMES =
87+
stream(Enum.class.getDeclaredMethods()).map(Method::getName).collect(toImmutableSet());
88+
8589
private static final ImmutableMap<Class<?>, CelType> JAVA_TO_CEL_TYPE_MAP =
8690
ImmutableMap.<Class<?>, CelType>builder()
8791
.put(boolean.class, SimpleType.BOOL)
@@ -603,7 +607,7 @@ private static boolean isGetter(Method method) {
603607
return false;
604608
}
605609
String name = method.getName();
606-
if (OBJECT_METHOD_NAMES.contains(name)) {
610+
if (OBJECT_METHOD_NAMES.contains(name) || ENUM_METHOD_NAMES.contains(name)) {
607611
return false;
608612
}
609613
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)