Skip to content

Commit e2f3e5f

Browse files
l46kokcopybara-github
authored andcommitted
Throw CelAttributeNotFoundException in ProtoMessageLiteValue.find for undeclared fields
PiperOrigin-RevId: 1002667477
1 parent ab4c852 commit e2f3e5f

2 files changed

Lines changed: 29 additions & 22 deletions

File tree

‎common/src/main/java/dev/cel/common/values/ProtoMessageLiteValue.java‎

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -133,19 +133,23 @@ public final String toString() {
133133

134134
@Override
135135
public Object select(String field) {
136-
FieldLiteDescriptor fd = descriptor().findByFieldName(field);
137-
if (fd == null) {
138-
throw CelAttributeNotFoundException.of(
139-
String.format("field '%s' is not declared in message '%s'", field, protoTypeName()));
140-
}
136+
FieldLiteDescriptor fd = findFieldDescriptor(field);
141137
Object fieldValue = readField(fd);
142138
return fieldValue != null ? fieldValue : protoLiteCelValueConverter().getDefaultCelValue(fd);
143139
}
144140

145141
@Override
146142
public Optional<Object> find(String field) {
147-
// Per SelectableValue#find, a field that doesn't exist is reported as absent.
148-
return Optional.ofNullable(descriptor().findByFieldName(field)).map(this::readField);
143+
return Optional.ofNullable(readField(findFieldDescriptor(field)));
144+
}
145+
146+
private FieldLiteDescriptor findFieldDescriptor(String field) {
147+
FieldLiteDescriptor fd = descriptor().findByFieldName(field);
148+
if (fd == null) {
149+
throw CelAttributeNotFoundException.of(
150+
String.format("field '%s' is not declared in message '%s'", field, protoTypeName()));
151+
}
152+
return fd;
149153
}
150154

151155
@Override

‎common/src/test/java/dev/cel/common/values/ProtoMessageLiteValueTest.java‎

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -368,32 +368,35 @@ public void select_undeclaredField_throwsCelAttributeNotFoundException() {
368368
+ " 'cel.expr.conformance.proto3.TestAllTypes'");
369369
}
370370

371-
@SuppressWarnings("ImmutableEnumChecker") // Test only
372-
private enum FindFieldTestCase {
373-
PRESENT("single_string", Optional.of("foo")),
374-
ABSENT("single_int64", Optional.empty()),
375-
UNDECLARED("undeclared_field", Optional.empty());
371+
@Test
372+
public void find_undeclaredField_throwsCelAttributeNotFoundException() {
373+
ProtoMessageLiteValue messageLiteValue =
374+
ProtoMessageLiteValue.create(
375+
TestAllTypes.getDefaultInstance(),
376+
TEST_ALL_TYPES_DESCRIPTOR,
377+
PROTO_LITE_CEL_VALUE_CONVERTER);
376378

377-
private final String fieldName;
378-
private final Optional<Object> expected;
379+
CelAttributeNotFoundException thrown =
380+
assertThrows(
381+
CelAttributeNotFoundException.class, () -> messageLiteValue.find("undeclared_field"));
379382

380-
FindFieldTestCase(String fieldName, Optional<Object> expected) {
381-
this.fieldName = fieldName;
382-
this.expected = expected;
383-
}
383+
assertThat(thrown)
384+
.hasMessageThat()
385+
.isEqualTo(
386+
"field 'undeclared_field' is not declared in message"
387+
+ " 'cel.expr.conformance.proto3.TestAllTypes'");
384388
}
385389

386390
@Test
387-
public void find_returnsValueOnlyIfFieldIsPresent(@TestParameter FindFieldTestCase testCase) {
391+
public void find_returnsValueOnlyIfFieldIsPresent() {
388392
ProtoMessageLiteValue messageLiteValue =
389393
ProtoMessageLiteValue.create(
390394
TestAllTypes.newBuilder().setSingleString("foo").build(),
391395
TEST_ALL_TYPES_DESCRIPTOR,
392396
PROTO_LITE_CEL_VALUE_CONVERTER);
393397

394-
Optional<Object> result = messageLiteValue.find(testCase.fieldName);
395-
396-
assertThat(result).isEqualTo(testCase.expected);
398+
assertThat(messageLiteValue.find("single_string")).hasValue("foo");
399+
assertThat(messageLiteValue.find("single_int64")).isEmpty();
397400
}
398401

399402
@SuppressWarnings("ImmutableEnumChecker") // Test only

0 commit comments

Comments
 (0)