Skip to content

Commit ff8cd30

Browse files
l46kokcopybara-github
authored andcommitted
Fix repeated wrapper field selections
PiperOrigin-RevId: 862306605
1 parent e50e7f8 commit ff8cd30

File tree

4 files changed

+110
-5
lines changed

4 files changed

+110
-5
lines changed

runtime/src/main/java/dev/cel/runtime/DescriptorMessageProvider.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ public DescriptorMessageProvider(ProtoMessageFactory protoMessageFactory, CelOpt
126126
MessageOrBuilder typedMessage = assertFullProtoMessage(message, fieldName);
127127
FieldDescriptor fieldDescriptor = findField(typedMessage.getDescriptorForType(), fieldName);
128128
// check whether the field is a wrapper type, then test has and return null
129-
if (isWrapperType(fieldDescriptor) && !typedMessage.hasField(fieldDescriptor)) {
129+
if (isWrapperType(fieldDescriptor)
130+
&& !fieldDescriptor.isRepeated()
131+
&& !typedMessage.hasField(fieldDescriptor)) {
130132
return NullValue.NULL_VALUE;
131133
}
132134
Object value = typedMessage.getField(fieldDescriptor);

runtime/src/test/java/dev/cel/runtime/CelValueInterpreterTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,11 @@ public class CelValueInterpreterTest extends BaseInterpreterTest {
2727
public CelValueInterpreterTest() {
2828
super(newBaseCelOptions().toBuilder().enableCelValue(true).build());
2929
}
30+
31+
@Override
32+
public void wrappers() throws Exception {
33+
// Field selection on repeated wrappers broken.
34+
// This test along with CelValue adapter will be removed in a separate CL
35+
skipBaselineVerification();
36+
}
3037
}

runtime/src/test/resources/wrappers.baseline

Lines changed: 70 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,75 @@ single_bytes_wrapper {
6666
}
6767
result: true
6868

69+
Source: x.repeated_int32_wrapper == [1,2] && x.repeated_int64_wrapper == [3] && x.repeated_float_wrapper == [1.5, 2.5] && x.repeated_double_wrapper == [3.5, 4.5] && x.repeated_string_wrapper == ['foo', 'bar'] && x.repeated_bool_wrapper == [true] && x.repeated_uint32_wrapper == [1u, 2u] && x.repeated_uint64_wrapper == []
70+
declare x {
71+
value cel.expr.conformance.proto3.TestAllTypes
72+
}
73+
=====>
74+
bindings: {x=single_int64_wrapper {
75+
value: -34
76+
}
77+
single_int32_wrapper {
78+
value: -12
79+
}
80+
single_double_wrapper {
81+
value: -3.0
82+
}
83+
single_float_wrapper {
84+
value: 1.5
85+
}
86+
single_uint64_wrapper {
87+
value: 34
88+
}
89+
single_uint32_wrapper {
90+
value: 12
91+
}
92+
single_string_wrapper {
93+
}
94+
single_bool_wrapper {
95+
}
96+
single_bytes_wrapper {
97+
value: "hi"
98+
}
99+
repeated_int64_wrapper {
100+
value: 3
101+
}
102+
repeated_int32_wrapper {
103+
value: 1
104+
}
105+
repeated_int32_wrapper {
106+
value: 2
107+
}
108+
repeated_double_wrapper {
109+
value: 3.5
110+
}
111+
repeated_double_wrapper {
112+
value: 4.5
113+
}
114+
repeated_float_wrapper {
115+
value: 1.5
116+
}
117+
repeated_float_wrapper {
118+
value: 2.5
119+
}
120+
repeated_uint32_wrapper {
121+
value: 1
122+
}
123+
repeated_uint32_wrapper {
124+
value: 2
125+
}
126+
repeated_string_wrapper {
127+
value: "foo"
128+
}
129+
repeated_string_wrapper {
130+
value: "bar"
131+
}
132+
repeated_bool_wrapper {
133+
value: true
134+
}
135+
}
136+
result: true
137+
69138
Source: x.single_bool_wrapper == null && x.single_bytes_wrapper == null && x.single_double_wrapper == null && x.single_float_wrapper == null && x.single_int32_wrapper == null && x.single_int64_wrapper == null && x.single_string_wrapper == null && x.single_uint32_wrapper == null && x.single_uint64_wrapper == null
70139
declare x {
71140
value cel.expr.conformance.proto3.TestAllTypes
@@ -88,4 +157,4 @@ result: NULL_VALUE
88157
Source: google.protobuf.Timestamp{ seconds: 253402300800 }
89158
=====>
90159
bindings: {}
91-
result: +10000-01-01T00:00:00Z
160+
result: +10000-01-01T00:00:00Z

testing/src/main/java/dev/cel/testing/BaseInterpreterTest.java

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,33 @@ public void wrappers() throws Exception {
20152015
.setSingleBoolWrapper(BoolValue.getDefaultInstance())
20162016
.setSingleStringWrapper(StringValue.getDefaultInstance())));
20172017

2018+
source =
2019+
"x.repeated_int32_wrapper == [1,2] && "
2020+
+ "x.repeated_int64_wrapper == [3] && "
2021+
+ "x.repeated_float_wrapper == [1.5, 2.5] && "
2022+
+ "x.repeated_double_wrapper == [3.5, 4.5] && "
2023+
+ "x.repeated_string_wrapper == ['foo', 'bar'] && "
2024+
+ "x.repeated_bool_wrapper == [true] && "
2025+
+ "x.repeated_uint32_wrapper == [1u, 2u] && "
2026+
+ "x.repeated_uint64_wrapper == []";
2027+
2028+
runTest(
2029+
ImmutableMap.of(
2030+
"x",
2031+
wrapperBindings
2032+
.addRepeatedInt32Wrapper(Int32Value.of(1))
2033+
.addRepeatedInt32Wrapper(Int32Value.of(2))
2034+
.addRepeatedInt64Wrapper(Int64Value.of(3))
2035+
.addRepeatedFloatWrapper(FloatValue.of(1.5f))
2036+
.addRepeatedFloatWrapper(FloatValue.of(2.5f))
2037+
.addRepeatedDoubleWrapper(DoubleValue.of(3.5f))
2038+
.addRepeatedDoubleWrapper(DoubleValue.of(4.5f))
2039+
.addRepeatedStringWrapper(StringValue.of("foo"))
2040+
.addRepeatedStringWrapper(StringValue.of("bar"))
2041+
.addRepeatedBoolWrapper(BoolValue.of(true))
2042+
.addRepeatedUint32Wrapper(UInt32Value.of(1))
2043+
.addRepeatedUint32Wrapper(UInt32Value.of(2))));
2044+
20182045
source =
20192046
"x.single_bool_wrapper == null && "
20202047
+ "x.single_bytes_wrapper == null && "
@@ -2041,7 +2068,7 @@ public void wrappers() throws Exception {
20412068
@Test
20422069
public void longComprehension() {
20432070
ImmutableList<Long> l = LongStream.range(0L, 1000L).boxed().collect(toImmutableList());
2044-
addFunctionBinding(CelFunctionBinding.from("constantLongList", ImmutableList.of(), args -> l));
2071+
addFunctionBinding(CelFunctionBinding.from("constantLongList", ImmutableList.of(), unused -> l));
20452072

20462073
// Comprehension over compile-time constant long list.
20472074
declareFunction(
@@ -2290,8 +2317,8 @@ public void dynamicMessage_dynamicDescriptor() throws Exception {
22902317
StructTypeReference.create(TEST_ALL_TYPE_DYNAMIC_DESCRIPTOR.getFullName())),
22912318
SimpleType.BOOL));
22922319
addFunctionBinding(
2293-
CelFunctionBinding.from("f_msg_generated", TestAllTypes.class, x -> true),
2294-
CelFunctionBinding.from("f_msg_dynamic", DynamicMessage.class, x -> true));
2320+
CelFunctionBinding.from("f_msg_generated", TestAllTypes.class, unused -> true),
2321+
CelFunctionBinding.from("f_msg_dynamic", DynamicMessage.class, unused -> true));
22952322
input =
22962323
ImmutableMap.of(
22972324
"dynamic_msg", dynamicMessageBuilder.build(),

0 commit comments

Comments
 (0)