go data_error ai_generated true

proto: (第 1 行:第 2 列): 在 pb.YourMessage 中未知字段 "unknown_field"

proto: (line 1:2): unknown field "unknown_field" in pb.YourMessage

ID: go/protobuf-json-marshal-unknown-field

其他格式: JSON · Markdown 中文 · English
80%修复率
85%置信度
0证据数
2024-08-22首次发现

版本兼容性

版本状态引入弃用备注
1.x active — — —

根因分析

JSON 输入包含 protobuf 消息中未定义的字段,且解组器配置为拒绝未知字段。

English

The JSON input contains a field that is not defined in the protobuf message, and the unmarshaler is configured to reject unknown fields.

generic

解决方案

  1. 95% 成功率
    import "google.golang.org/protobuf/encoding/protojson"
    opts := protojson.UnmarshalOptions{DiscardUnknown: true}
    err := opts.Unmarshal(data, msg)
  2. 90% 成功率
    // Add the field to your .proto file and regenerate.
    // message YourMessage { string unknown_field = 1; }

无效尝试

常见但无效的做法:

  1. 60% 失败

    This is a temporary fix; the field may reappear or be required by other clients.

  2. 80% 失败

    encoding/json does not understand protobuf field names and will produce incorrect results.