Skip to content

Commit a9cd4f3

Browse files
l46kokcopybara-github
authored andcommitted
Assert the key type for JSON struct to be a string
PiperOrigin-RevId: 755547701
1 parent 43486ef commit a9cd4f3

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

common/src/main/java/dev/cel/common/values/BaseProtoCelValueConverter.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,13 +158,20 @@ private ListValue<CelValue> adaptJsonListToCelValue(com.google.protobuf.ListValu
158158
.collect(toImmutableList()));
159159
}
160160

161-
// TODO: Investigate changing MapValue key to StringValue
162-
private MapValue<CelValue, CelValue> adaptJsonStructToCelValue(Struct struct) {
161+
private MapValue<dev.cel.common.values.StringValue, CelValue> adaptJsonStructToCelValue(
162+
Struct struct) {
163163
return ImmutableMapValue.create(
164164
struct.getFieldsMap().entrySet().stream()
165165
.collect(
166166
toImmutableMap(
167-
e -> fromJavaObjectToCelValue(e.getKey()),
167+
e -> {
168+
CelValue key = fromJavaObjectToCelValue(e.getKey());
169+
if (!(key instanceof dev.cel.common.values.StringValue)) {
170+
throw new IllegalStateException(
171+
"Expected a string type for the key, but instead got: " + key);
172+
}
173+
return (dev.cel.common.values.StringValue) key;
174+
},
168175
e -> adaptJsonValueToCelValue(e.getValue()))));
169176
}
170177

0 commit comments

Comments
 (0)