From f714daad0637169e8160075b49c0323df270c019 Mon Sep 17 00:00:00 2001 From: Sean Huh Date: Wed, 14 Oct 2026 12:18:45 -0700 Subject: [PATCH] Build ProtoMessageValue's CelType on demand PiperOrigin-RevId: 999534382 --- .../cel/common/values/ProtoMessageValue.java | 21 ++++++++++++------- .../common/values/ProtoMessageValueTest.java | 13 ++++++++++++ 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/common/src/main/java/dev/cel/common/values/ProtoMessageValue.java b/common/src/main/java/dev/cel/common/values/ProtoMessageValue.java index 35331cdfc..fa3864ad2 100644 --- a/common/src/main/java/dev/cel/common/values/ProtoMessageValue.java +++ b/common/src/main/java/dev/cel/common/values/ProtoMessageValue.java @@ -35,20 +35,29 @@ public abstract class ProtoMessageValue extends StructValue @Override public abstract Message value(); - @Override - public abstract CelType celType(); - abstract CelDescriptorPool celDescriptorPool(); abstract ProtoCelValueConverter protoCelValueConverter(); abstract boolean enableJsonFieldNames(); + @Override + public CelType celType() { + // Built on demand, since one of these values is created on every hop but rarely asked its type. + return StructTypeReference.create(value().getDescriptorForType().getFullName()); + } + @Override public boolean isZeroValue() { return value().getDefaultInstanceForType().equals(value()); } + @Override + public final String toString() { + // AutoValue's would print the message's contents, which may be sensitive. + return "ProtoMessageValue{protoTypeName=" + value().getDescriptorForType().getFullName() + "}"; + } + @Override public Object select(String field) { FieldDescriptor fieldDescriptor = @@ -95,11 +104,7 @@ public static ProtoMessageValue create( checkNotNull(celDescriptorPool); checkNotNull(protoCelValueConverter); return new AutoValue_ProtoMessageValue( - value, - StructTypeReference.create(value.getDescriptorForType().getFullName()), - celDescriptorPool, - protoCelValueConverter, - enableJsonFieldNames); + value, celDescriptorPool, protoCelValueConverter, enableJsonFieldNames); } private Optional findFieldValue(FieldDescriptor fieldDescriptor) { diff --git a/common/src/test/java/dev/cel/common/values/ProtoMessageValueTest.java b/common/src/test/java/dev/cel/common/values/ProtoMessageValueTest.java index a718af143..5524e363e 100644 --- a/common/src/test/java/dev/cel/common/values/ProtoMessageValueTest.java +++ b/common/src/test/java/dev/cel/common/values/ProtoMessageValueTest.java @@ -426,6 +426,19 @@ public void celTypeTest() { .isEqualTo(StructTypeReference.create(TestAllTypes.getDescriptor().getFullName())); } + @Test + public void toString_returnsTypeNameOnly() { + ProtoMessageValue protoMessageValue = + ProtoMessageValue.create( + TestAllTypes.newBuilder().setSingleString("secret").build(), + DefaultDescriptorPool.INSTANCE, + PROTO_CEL_VALUE_CONVERTER, + /* enableJsonFieldNames= */ false); + + assertThat(protoMessageValue.toString()) + .isEqualTo("ProtoMessageValue{protoTypeName=cel.expr.conformance.proto2.TestAllTypes}"); + } + @Test public void findField_jsonName_success() { TestAllTypes testAllTypes = TestAllTypes.newBuilder().setSingleInt32(42).build();