go type_error ai_generated true

rpc 错误:代码 = InvalidArgument 描述 = 字段 age 期望字符串,但得到数字

rpc error: code = InvalidArgument desc = expected string, got number for field age

ID: go/grpc-invalid-argument-field-type

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

版本兼容性

版本状态引入弃用备注
1.56.x active
1.57.x active

根因分析

protobuf 消息字段类型不匹配;例如,在期望字符串的地方发送了整数。

English

The protobuf message field type mismatch; e.g., sending an integer where a string is expected.

generic

解决方案

  1. 95% 成功率
    string age = 1; protoc --go_out=. *.proto
  2. 90% 成功率
    req := &pb.Person{Age: strconv.Itoa(30)}
  3. 85% 成功率
    if _, ok := req.Age.(string); !ok { return status.Error(codes.InvalidArgument, "age must be string") }

无效尝试

常见但无效的做法:

  1. Continuing to send incorrect type. 100% 失败

    Same error persists.

  2. Using type assertion without changing proto. 80% 失败

    Proto definition determines wire format.

  3. Blaming server for type checking. 50% 失败

    Client must follow proto definition.