{"record":{"id":"77700225cc796cdb","repo":"apache/beam","slug":"error-decoding-bson-w","errorCode":null,"errorMessage":"error decoding BSON: %w","messagePattern":"error decoding BSON: %w","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"sdks/go/pkg/beam/io/mongodbio/coder.go","lineNumber":74,"sourceCode":"\treturn encodeBSON(in)\n}\nfunc decodeRange(in []byte) (idRange, error) {\n\treturn decodeBSON[idRange](in)\n}\n\nfunc encodeBSON[T any](in T) ([]byte, error) {\n\tout, err := bson.Marshal(in)\n\tif err != nil {\n\t\treturn nil, fmt.Errorf(\"error encoding BSON: %w\", err)\n\t}\n\n\treturn out, nil\n}\n\nfunc decodeBSON[T any](in []byte) (T, error) {\n\tvar out T\n\tif err := bson.Unmarshal(in, &out); err != nil {\n\t\treturn out, fmt.Errorf(\"error decoding BSON: %w\", err)\n\t}\n\n\treturn out, nil\n}\n\nfunc encodeObjectID(objectID primitive.ObjectID) []byte {\n\treturn objectID[:]\n}\n\nfunc decodeObjectID(bytes []byte) primitive.ObjectID {\n\tvar out primitive.ObjectID\n\n\tcopy(out[:], bytes[:])\n\n\treturn out\n}\n","sourceCodeStart":56,"sourceCodeEnd":91,"githubUrl":"https://github.com/apache/beam/blob/12126d8942aaf848030c478b4c6a28c6af861c66/sdks/go/pkg/beam/io/mongodbio/coder.go#L56-L91","documentation":"Returned by the generic helper decodeBSON in mongodbio when bson.Unmarshal fails to deserialize stored BSON bytes into the target type. This typically means the bytes are corrupt/truncated or the target Go type no longer matches the schema that was encoded.","triggerScenarios":"Calling decodeBSON (via decodeRestriction/decodeRange) on state bytes produced with an older/different struct schema, or on corrupted checkpoint state.","commonSituations":"Changing restriction/range struct fields between pipeline resume/checkpoint runs; upgrading mongo-driver with changed BSON handling; truncated state bytes.","solutions":["Ensure the restriction/range struct shape is unchanged from when the state was encoded (schema evolution breaks decode).","Inspect the wrapped bson error to identify the mismatched field/type.","If resuming an old checkpoint is not required, restart the pipeline fresh to re-encode state.","Add bson tags so old and new field names stay compatible across versions."],"exampleFix":"// before: renamed field breaks decode of old state\nsplitFn struct { offset int64 }\n\n// after: keep encoded name\ntype splitFn struct { Start int64 `bson:\"start\"`; offset int64 }","handlingStrategy":"try-catch","validationCode":"// verify round-trip after any schema change:\nout, err := bson.Marshal(r)\nif err == nil { _, err = bson.Unmarshal(out, &target) }\n// err != nil => schema drift, fix before deploying","typeGuard":null,"tryCatchPattern":"r, err := decodeBSON[splitRestriction](state)\nif err != nil {\n    return fmt.Errorf(\"decoding split restriction (schema changed?): %w\", err)\n}","preventionTips":["Never rename or remove fields in restriction/range structs without keeping old bson tags.","Add bson tags up front so encodings remain stable across refactors.","Test encode->decode round trips in unit tests after any struct change.","Avoid resuming checkpoints from pipelines built with incompatible versions."],"tags":["mongodb","bson","deserialization","go"],"backgroundTag":"json-unmarshal-failed","analyzedSha":"12126d8942aaf848030c478b4c6a28c6af861c66","analyzedAt":"2026-09-13T01:50:10.254Z","contentChangedAt":"2026-09-13T01:50:10.254Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}