Skip to content

Conversation

@smaye81
Copy link
Contributor

@smaye81 smaye81 commented Jun 10, 2025

This adds new message rule oneof which allows for oneof semantics without the ceremony of Protobuf oneof.

Consumers can specify a list of field names in a message that constitute the oneof and can also specify an optional required flag, which enforces that exactly one of the fields must be set. If required is false, it enforces that zero-or-one of specified fields can be set.

@github-actions
Copy link

github-actions bot commented Jun 11, 2025

The latest Buf updates on your PR. Results from workflow Buf CI / buf (pull_request).

BuildFormatLintBreakingUpdated (UTC)
✅ passed✅ passed✅ passed✅ passedJun 11, 2025, 5:17 PM

@smaye81
Copy link
Contributor Author

smaye81 commented Jun 11, 2025

Updated to include a test for unknown field name and changed the formatting for the validation errors.

@smaye81 smaye81 marked this pull request as ready for review June 11, 2025 16:00
@smaye81 smaye81 requested review from a user, Alfus, rodaine and srikrsna-buf June 11, 2025 16:00

message MessageOneofRule {
repeated string fields = 1;
optional bool required = 2;
Copy link
Collaborator

Choose a reason for hiding this comment

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

add comments on the fields too?

bool bool_field = 2;
}

message MessageOneofUnknownFieldName {
Copy link
Collaborator

Choose a reason for hiding this comment

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

consider also having oneof with no fields (which should also be an error)

Choose a reason for hiding this comment

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

There are a few rough edges (like this, like if you specify the same field name multiple times, and certainly others we haven't had time to think of yet), but the desire was to get this out fast. We'll keep track of these and hopefully can fix them soon, but I don't want to block this on edge cases.

The faster you can start using this, the faster you can help us find more of those edges cases and we can take care of them!

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In light of this, I removed the MessageOneofNoFields test case in fd2217b and we can address as a follow-up.

Steve Ayers added 2 commits June 11, 2025 12:17
@smaye81
Copy link
Contributor Author

smaye81 commented Jun 11, 2025

@Alfus incorporated feedback in 57e344b. Lmk what you think.

@smaye81 smaye81 requested review from Alfus and timostamm June 11, 2025 16:19
@smaye81 smaye81 requested a review from jhump June 11, 2025 16:36

message MessageWith3dInside {}

message MessageOneofSingleField {
Copy link
Member

Choose a reason for hiding this comment

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

Add test case for MessageOneofSingleFieldRequired?

Copy link
Member

Choose a reason for hiding this comment

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

can do in followup PR

Copy link
Member

Choose a reason for hiding this comment

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

Follow-up: #379

@smaye81 smaye81 merged commit 03d8496 into main Jun 11, 2025
7 checks passed
@smaye81 smaye81 deleted the sayers/message_oneof_rule branch June 11, 2025 18:00
Comment on lines +153 to +156
// 3. This will generate validation errors when unmarshalling, even from the binary
// format. With a Protobuf oneof, if multiple fields are present in the serialized
// form, earlier values are usually silently ignored when unmarshalling, with only
// the last field being present when unmarshalling completes.
Copy link
Member

@jhump jhump Jun 11, 2025

Choose a reason for hiding this comment

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

I know I proposed this copy, but I found some issues in it. Here's a take two 😄

Suggested change
// 3. This will generate validation errors when unmarshalling, even from the binary
// format. With a Protobuf oneof, if multiple fields are present in the serialized
// form, earlier values are usually silently ignored when unmarshalling, with only
// the last field being present when unmarshalling completes.
// 3. This will always generate validation errors for a message unmarshalled from
// serialized data that sets more than one field. With a Protobuf oneof, when
// multiple fields are present in the serialized form, earlier values are usually
// silently ignored when unmarshalling, with only the last field being set when
// unmarshalling completes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah shoot, sorry. Will update this in a follow-up.

Copy link
Member

Choose a reason for hiding this comment

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

Follow-up: #378

smaye81 added a commit to bufbuild/protovalidate-go that referenced this pull request Jun 11, 2025
This implements the `MessageOneof` rule specified in
bufbuild/protovalidate#377

---------

Co-authored-by: Timo Stamm <[email protected]>
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.

8 participants