{"id":"d15f48b1df1f948d","repo":"jackc/pgx","slug":"invalid-length-for-close-objecttype","errorCode":null,"errorMessage":"invalid length for Close.ObjectType","messagePattern":"invalid length for Close\\.ObjectType","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgproto3/close.go","lineNumber":75,"sourceCode":"}\n\n// UnmarshalJSON implements encoding/json.Unmarshaler.\nfunc (dst *Close) 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\tObjectType string\n\t\tName       string\n\t}\n\tif err := json.Unmarshal(data, &msg); err != nil {\n\t\treturn err\n\t}\n\n\tif len(msg.ObjectType) != 1 {\n\t\treturn errors.New(\"invalid length for Close.ObjectType\")\n\t}\n\n\tdst.ObjectType = msg.ObjectType[0]\n\tdst.Name = msg.Name\n\treturn nil\n}\n","sourceCodeStart":57,"sourceCodeEnd":82,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgproto3/close.go#L57-L82","documentation":"Returned by Close.UnmarshalJSON when the JSON `ObjectType` field is not exactly one character long. In the wire protocol ObjectType is a single byte ('S' for prepared statement, 'P' for portal), so the JSON representation requires a single-character string. Anything else (empty string, a multi-char word like \"statement\", or a number) is rejected.","triggerScenarios":"Calling `json.Unmarshal(data, &closeMsg)` on JSON where the `ObjectType` field has length != 1 — e.g. `{\"ObjectType\": \"statement\"}` or `{\"ObjectType\": \"\"}`.","commonSituations":"Test fixtures or recording/replay tools produce JSON with a human-readable ObjectType (\"statement\"/\"portal\") instead of the single-letter wire form. Also seen when a JSON generator omits ObjectType or encodes it as a numeric code.","solutions":["Use the single-character wire form: \"S\" for prepared statement, \"P\" for portal.","If your JSON source uses full words, map them before unmarshalling: `\"statement\" -> \"S\"`, `\"portal\" -> \"P\"`.","Validate the JSON shape with a schema or a pre-unmarshal check that `len(objType) == 1` and report a clearer error to the caller.","Regenerate any recorded fixtures with the library's own MarshalJSON so the field shape always matches."],"exampleFix":"// before\nraw := []byte(`{\"ObjectType\":\"statement\",\"Name\":\"foo\"}`)\nerr := json.Unmarshal(raw, &closeMsg) // error\n\n// after\nraw := []byte(`{\"ObjectType\":\"S\",\"Name\":\"foo\"}`)\nerr := json.Unmarshal(raw, &closeMsg)","handlingStrategy":"validation","validationCode":"func validateCloseJSON(raw []byte) error {\n\tvar probe struct{ ObjectType string }\n\tif err := json.Unmarshal(raw, &probe); err != nil { return err }\n\tif len(probe.ObjectType) != 1 {\n\t\treturn fmt.Errorf(\"ObjectType must be a single char (\\\"S\\\" or \\\"P\\\"), got %q\", probe.ObjectType)\n\t}\n\treturn nil\n}","typeGuard":"func isValidCloseObjectType(s string) bool {\n\treturn len(s) == 1 && (s == \"S\" || s == \"P\")\n}","tryCatchPattern":"var c pgproto3.Close\nif err := json.Unmarshal(raw, &c); err != nil {\n    // log err mentioning ObjectType must be \"S\" or \"P\"\n    return err\n}","preventionTips":["Always emit ObjectType as the single wire character: \"S\" (statement) or \"P\" (portal).","Generate fixtures from MarshalJSON so the field shape is always correct.","Add a JSON-schema check for one-char ObjectType when ingesting third-party JSON."],"tags":["pgproto3","json","serialization","validation","close"],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}