{"record":{"id":"fbaa222e2fb04371","repo":"hyperledger/fabric","slug":"could-not-marshal-field-s","errorCode":null,"errorMessage":"could not marshal field %s","messagePattern":"could not marshal field (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"core/chaincode/lifecycle/serializer.go","lineNumber":159,"sourceCode":"\t\tfieldName := value.Type().Field(i).Name\n\t\tfieldValue := value.Field(i)\n\n\t\tkeyName := FieldKey(namespace, name, fieldName)\n\n\t\tstateData := &lb.StateData{}\n\t\tswitch fieldValue.Kind() {\n\t\tcase reflect.String:\n\t\t\tstateData.Type = &lb.StateData_String_{String_: fieldValue.String()}\n\t\tcase reflect.Int64:\n\t\t\tstateData.Type = &lb.StateData_Int64{Int64: fieldValue.Int()}\n\t\tcase reflect.Slice:\n\t\t\tstateData.Type = &lb.StateData_Bytes{Bytes: fieldValue.Bytes()}\n\t\tcase reflect.Pointer:\n\t\t\tvar bin []byte\n\t\t\tif !fieldValue.IsNil() {\n\t\t\t\tbin, err = s.Marshaler.Marshal(fieldValue.Interface().(proto.Message))\n\t\t\t\tif err != nil {\n\t\t\t\t\treturn errors.Wrapf(err, \"could not marshal field %s\", fieldName)\n\t\t\t\t}\n\t\t\t}\n\t\t\tstateData.Type = &lb.StateData_Bytes{Bytes: bin}\n\t\t\t// Note, other field kinds and bad types have already been checked by SerializableChecks\n\t\t}\n\n\t\tmarshaledFieldValue, err := s.Marshaler.Marshal(stateData)\n\t\tif err != nil {\n\t\t\treturn errors.WithMessagef(err, \"could not marshal value for key %s\", keyName)\n\t\t}\n\n\t\tif existingValue, ok := existingKeys[keyName]; !ok || !bytes.Equal(existingValue, marshaledFieldValue) {\n\t\t\terr := state.PutState(keyName, marshaledFieldValue)\n\t\t\tif err != nil {\n\t\t\t\treturn errors.WithMessage(err, \"could not write key into state\")\n\t\t\t}\n\t\t}\n\t\tdelete(existingKeys, keyName)","sourceCodeStart":141,"sourceCodeEnd":177,"githubUrl":"https://github.com/hyperledger/fabric/blob/2736b63f8fd5932511d56fe68b7039d15977f7f6/core/chaincode/lifecycle/serializer.go#L141-L177","documentation":"During Serialize, a non-nil proto pointer field is marshaled with the configured Marshaler; if proto marshaling fails (invalid message, marshaler misconfigured), the error is wrapped as 'could not marshal field <name>'. Unlike index 552, this occurs only for non-nil fields whose contents are invalid.","triggerScenarios":"Serialize on a struct whose proto pointer field is non-nil but internally invalid (e.g. a oneof unset / invalid required data) so s.Marshaler.Marshal returns an error.","commonSituations":"Constructing an EndorsementPolicy/ApplicationPolicy message incorrectly (empty oneof) before committing a chaincode definition; custom Marshaler functions returning errors for certain message types.","solutions":["Fully initialize the proto message including its oneof before calling Serialize","Validate the message with msg.ProtoReflect().IsValid() / proto checks before serializing","Inspect the wrapped inner error (errors.Unwrap) to find the underlying marshal failure","If a custom Marshaler is installed, verify it handles the message type correctly"],"exampleFix":"// before\npolicy := &pb.ApplicationPolicy{} // oneof unset\ns.Serialize(state, metadata, &Def{Policy: policy})\n// after\npolicy := &pb.ApplicationPolicy{Type: &pb.ApplicationPolicy_Policy{Policy: &pb.SignaturePolicyEnvelope{Version: 0}}}\ns.Serialize(state, metadata, &Def{Policy: policy})","handlingStrategy":"try-catch","validationCode":"if fieldMsg != nil && !fieldMsg.ProtoReflect().IsValid() {\n  return errors.New(\"proto field invalid before Serialize\")\n}","typeGuard":"func isMarshalableProto(msg proto.Message) bool { return msg != nil && msg.ProtoReflect().IsValid() }","tryCatchPattern":"err := s.Serialize(state, metadata, def)\nif err != nil && strings.Contains(err.Error(), \"could not marshal field\") {\n  var inner error\n  if e, ok := err.(interface{ Unwrap() error }); ok { inner = e.Unwrap() }\n  // inspect inner marshal error, fix the named field's proto message\n}","preventionTips":["Fully populate proto messages (especially oneofs) before Serialize","Validate messages with IsValid() prior to serialization","Check the wrapped inner error to identify the failing field","Test Serialize paths whenever proto definitions change"],"tags":["fabric","serialization","protobuf","marshal"],"backgroundTag":"proto-marshal-nil","analyzedSha":"2736b63f8fd5932511d56fe68b7039d15977f7f6","analyzedAt":"2026-09-04T08:52:36.465Z","contentChangedAt":"2026-09-04T08:52:36.465Z","schemaVersion":2},"datasetVersion":"2026-09-08T10:18:20.063Z"}