{"id":"dc3f0acd14f12f5e","repo":"jackc/pgx","slug":"invalid-length-for-copyinresponse-overallformat","errorCode":null,"errorMessage":"invalid length for CopyInResponse.OverallFormat","messagePattern":"invalid length for CopyInResponse\\.OverallFormat","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgproto3/copy_in_response.go","lineNumber":90,"sourceCode":"}\n\n// UnmarshalJSON implements encoding/json.Unmarshaler.\nfunc (dst *CopyInResponse) UnmarshalJSON(data []byte) error {\n\t// Ignore null, like in the main JSON package.\n\tif string(data) == \"null\" {\n\t\treturn nil\n\t}\n\n\tvar msg struct {\n\t\tOverallFormat     string\n\t\tColumnFormatCodes []uint16\n\t}\n\tif err := json.Unmarshal(data, &msg); err != nil {\n\t\treturn err\n\t}\n\n\tif len(msg.OverallFormat) != 1 {\n\t\treturn errors.New(\"invalid length for CopyInResponse.OverallFormat\")\n\t}\n\n\tdst.OverallFormat = msg.OverallFormat[0]\n\tdst.ColumnFormatCodes = msg.ColumnFormatCodes\n\treturn nil\n}\n","sourceCodeStart":72,"sourceCodeEnd":97,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgproto3/copy_in_response.go#L72-L97","documentation":"Returned by CopyInResponse.UnmarshalJSON when the JSON `OverallFormat` field is not exactly one character. OverallFormat is a single wire byte (0 = text, 1 = binary), so the JSON form must be a one-char string. Words like \"text\" or numeric literals fail.","triggerScenarios":"`json.Unmarshal(data, &copyIn)` where `OverallFormat` has length != 1 — e.g. `{\"OverallFormat\": \"binary\"}` or `{\"OverallFormat\": 1}`.","commonSituations":"Replay fixtures, snapshot tests, or proxy tooling serialise OverallFormat as a word or number rather than the one-char string. Hand-edited JSON where the field is omitted (empty string) also triggers it.","solutions":["Use the single-char wire form: \"0\" (text) or \"1\" (binary).","Map words to codes before unmarshalling: `\"text\" -> \"0\"`, `\"binary\" -> \"1\"`.","Regenerate fixtures with MarshalJSON so the shape always matches.","Pre-validate with a schema requiring a one-char OverallFormat string."],"exampleFix":"// before\nraw := []byte(`{\"OverallFormat\":\"binary\",\"ColumnFormatCodes\":[1]}`)\nerr := json.Unmarshal(raw, &ci) // error\n\n// after\nraw := []byte(`{\"OverallFormat\":\"1\",\"ColumnFormatCodes\":[1]}`)\nerr := json.Unmarshal(raw, &ci)","handlingStrategy":"validation","validationCode":"func validateCopyInOverallFormat(raw []byte) error {\n\tvar probe struct{ OverallFormat string }\n\tif err := json.Unmarshal(raw, &probe); err != nil { return err }\n\tif len(probe.OverallFormat) != 1 {\n\t\treturn fmt.Errorf(\"OverallFormat must be one char (\\\"0\\\"/\\\"1\\\"), got %q\", probe.OverallFormat)\n\t}\n\treturn nil\n}","typeGuard":"func isValidCopyOverallFormat(s string) bool {\n\treturn len(s) == 1 && (s == \"0\" || s == \"1\")\n}","tryCatchPattern":"var ci pgproto3.CopyInResponse\nif err := json.Unmarshal(raw, &ci); err != nil {\n    // clarify OverallFormat must be \"0\"/\"1\"\n    return err\n}","preventionTips":["Emit OverallFormat as \"0\" (text) or \"1\" (binary) — never a word or number.","Regenerate fixtures from MarshalJSON.","Map words to codes for incoming JSON."],"tags":["pgproto3","json","serialization","validation","copy"],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}