fix(openapiv2): exclude oneof fields from required with proto3 field semantics#6335
Open
sessa wants to merge 1 commit intogrpc-ecosystem:mainfrom
Open
fix(openapiv2): exclude oneof fields from required with proto3 field semantics#6335sessa wants to merge 1 commit intogrpc-ecosystem:mainfrom
sessa wants to merge 1 commit intogrpc-ecosystem:mainfrom
Conversation
9cf733d to
f736b46
Compare
johanbrandhorst
requested changes
Feb 6, 2026
Collaborator
johanbrandhorst
left a comment
There was a problem hiding this comment.
Thanks for your contribution, could you please submit an example proto file that reproduces this issue to internal/example/proto? Either add it to an existing one with the flag set, or add a new protobuf file and buf.gen.yaml file with the option set. It's a secondary type of testing we use in addition to template_test.go. Thanks!
…semantics When `use_proto3_field_semantics=true` is set, all non-optional proto3 fields are marked as required. However, fields in a `oneof` group should not be required since at most one of them can be set at a time. This adds a check for `field.OneofIndex == nil` so that oneof members are excluded from the required array. Explicit `REQUIRED` field behavior annotations still override this and mark the field as required. Fixes grpc-ecosystem#6334 Co-authored-by: Cursor <cursoragent@cursor.com>
f736b46 to
abb05f9
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
oneoffields being incorrectly marked asrequiredwhenuse_proto3_field_semantics=trueis setfield.OneofIndex == nilcheck inupdateSwaggerObjectFromFieldBehaviorso that oneof members are excluded from the required arrayREQUIREDfield behavior annotations still override this correctlyProblem
When
use_proto3_field_semantics=true, all non-optional proto3 fields are added to the OpenAPIrequiredarray. This includesoneofmembers, which is semantically wrong -- aoneofmeans at most one field can be set, so they should never all be required.For example, this proto:
Generates both
listandruleas required, when neither should be.Fix
One-line change in
updateSwaggerObjectFromFieldBehavior:Proto3
optionalfields use synthetic oneofs (OneofIndex != nil), butGetProto3Optional()already handles that case. TheOneofIndex == nilcheck catches realoneofgroups.Test plan
Test_updateSwaggerObjectFromFieldBehavior:REQUIREDFixes #6334
Made with Cursor