{"id":"3b7fb5f83b4f67d0","repo":"jackc/pgx","slug":"invalid-length-for-copyoutresponse-overallformat","errorCode":null,"errorMessage":"invalid length for CopyOutResponse.OverallFormat","messagePattern":"invalid length for CopyOutResponse\\.OverallFormat","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgproto3/copy_out_response.go","lineNumber":90,"sourceCode":"}\n\n// UnmarshalJSON implements encoding/json.Unmarshaler.\nfunc (dst *CopyOutResponse) 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 CopyOutResponse.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_out_response.go#L72-L97","documentation":"Returned by CopyOutResponse.UnmarshalJSON when the JSON `OverallFormat` field is not exactly one character. The wire field is a single byte (0 = text, 1 = binary), so the JSON form must be a one-char string.","triggerScenarios":"`json.Unmarshal(data, &copyOut)` where `OverallFormat` has length != 1 — e.g. `{\"OverallFormat\": \"text\"}` or `{\"OverallFormat\": 0}` (number).","commonSituations":"Fixtures, snapshot tests, or proxy tooling encode OverallFormat as a word or numeric literal. A hand-edited or generated JSON with the field omitted (empty after default) also fails.","solutions":["Use the single-char wire form: \"0\" (text) or \"1\" (binary).","Map words to codes: `\"text\" -> \"0\"`, `\"binary\" -> \"1\"`.","Regenerate fixtures from MarshalJSON output.","Pre-validate the JSON shape before unmarshalling."],"exampleFix":"// before\nraw := []byte(`{\"OverallFormat\":\"text\",\"ColumnFormatCodes\":[0]}`)\nerr := json.Unmarshal(raw, &co) // error\n\n// after\nraw := []byte(`{\"OverallFormat\":\"0\",\"ColumnFormatCodes\":[0]}`)\nerr := json.Unmarshal(raw, &co)","handlingStrategy":"validation","validationCode":"func validateCopyOutOverallFormat(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 co pgproto3.CopyOutResponse\nif err := json.Unmarshal(raw, &co); err != nil {\n    // clarify OverallFormat must be \"0\"/\"1\"\n    return err\n}","preventionTips":["Emit OverallFormat as \"0\" (text) or \"1\" (binary).","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}