Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ public CelCheckerLegacyImpl build() {
} else if (celTypeProvider != null) {
messageTypeProvider =
new CelTypeProvider.CombinedCelTypeProvider(
ImmutableList.of(celTypeProvider, messageTypeProvider));
ImmutableList.of(messageTypeProvider, celTypeProvider));
}

// Configure the declaration set, and possibly alter the type provider if ProtoDecl values
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.protobuf.Duration;
import com.google.protobuf.FieldMask;
import com.google.protobuf.Timestamp;
Expand All @@ -40,6 +41,7 @@
import dev.cel.common.types.ListType;
import dev.cel.common.types.MapType;
import dev.cel.common.types.SimpleType;
import dev.cel.common.types.StructType;
import dev.cel.common.types.StructTypeReference;
import dev.cel.common.types.TypeType;
import dev.cel.compiler.CelCompiler;
Expand Down Expand Up @@ -409,6 +411,47 @@ public Optional<CelType> findType(String typeName) {
assertThat(ast.getResultType()).isEqualTo(preWrappedType);
}

@Test
public void check_combinedTypeProviders_protoMessageTakesPrecedenceOverCustom() throws Exception {
StructType shadowingStructType =
StructType.create(
TestAllTypes.getDescriptor().getFullName(),
ImmutableSet.of("single_int64"),
fieldName -> Optional.of(SimpleType.STRING));
StructType customOnlyStructType =
StructType.create(
"custom.CustomStruct",
ImmutableSet.of("custom_field"),
fieldName -> Optional.of(SimpleType.STRING));
CelTypeProvider customTypeProvider =
new CelTypeProvider() {
@Override
public ImmutableList<CelType> types() {
return ImmutableList.of(shadowingStructType, customOnlyStructType);
}

@Override
public Optional<CelType> findType(String typeName) {
return types().stream().filter(t -> t.name().equals(typeName)).findFirst();
}
};
CelCompiler celCompiler =
CelCompilerFactory.standardCelCompilerBuilder()
.addMessageTypes(TestAllTypes.getDescriptor())
.setTypeProvider(customTypeProvider)
.build();

CelAbstractSyntaxTree protoAst =
celCompiler
.compile("cel.expr.conformance.proto3.TestAllTypes{single_int64: 1}.single_int64")
.getAst();
CelAbstractSyntaxTree customAst =
celCompiler.compile("custom.CustomStruct{custom_field: 'hello'}.custom_field").getAst();

assertThat(protoAst.getResultType()).isEqualTo(SimpleType.INT);
assertThat(customAst.getResultType()).isEqualTo(SimpleType.STRING);
}

private enum FieldTypeTestCase {
REPEATED_PRIMITIVE("msg.repeated_int64", ListType.create(SimpleType.INT)),
MAP_PRIMITIVE("msg.map_string_string", MapType.create(SimpleType.STRING, SimpleType.STRING)),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,6 @@ public class DescriptorTypeResolverTest {
private static final Cel CEL =
CelFactory.plannerCelBuilder()
.setTypeProvider(PROTO_MESSAGE_TYPE_PROVIDER)
// TODO: Replace setValueProvider with
// addMessageTypes(TestAllTypes.getDescriptor()) once CelRuntimeImpl prioritizes custom
// CelTypeProvider over its internal messageTypeProvider.
.setValueProvider(
(structType, fields) ->
structType.equals(TestAllTypes.getDescriptor().getFullName())
Expand Down
Loading