Skip to content

[Bug]: @typespec/protobuf don't support optional keyword #10499

@wennerryle

Description

@wennerryle

Describe the bug

@message
model ChatBase {
    @field(1) username: string;
    @field(2) first_name?: string;
    @field(3) last_name?: string;
    @field(4) id: int64;
    @field(5) createdAt: Time;
}

Generating wrong proto3 schema:

message ChatBase {
  string username = 1;
  string first_name = 2;
  string last_name = 3;
  int64 id = 4;
  int64 createdAt = 5;
}

Expected result according to proto3
docs: https://protobuf.dev/programming-guides/proto3/#field-labels

Image
message ChatBase {
  string username = 1;
  optional string first_name = 2;
  optional string last_name = 3;
  int64 id = 4;
  int64 createdAt = 5;
}

Yes, by design all properties in proto3 are optional (like in Go way: Optional of string is = "")

But sometimes we wanna know when the property was explicitly set:

message Person {
   int64 age = 1; // when default it'll be 0, but there's some 0 age people in the planet :)
}
message Person {
   optional int64 age = 1; // OK, it's just not set, not overlapsing with 0 age people
}

Reproduction

https://typespec.io/playground/?e=%40typespec%2Fprotobuf&c=aW1wb3J0ICJAdHlwZXNwZWMvcHJvdG9idWYiOwoKdXNpbmcgUMcSOwoKQMgMLmBwYWNrYWdlYCh7CiAgbmFtZTogIkV4YW1wbGUiCn0pCsQTc3BhY2UgZcYWxD9tZXNzYWdlCm1vZGVsIENoYXRCYXNlIMRDICBAZmllbGQoMSkgdXNlcsZTc3RyaW5nO8wgMikgZmlyc3RfxCI%2F1SMzKSBsYd0iNCkgaWQ6IGludDY0zRk1KSBjcmVhdGVkQXQ66QD7LldlbGxLbm93bi5UaW1lc3RhbXA7Cn0K&options=%7B%7D&vs=%7B%7D

Checklist

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions