Skip to content

Commit

Permalink
Fix bug where cel expression cannot compile for fields of type google…
Browse files Browse the repository at this point in the history
….protobuf.Any (#65)

This fixes the problem where cel expressions defined on fields of type
`google.protobuf.Any` cannot compile.

For example, when validating a `FieldOfTypeAny` with its `any` field set
to a `Simple`, it would fail with
```
runtime error: error evaluating any_type: unknown type: 'tests.example.v1.Simple'
```
```
message Simple {
  string s = 1;
}

message FieldOfTypeAny {
  google.protobuf.Any any = 1 [(buf.validate.field).cel = {
    id: "any_type",
    message: "this should never fail",
    expression: "this == this"
  }];
}
```
  • Loading branch information
oliversun9 authored Oct 24, 2023
1 parent ec5a306 commit 60a89b5
Show file tree
Hide file tree
Showing 4 changed files with 316 additions and 153 deletions.
1 change: 1 addition & 0 deletions internal/evaluator/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ func (bldr *Builder) processFieldExpressions(
var opts []cel.EnvOption
if fieldDesc.Kind() == protoreflect.MessageKind {
opts = []cel.EnvOption{
cel.Types(dynamicpb.NewMessage(fieldDesc.ContainingMessage())),
cel.Types(dynamicpb.NewMessage(fieldDesc.Message())),
cel.Variable("this", cel.ObjectType(string(fieldDesc.Message().FullName()))),
}
Expand Down
Loading

0 comments on commit 60a89b5

Please sign in to comment.