# 当字段编号在不同消息类型之间重复使用时，Protobuf反序列化产生错误值

- **ID:** `data/protobuf-field-number-collision`
- **领域:** data
- **类别:** serialization_error
- **验证级别:** ai_generated
- **修复率:** 90%

## 根因

Protobuf线格式仅使用字段编号，而非名称，因此在同一文件中跨不同消息类型重复使用字段编号可能导致反序列化期间字段值被分配给错误的字段。

## 版本兼容性

| 版本 | 状态 | 引入 | 弃用 |
|------|------|------|------|
| Protocol Buffers 3.21.0 | active | — | — |
| protoc 24.0 | active | — | — |
| gRPC 1.57.0 | active | — | — |

## 解决方案

1. ```
   Ensure all message types use unique field numbers across the entire .proto file: message User { int32 id = 1; } message Product { int32 product_id = 1; } // Change to use distinct numbers
   ```
2. ```
   Use a migration script to rewrite existing serialized data with corrected field numbers: for each message, decode using old schema, then re-encode with new schema mapping old field numbers to new ones
   ```

## 无效尝试

- **Adding deprecated annotations to conflicting fields** — Adding fields with new numbers does not fix the existing field number collision issue. (80% 失败率)
- **Using 'reserved' keyword for conflicting field numbers** — Reserved prevents new use but does not fix existing serialized data with wrong field numbers. (75% 失败率)
