go data_error ai_generated true

proto: required field "User.email" not set

ID: go/proto-required-field-not-set

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

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
github.com/golang/protobuf v1.5 / proto2 active

Root Cause

proto2 required fields must be explicitly set before marshaling. In proto3 there are no required fields, so this only appears with proto2 or legacy generated code.

generic

中文

proto2 的 required 字段在序列化前必须显式设置。proto3 没有 required 字段,因此该错误只出现在 proto2 或旧版生成代码中。

Workarounds

  1. 90% success
    u.Email = proto.String("[email protected]")
    if err := proto.Marshal(u); err != nil {
        log.Fatalf("marshal: %v", err)
    }
  2. 80% success
    // user.proto: syntax = "proto3";
    // string email = 1 [ (validate.rules).string.email = true ];

Dead Ends

Common approaches that don't work:

  1. 95% fail

    Marshaling is deterministic; an unset required field stays unset. Retrying never fills the field.

  2. 50% fail

    Regenerating changes the API; callers relying on presence semantics may now send empty values silently.