go data_error ai_generated true

proto: message field "User.name" is required but not set

ID: go/protobuf-message-field-not-set

Also available as: JSON · Markdown · 中文
80%Fix Rate
86%Confidence
0Evidence
2024-05-19First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
google.golang.org/protobuf v1.28+ active — — —

Root Cause

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.

generic

中文

proto2 的 required 字段(或 proto3 中期望字段存在的场景)在序列化前未填充。proto3 移除了 required,因此通常来自 proto2 文件或 protoc-gen-validate 等校验库。

Workarounds

  1. 95% success
    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)
  2. 90% success
    // user.proto: string name = 1 [(validate.rules).string = {min_len: 1}];
    if err := u.Validate(); err != nil {
        return status.Errorf(codes.InvalidArgument, "%v", err)
    }

Dead Ends

Common approaches that don't work:

  1. 85% fail

    AllowPartial only suppresses the marshal error; it still emits a message missing the required field, which the peer will reject on unmarshal.

  2. 60% fail

    Removing required breaks backward compatibility and shifts the failure to runtime nil dereferences downstream.