From 27d70f84a17b7a8133068e41036fd88e5babe532 Mon Sep 17 00:00:00 2001 From: CEL Dev Team Date: Mon, 19 Oct 2026 12:14:04 -0700 Subject: [PATCH] Internal Changes PiperOrigin-RevId: 1002506123 --- .../java/dev/cel/common/values/BUILD.bazel | 1 + .../values/ProtoLiteCelValueConverter.java | 6 ++++++ .../values/RawProtoMessageLiteValue.java | 8 +++++-- .../values/RawProtoMessageLiteValueTest.java | 18 ++++++++-------- .../CelLiteRuntimeVersionSkewTest.java | 13 +++--------- .../dev/cel/runtime/RuntimeEqualityTest.java | 21 ++++++++----------- 6 files changed, 34 insertions(+), 33 deletions(-) diff --git a/common/src/main/java/dev/cel/common/values/BUILD.bazel b/common/src/main/java/dev/cel/common/values/BUILD.bazel index 70e2d79da..774f720e4 100644 --- a/common/src/main/java/dev/cel/common/values/BUILD.bazel +++ b/common/src/main/java/dev/cel/common/values/BUILD.bazel @@ -345,6 +345,7 @@ java_library( "//protobuf:cel_lite_descriptor", "@maven//:com_google_errorprone_error_prone_annotations", "@maven//:com_google_guava_guava", + "@maven//:com_google_protobuf_protobuf_java", "@maven//:org_jspecify_jspecify", "@maven_android//:com_google_protobuf_protobuf_javalite", ], diff --git a/common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java b/common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java index 5653e3760..248bf728d 100644 --- a/common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java +++ b/common/src/main/java/dev/cel/common/values/ProtoLiteCelValueConverter.java @@ -22,6 +22,7 @@ import com.google.common.collect.Iterables; import com.google.common.primitives.UnsignedLong; import com.google.errorprone.annotations.Immutable; +import com.google.protobuf.Any; import com.google.protobuf.ByteString; import com.google.protobuf.CodedInputStream; import com.google.protobuf.ExtensionRegistryLite; @@ -215,6 +216,11 @@ private Object toRuntimeValue(MessageLite msg, MessageLiteDescriptor descriptor) return ProtoMessageLiteValue.create(msg, descriptor, this); } + if (wellKnownProto == WellKnownProto.ANY_VALUE) { + Any any = (Any) msg; + return RawProtoMessageLiteValue.create(any.getValue(), this); + } + return fromWellKnownProto(msg, checkNotNull(wellKnownProto)); } diff --git a/common/src/main/java/dev/cel/common/values/RawProtoMessageLiteValue.java b/common/src/main/java/dev/cel/common/values/RawProtoMessageLiteValue.java index 81643d46c..7eda88e8d 100644 --- a/common/src/main/java/dev/cel/common/values/RawProtoMessageLiteValue.java +++ b/common/src/main/java/dev/cel/common/values/RawProtoMessageLiteValue.java @@ -73,8 +73,12 @@ public WireMessageLite value() { @Override public final boolean equals(Object other) { - // TODO: Support message equality - throw new UnsupportedOperationException("Message equality is not supported"); + if (other instanceof RawProtoMessageLiteValue) { + RawProtoMessageLiteValue that = (RawProtoMessageLiteValue) other; + return protoTypeName().equals(that.protoTypeName()) + && this.toByteString().equals(that.toByteString()); + } + return false; } @Override diff --git a/common/src/test/java/dev/cel/common/values/RawProtoMessageLiteValueTest.java b/common/src/test/java/dev/cel/common/values/RawProtoMessageLiteValueTest.java index f47eef35f..87c6373e7 100644 --- a/common/src/test/java/dev/cel/common/values/RawProtoMessageLiteValueTest.java +++ b/common/src/test/java/dev/cel/common/values/RawProtoMessageLiteValueTest.java @@ -101,20 +101,20 @@ public void create_emptyTypeName_normalizesToUnknownMessageTypeName() { } @Test - @SuppressWarnings("SelfEquals") // Testing that equals throws even on self-comparison - public void equals_throwsUnsupportedOperationException() { + public void equals_returnsExpectedResult() { RawProtoMessageLiteValue value1 = RawProtoMessageLiteValue.create(ByteString.EMPTY, "custom.Message", EMPTY_CONVERTER); RawProtoMessageLiteValue value2 = RawProtoMessageLiteValue.create(ByteString.EMPTY, "custom.Message", EMPTY_CONVERTER); + RawProtoMessageLiteValue value3 = + RawProtoMessageLiteValue.create( + ByteString.copyFromUtf8("test"), "custom.Message", EMPTY_CONVERTER); + RawProtoMessageLiteValue value4 = + RawProtoMessageLiteValue.create(ByteString.EMPTY, "custom.Message2", EMPTY_CONVERTER); - UnsupportedOperationException selfThrown = - assertThrows(UnsupportedOperationException.class, () -> value1.equals(value1)); - UnsupportedOperationException otherThrown = - assertThrows(UnsupportedOperationException.class, () -> value1.equals(value2)); - - assertThat(selfThrown).hasMessageThat().isEqualTo("Message equality is not supported"); - assertThat(otherThrown).hasMessageThat().isEqualTo("Message equality is not supported"); + assertThat(value1).isEqualTo(value2); + assertThat(value1).isNotEqualTo(value3); + assertThat(value1).isNotEqualTo(value4); } @Test diff --git a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java index 5fbf0b1a6..e9d4af566 100644 --- a/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java +++ b/runtime/src/test/java/dev/cel/runtime/CelLiteRuntimeVersionSkewTest.java @@ -1273,7 +1273,7 @@ public void submessageEquality_withKnownSubmessageDescriptor_throwsUnsupportedOp } @Test - public void submessageEquality_withoutSubmessageDescriptor_throwsUnsupportedOperationException( + public void submessageEquality_withoutSubmessageDescriptor_evaluatesCorrectly( @TestParameter({ "msg.single_nested_message == msg.single_nested_message", "msg.repeated_nested_message == msg.repeated_nested_message" @@ -1285,16 +1285,9 @@ public void submessageEquality_withoutSubmessageDescriptor_throwsUnsupportedOper serverOptimizer.optimize(serverCompiler.compile(expr).getAst()); Program program = runtimeWithoutNestedDesc.createProgram(optimizedAst); - CelEvaluationException thrown = - assertThrows( - CelEvaluationException.class, - () -> program.eval(ImmutableMap.of("msg", POPULATED_SERVER_MESSAGE))); + Object result = program.eval(ImmutableMap.of("msg", POPULATED_SERVER_MESSAGE)); - assertThat(thrown).hasCauseThat().isInstanceOf(UnsupportedOperationException.class); - assertThat(thrown) - .hasCauseThat() - .hasMessageThat() - .contains("Message equality is not supported"); + assertThat(result).isEqualTo(true); } @Test diff --git a/runtime/src/test/java/dev/cel/runtime/RuntimeEqualityTest.java b/runtime/src/test/java/dev/cel/runtime/RuntimeEqualityTest.java index e67c4f124..e49aa8509 100644 --- a/runtime/src/test/java/dev/cel/runtime/RuntimeEqualityTest.java +++ b/runtime/src/test/java/dev/cel/runtime/RuntimeEqualityTest.java @@ -75,7 +75,7 @@ public void objectEquals_messageLite_throws() { } @Test - public void objectEquals_wireMessageLite_throws() { + public void objectEquals_wireMessageLite_returnsExpectedResult() { RuntimeEquality runtimeEquality = RuntimeEquality.create(RuntimeHelpers.create(), CelOptions.DEFAULT); Object wireMsg1 = @@ -86,18 +86,15 @@ public void objectEquals_wireMessageLite_throws() { ProtoMessageLiteValueProvider.newInstance() .protoCelValueConverter() .toRuntimeValue(TestAllTypes.newBuilder().setSingleInt32(1).build()); + Object wireMsg3 = + ProtoMessageLiteValueProvider.newInstance() + .protoCelValueConverter() + .toRuntimeValue(TestAllTypes.newBuilder().setSingleInt32(1).build()); + assertThat(wireMsg1).isNotEqualTo(wireMsg2); + assertThat(wireMsg2).isEqualTo(wireMsg3); - UnsupportedOperationException messagesThrown = - assertThrows( - UnsupportedOperationException.class, - () -> runtimeEquality.objectEquals(wireMsg1, wireMsg2)); - UnsupportedOperationException stringThrown = - assertThrows( - UnsupportedOperationException.class, - () -> runtimeEquality.objectEquals("not_a_message", wireMsg1)); - - assertThat(messagesThrown).hasMessageThat().isEqualTo("Message equality is not supported"); - assertThat(stringThrown).hasMessageThat().isEqualTo("Message equality is not supported"); + assertThat(runtimeEquality.objectEquals(wireMsg2, wireMsg3)).isTrue(); + assertThat(runtimeEquality.objectEquals(wireMsg1, wireMsg3)).isFalse(); } @Test