go
data_error
ai_generated
true
proto: (第 1 行第 14 列):未知字段 "userName"
proto: (line 1:14): unknown field "userName"
ID: go/protobuf-json-unknown-field
80%修复率
86%置信度
0证据数
2024-11-18首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| google.golang.org/protobuf 1.28+ | active | — | — | — |
根因分析
protojson.Unmarshal 遇到了与目标消息任何字段都不匹配的 JSON 键。protobuf JSON 使用 proto 字段名(snake_case)或 lowerCamelCase 的 JSON 名;使用不相关的命名会失败。
English
protojson.Unmarshal encountered a JSON key that does not match any field in the target message. protobuf JSON uses either the proto field name (snake_case) or the lowerCamelCase JSON name; using an unrelated casing fails.
解决方案
-
90% 成功率
opts := protojson.UnmarshalOptions{DiscardUnknown: true} if err := opts.Unmarshal(data, msg); err != nil { return err } -
88% 成功率
// .proto: string user_name = 1; // JSON accepts either: // {"user_name":"alice"} or {"userName":"alice"}
无效尝试
常见但无效的做法:
-
80% 失败
encoding/json ignores protobuf-specific semantics (oneofs, well-known types) and produces incorrect values.
-
60% 失败
Fragile and breaks for nested messages, repeated fields, and oneofs; also loses type info.