Skip to content

[protobuf] Represent TypeSpec optional properties as optional fields#10598

Open
witemple-msft wants to merge 4 commits intomicrosoft:mainfrom
witemple-msft:protobuf-optional
Open

[protobuf] Represent TypeSpec optional properties as optional fields#10598
witemple-msft wants to merge 4 commits intomicrosoft:mainfrom
witemple-msft:protobuf-optional

Conversation

@witemple-msft
Copy link
Copy Markdown
Member

@witemple-msft witemple-msft commented May 5, 2026

Resolves a long-outstanding capability issue with the protobuf emitter not supporting optional because it wasn't specified in the proto3 language specification when this emitter was created.

There is a fair amount of nuance about how "optionality" is represented in protobuf (everything is actually optional in a protobuf message -- the only difference is whether "presence" is explicit or implicit, so optional is really rather a misnomer).

  • For Protobuf messages, we don't emit optional because Protobuf message-typed fields always have explicit presence discipline. Applications are responsible for validating whether an "unset" field is acceptable.
  • For Protobuf scalars, we emit optional. However, the alternative is not that the message will fail to validate if the field is not provided, rather that the client will provide the default value of that scalar (zero, empty string, etc.).
  • For repeated/map-typed fields (TypeSpec arrays, Protobuf.Map), we produce a warning. Due to the nature of how repeated fields are encoded (and map desugars to a repeated key-value pair), it is impossible to differentiate empty from unset in an encoded protobuf message.

Closes #10499
Closes #4311

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 5, 2026

Open in StackBlitz

npm i https://pkg.pr.new/@typespec/protobuf@10598

commit: 52848d1

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented May 5, 2026

All changed packages have been documented.

  • @typespec/protobuf
Show changes

@typespec/protobuf - feature ✏️

Map TypeSpec optionality (?) to protobuf optional where appropriate.,> ,> - optional is applied to fields with protobuf scalar types to set explicit presence.,> - optional is not applied to fields with message types, because they always have explicit presence.,> - Attempting to convert a TypeSpec optional property where the type is an array or Protobuf.Map instance produces a warning, because protobuf cannot differentiate between "empty" and "unset" repeated/map-typed fields.

@azure-sdk
Copy link
Copy Markdown
Collaborator

azure-sdk commented May 5, 2026

You can try these changes here

🛝 Playground 🌐 Website 🛝 VSCode Extension

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

emitter:protobuf The protobuf emitter meta:website TypeSpec.io updates

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: @typespec/protobuf don't support optional keyword [Feature request] [protobuf] add optional keyword

2 participants