{"id":"1974f9985a3f5d1a","repo":"jackc/pgx","slug":"invalid-length-for-describe-objecttype","errorCode":null,"errorMessage":"invalid length for Describe.ObjectType","messagePattern":"invalid length for Describe\\.ObjectType","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"pgproto3/describe.go","lineNumber":74,"sourceCode":"\t})\n}\n\n// UnmarshalJSON implements encoding/json.Unmarshaler.\nfunc (dst *Describe) 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\tif len(msg.ObjectType) != 1 {\n\t\treturn errors.New(\"invalid length for Describe.ObjectType\")\n\t}\n\n\tdst.ObjectType = msg.ObjectType[0]\n\tdst.Name = msg.Name\n\treturn nil\n}\n","sourceCodeStart":56,"sourceCodeEnd":81,"githubUrl":"https://github.com/jackc/pgx/blob/ec1a0befd22592cffffdeeb0a50311b506372f4c/pgproto3/describe.go#L56-L81","documentation":"Returned by Describe.UnmarshalJSON when the JSON `ObjectType` field is not exactly one character. On the wire ObjectType is a single byte ('S' = prepared statement, 'P' = portal), so the JSON form must be a one-char string. Longer words or empty strings are rejected.","triggerScenarios":"`json.Unmarshal(data, &describe)` where `ObjectType` has length != 1 — e.g. `{\"ObjectType\": \"portal\"}` or `{\"ObjectType\": \"\"}`.","commonSituations":"Fixtures or replay tools serialise ObjectType as a full word (\"statement\"/\"portal\") or a numeric code. Hand-edited JSON with the field missing also triggers it.","solutions":["Use the single-char wire form: \"S\" (prepared statement) or \"P\" (portal).","Map full words before unmarshalling: `\"statement\" -> \"S\"`, `\"portal\" -> \"P\"`.","Regenerate fixtures with MarshalJSON so the field shape matches.","Pre-validate with a schema requiring a one-char ObjectType."],"exampleFix":"// before\nraw := []byte(`{\"ObjectType\":\"portal\",\"Name\":\"p1\"}`)\nerr := json.Unmarshal(raw, &d) // error\n\n// after\nraw := []byte(`{\"ObjectType\":\"P\",\"Name\":\"p1\"}`)\nerr := json.Unmarshal(raw, &d)","handlingStrategy":"validation","validationCode":"func validateDescribeJSON(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 isValidDescribeObjectType(s string) bool {\n\treturn len(s) == 1 && (s == \"S\" || s == \"P\")\n}","tryCatchPattern":"var d pgproto3.Describe\nif err := json.Unmarshal(raw, &d); err != nil {\n    // clarify ObjectType must be \"S\"/\"P\"\n    return err\n}","preventionTips":["Emit ObjectType as \"S\" (statement) or \"P\" (portal).","Generate fixtures from MarshalJSON.","Add a schema check for one-char ObjectType."],"tags":["pgproto3","json","serialization","validation","describe"],"analyzedSha":"ec1a0befd22592cffffdeeb0a50311b506372f4c","analyzedAt":"2026-08-04T22:52:11.263Z","schemaVersion":2}