microsoft / microsoft/typespec

Proto emitter: @field numbers collide when using model inheritance

Open
#9,278 2 comments 1 reaction 1 assignee Claimed by @witemple-msft View on GitHub
bug emitter:protobuf triaged:core
Dominant language
Java
Stars
5.9k
Forks
394
Avg merge
1d 23h
Merged PRs (30d)
104

Description

When using the Protobuf emitter, @field(n) is mandatory.

However, when a model extends another model, field numbers are not
merged or offset. This leads to duplicated field numbers in the
generated message, which is invalid in Protobuf.

Example:

model Base {
@field(1)
id: string;
}

model Child extends Base {
@field(1)
name: string;
}

Generated proto:

message Child {
string id = 1;
string name = 1; // invalid: duplicate field number
}

This makes model inheritance unsafe with Protobuf.

Current issues:
- @field numbers are scoped per model, not per message
- No validation or error is raised
- No automatic offset or merging strategy exists

This breaks:
- Protobuf constraints
- Backward compatibility guarantees
- Design-first modeling with inheritance

Suggested directions:
- Automatically shift child field numbers after inherited fields
- Or support an implicit field numbering strategy
- Or disallow inheritance for proto models with a clear error

At the moment, using inheritance with Protobuf is dangerous and
non-deterministic.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.