go
data_error
ai_generated
true
proto:(第 1 行第 2 列):pb.User 中未知字段 "user_id"
proto: (line 1:2): unknown field "user_id" in pb.User
ID: go/proto-unknown-field-json
80%修复率
87%置信度
0证据数
2024-06-01首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| google.golang.org/protobuf 1.26+ | active | — | — | — |
根因分析
protojson.Unmarshal 遇到目标消息中不存在的 JSON 字段。常见于 JSON 使用 snake_case 而 proto 字段使用 camelCase(protojson 默认),或 JSON 来自不同 schema 版本。
English
protojson.Unmarshal encountered a JSON field not present in the target message. Common when JSON uses snake_case but the proto field uses camelCase (protojson default), or the JSON came from a different schema version.
解决方案
-
90% 成功率
Use protojson with explicit options: opts := protojson.UnmarshalOptions{ DiscardUnknown: true, } err := opts.Unmarshal(data, msg) -
85% 成功率
Configure the emitter/unmarshaler for the naming convention you expect: protojson.UnmarshalOptions{Resolver: ...} // and emit with UseProtoNames to force snake_case: m := protojson.MarshalOptions{UseProtoNames: true}
无效尝试
常见但无效的做法:
-
85% 失败
protojson returns a partial message; downstream code sees zero values and produces wrong results.
-
90% 失败
Generated protobuf structs do not honor json tags; protojson uses its own field-name mapping.