# Protobuf 反序列化静默丢弃未知的 oneof 字段导致数据丢失

- **ID:** `data/protobuf-oneof-unknown-field`
- **领域:** data
- **类别:** protocol_error
- **错误码:** `No explicit error; silent data corruption`
- **验证级别:** ai_generated
- **修复率:** 80%

## 根因

当包含 oneof 字段的 Protobuf 消息被不知道新 oneof 变体的旧版本反序列化时，整个 oneof 被设置为 None，丢失该 oneof 中所有其他已知字段。

## 版本兼容性

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

## 解决方案

1. ```
   Add a catch-all 'bytes unknown_oneof' field to the message to preserve raw bytes of unknown oneof variants, then manually parse.
   ```
2. ```
   Ensure all consumers are updated to the latest proto definition before deploying new oneof variants.
   ```

## 无效尝试

- **Adding a new field outside the oneof to carry the new variant** — This changes the message structure and may cause confusion, and the oneof still gets cleared on unknown variants. (50% 失败率)
- **Using 'optional' keyword for oneof fields** — The 'optional' keyword does not prevent the oneof from being cleared when an unknown variant is encountered. (70% 失败率)
