Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(fieldmask): traverse repeated fields with wildcard in validation #269

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion fieldmask/update_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ func TestUpdate(t *testing.T) {
// can not update individual fields in a repeated message
name: "repeated message: deep",
paths: []string{
"repeated_message.string",
"repeated_message.*.string",
},
src: &syntaxv1.Message{
RepeatedMessage: []*syntaxv1.Message{
Expand Down
14 changes: 13 additions & 1 deletion fieldmask/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,24 @@ func Validate(fm *fieldmaskpb.FieldMask, m proto.Message) error {
md0 := m.ProtoReflect().Descriptor()
for _, path := range fm.GetPaths() {
md := md0
var fd protoreflect.FieldDescriptor
if !rangeFields(path, func(field string) bool {
// Search the field within the message.
if md == nil {
return false // not within a message
}
fd := md.Fields().ByName(protoreflect.Name(field))
// Parent message is a repeated field.
// Targeting sub-fields is allowed with the use of wildcard
// https://google.aip.dev/161#wildcards
if fd != nil && fd.IsList() {
if field == WildcardPath {
fd = nil
return true
}

return false
}
fd = md.Fields().ByName(protoreflect.Name(field))
// The real field name of a group is the message name.
if fd == nil {
gd := md.Fields().ByName(protoreflect.Name(strings.ToLower(field)))
Expand Down
18 changes: 18 additions & 0 deletions fieldmask/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package fieldmask
import (
"testing"

freightv1 "go.einride.tech/aip/proto/gen/einride/example/freight/v1"
"google.golang.org/genproto/googleapis/example/library/v1"
"google.golang.org/protobuf/proto"
"google.golang.org/protobuf/types/known/fieldmaskpb"
Expand Down Expand Up @@ -71,6 +72,23 @@ func TestValidate(t *testing.T) {
message: &library.CreateBookRequest{},
errorContains: "invalid field path: book.foo",
},

{
name: "invalid nested in repeated field",
fieldMask: &fieldmaskpb.FieldMask{
Paths: []string{"shipment.line_items.title"},
},
message: &freightv1.CreateShipmentRequest{},
errorContains: "invalid field path: shipment.line_items.title",
},

{
name: "valid nested in repeated field",
fieldMask: &fieldmaskpb.FieldMask{
Paths: []string{"shipment.line_items.*.title"},
},
message: &freightv1.CreateShipmentRequest{},
},
} {
tt := tt
t.Run(tt.name, func(t *testing.T) {
Expand Down