diff --git a/CHANGELOG.md b/CHANGELOG.md index fcdffaa16f..7aa1bccd66 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,8 @@ be consulted to resolve an element. This can be useful when the result of an RPC contains extensions or values in `google.protobuf.Any` messages that are not defined in the same schema that defines the RPC service. +- Fix issue where `buf lint` incorrectly reports error when `(buf.validate.field).required` + is set for an optional field in proto3. ## [v1.28.0] - 2023-11-10 diff --git a/private/bufpkg/bufcheck/buflint/internal/buflintvalidate/field.go b/private/bufpkg/bufcheck/buflint/internal/buflintvalidate/field.go index 5f45d8fda8..37ddd25cfc 100644 --- a/private/bufpkg/bufcheck/buflint/internal/buflintvalidate/field.go +++ b/private/bufpkg/bufcheck/buflint/internal/buflintvalidate/field.go @@ -178,7 +178,9 @@ func checkConstraintsForField( if fieldDescriptor.IsExtension() { checkConstraintsForExtension(adder, fieldConstraints) } - if fieldDescriptor.ContainingOneof() != nil && fieldConstraints.GetRequired() { + if fieldDescriptor.ContainingOneof() != nil && + !protodesc.ToFieldDescriptorProto(fieldDescriptor).GetProto3Optional() && + fieldConstraints.GetRequired() { adder.addForPathf( []int32{requiredFieldNumber}, "Field %q has %s but is in a oneof (%s). Oneof fields must not have %s.", diff --git a/private/bufpkg/bufcheck/buflint/testdata/protovalidate_rules/oneof.proto b/private/bufpkg/bufcheck/buflint/testdata/protovalidate_rules/oneof.proto index aecac9592e..83d13b2728 100644 --- a/private/bufpkg/bufcheck/buflint/testdata/protovalidate_rules/oneof.proto +++ b/private/bufpkg/bufcheck/buflint/testdata/protovalidate_rules/oneof.proto @@ -22,4 +22,7 @@ message OneofTest { } // required is OK for non-oneof fields string f4 = 4 [(buf.validate.field).required = true]; + // optional in proto3 is a synthetic oneof, we should not report error. + optional string f5 = 5 [(buf.validate.field).required = true]; + optional google.protobuf.Duration f6 = 6 [(buf.validate.field).required = true]; }