Skip to content

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

Merged
johanbrandhorst merged 3 commits intogrpc-ecosystem:mainfrom
sessa:fix/oneof-required-proto3-semantics
Feb 10, 2026
Merged

fix(openapiv2): exclude oneof fields from required with proto3 field semantics#6335
johanbrandhorst merged 3 commits intogrpc-ecosystem:mainfrom
sessa:fix/oneof-required-proto3-semantics

Conversation

@sessa
Copy link
Copy Markdown
Contributor

@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
Copy Markdown
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
@sessa
Copy link
Copy Markdown
Contributor Author

sessa commented Feb 9, 2026

@johanbrandhorst, hope you had a good weekend -- thank you for the feedback (and this tool). Let me know if I you need anything else from me.

Copy link
Copy Markdown
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.

Sorry for the delay, seems like the linter found a slight error in the new file.

Add the new proto file to the buf lint ignore_only sections for:
- DIRECTORY_SAME_PACKAGE
- PACKAGE_DIRECTORY_MATCH
- PACKAGE_SAME_GO_PACKAGE
- PACKAGE_VERSION_SUFFIX

This follows the same pattern as other proto files in the examplepb
directory.

Co-authored-by: Cursor <cursoragent@cursor.com>
@sessa
Copy link
Copy Markdown
Contributor Author

sessa commented Feb 9, 2026

Added it to the ignore sections like the other example use cases, tested locally, buf lint should pass now.

@johanbrandhorst
Copy link
Copy Markdown
Collaborator

Added it to the ignore sections like the other example use cases, tested locally, buf lint should pass now.

Busted by the formatter this time 😂. Sorry!

Co-authored-by: Cursor <cursoragent@cursor.com>
@sessa
Copy link
Copy Markdown
Contributor Author

sessa commented Feb 10, 2026

My fault. I think that should do it. Thank you!

@johanbrandhorst johanbrandhorst enabled auto-merge (squash) February 10, 2026 16:19
@johanbrandhorst johanbrandhorst merged commit 2d02a4a into grpc-ecosystem:main Feb 10, 2026
14 checks passed
@sessa
Copy link
Copy Markdown
Contributor Author

sessa commented Feb 10, 2026

@johanbrandhorst -- what's the releasee process like for this?

Thank you!

@johanbrandhorst
Copy link
Copy Markdown
Collaborator

I'll usually put one out every few months or if there is a significant bug fix, but I think we can put one out for this and other fixes, so I did: https://github.com/grpc-ecosystem/grpc-gateway/releases/tag/v2.27.8.

@mrthebunny
Copy link
Copy Markdown

Hi @johanbrandhorst what steps are required to publish 2.27.8 to the Buf Schema Registry ? we are relying on it to fetch the new version.

https://buf.build/grpc-ecosystem/openapiv2

@johanbrandhorst
Copy link
Copy Markdown
Collaborator

The buf.build plugin version is maintained by buf: https://github.com/bufbuild/plugins/tree/main/plugins/grpc-ecosystem/openapiv2. I'm not sure why it hasn't been updated yet, maybe a problem with their automation? You could try reaching out on their slack or opening an issue on the repo.

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

3 participants