go
data_error
ai_generated
true
proto: 消息字段 "User.name" 是必填的但未设置
proto: message field "User.name" is required but not set
ID: go/protobuf-message-field-not-set
80%修复率
86%置信度
0证据数
2024-05-19首次发现
版本兼容性
| 版本 | 状态 | 引入 | 弃用 | 备注 |
|---|---|---|---|---|
| google.golang.org/protobuf v1.28+ | active | — | — | — |
根因分析
proto2 的 required 字段(或 proto3 中期望字段存在的场景)在序列化前未填充。proto3 移除了 required,因此通常来自 proto2 文件或 protoc-gen-validate 等校验库。
English
proto2 required fields (or proto3 with field presence expectations) were not populated before marshaling. Proto3 removed required, so this often comes from proto2 files or validation libraries like protoc-gen-validate.
解决方案
-
95% 成功率
u := &pb.User{Name: "alice", Email: "[email protected]"} if err := proto.CheckInitialized(u); err != nil { return fmt.Errorf("invalid user: %w", err) } b, err := proto.Marshal(u) -
90% 成功率
// user.proto: string name = 1 [(validate.rules).string = {min_len: 1}]; if err := u.Validate(); err != nil { return status.Errorf(codes.InvalidArgument, "%v", err) }
无效尝试
常见但无效的做法:
-
85% 失败
AllowPartial only suppresses the marshal error; it still emits a message missing the required field, which the peer will reject on unmarshal.
-
60% 失败
Removing required breaks backward compatibility and shifts the failure to runtime nil dereferences downstream.