go
data_error
ai_generated
true
proto: (第 1 行第 15 列):未知字段 "userName"
proto: (line 1:15): unknown field "userName"
ID: go/proto-json-unknown-field
80%修复率
88%置信度
0证据数
2024-08-14首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| google.golang.org/protobuf v1.30+ | active | — | — | — |
根因分析
protojson.Unmarshal 遇到与消息中任何字段都不匹配的 JSON 键。默认情况下 protojson 拒绝未知字段,且对 proto 字段名大小写敏感(接受 camelCase 或 snake_case)。
English
protojson.Unmarshal encountered a JSON key that does not match any field in the message. By default protojson rejects unknown fields and is case-sensitive on the proto field names (camelCase or snake_case accepted).
解决方案
-
90% 成功率
opts := protojson.UnmarshalOptions{DiscardUnknown: true} err := opts.Unmarshal(data, msg) -
85% 成功率
// user.proto string user_name = 1 [json_name = "userName"];
无效尝试
常见但无效的做法:
-
60% 失败
Field renames change the wire contract and require regenerating all clients; it treats a symptom, not the schema mismatch.
-
85% 失败
protojson returns an error and leaves the message partially populated or empty; downstream reads wrong data.