Skip to content

fix(openapiv2): exclude oneof fields from required with proto3 field semantics#6335

Open
sessa wants to merge 1 commit intogrpc-ecosystem:mainfrom
sessa:fix/oneof-required-proto3-semantics
Open

fix(openapiv2): exclude oneof fields from required with proto3 field semantics#6335
sessa wants to merge 1 commit intogrpc-ecosystem:mainfrom
sessa:fix/oneof-required-proto3-semantics

Conversation

@sessa
Copy link

@sessa sessa commented Feb 6, 2026

Summary

  • Fixes oneof fields being incorrectly marked as required when use_proto3_field_semantics=true is set
  • Adds a field.OneofIndex == nil check in updateSwaggerObjectFromFieldBehavior so that oneof members are excluded from the required array
  • Explicit REQUIRED field behavior annotations still override this correctly

Problem

When use_proto3_field_semantics=true, all non-optional proto3 fields are added to the OpenAPI required array. This includes oneof members, which is semantically wrong -- a oneof means at most one field can be set, so they should never all be required.

For example, this proto:

message Filter {
  oneof filter_group_or_value {
    FilterList list = 1;
    FilterRule rule = 2;
  }
}

Generates both list and rule as required, when neither should be.

Fix

One-line change in updateSwaggerObjectFromFieldBehavior:

// Before
required = !field.GetProto3Optional()

// After
required = !field.GetProto3Optional() && field.OneofIndex == nil

Proto3 optional fields use synthetic oneofs (OneofIndex != nil), but GetProto3Optional() already handles that case. The OneofIndex == nil check catches real oneof groups.

Test plan

  • Added 3 new test cases to Test_updateSwaggerObjectFromFieldBehavior:
    • Oneof field not required with proto3 field semantics
    • Oneof field not required without proto3 field semantics
    • Oneof field still required when explicitly annotated with REQUIRED
  • All existing tests pass with no regressions

Fixes #6334

Made with Cursor

@sessa sessa force-pushed the fix/oneof-required-proto3-semantics branch from 9cf733d to f736b46 Compare February 6, 2026 21:46
Copy link
Collaborator

@johanbrandhorst johanbrandhorst left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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>
@sessa sessa force-pushed the fix/oneof-required-proto3-semantics branch from f736b46 to abb05f9 Compare February 6, 2026 22:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

protoc-gen-openapiv2: oneof fields incorrectly marked as required with use_proto3_field_semantics=true

2 participants