{"record":{"id":"86eadf05023661d3","repo":"apache/cassandra","slug":"malformed-collection-value-s-missing-closing","errorCode":null,"errorMessage":"Malformed collection value \"%s\", missing closing '%s'","messagePattern":"Malformed collection value \"(.+?)\", missing closing '(.+?)'","errorType":"exception","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":2227,"sourceCode":"                    \"Cannot parse collection value from \\\"%s\\\", invalid CQL value at character %d\",\n                    value, idx),\n                    e);\n                }\n\n                l.add(eltCodec.parse(value.substring(idx, n)));\n                idx = n;\n\n                idx = ParseUtils.skipSpaces(value, idx);\n                if (value.charAt(idx) == getClosingChar()) return l;\n                if (value.charAt(idx++) != ',')\n                    throw new InvalidTypeException(\n                    String.format(\n                    \"Cannot parse collection value from \\\"%s\\\", at character %d expecting ',' but got '%c'\",\n                    value, idx, value.charAt(idx)));\n\n                idx = ParseUtils.skipSpaces(value, idx);\n            }\n            throw new InvalidTypeException(\n            String.format(\n            \"Malformed collection value \\\"%s\\\", missing closing '%s'\", value, getClosingChar()));\n        }\n\n        @Override\n        public boolean accepts(Object value)\n        {\n            if (getJavaType().getRawType().isAssignableFrom(value.getClass()))\n            {\n                // runtime type ok, now check element type\n                Collection<?> coll = (Collection<?>) value;\n                if (coll.isEmpty()) return true;\n                Object elt = coll.iterator().next();\n                return eltCodec.accepts(elt);\n            }\n            return false;\n        }\n","sourceCodeStart":2209,"sourceCodeEnd":2245,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L2209-L2245","documentation":"This InvalidTypeException is thrown by the collection codec's parse() method when a CQL literal string representing a collection (list/set) ends before the expected closing character is found. The parser consumed all opening values but ran out of characters, meaning brackets are unbalanced. The library throws it because the string cannot correspond to any valid CQL collection literal.","triggerScenarios":"Calling codec.parse() (or deserializing a string via TypeCodec) with a truncated or unbalanced collection literal such as \"[1,2,3\" or \"['a','b'\" — the closing ']' (or '>' for sets) never appears before end-of-string.","commonSituations":"Manually built CQL literal strings that were truncated by string-slicing or logging; user-supplied input missing a bracket; code that formats collections without escaping/quoting elements properly so a quote swallows the closing bracket.","solutions":["Print the full value being parsed and add the missing closing character","Escape or quote string elements correctly (e.g. 'a''b' for embedded quotes) so brackets are not consumed by a quoted token","Validate the literal balance (count opening/closing chars) before calling parse()","Use format() on a typed collection instead of hand-writing literals"],"exampleFix":"// before\ncodec.parse(\"[1,2,3\"); // InvalidTypeException\n// after\ncodec.parse(\"[1,2,3]\");","handlingStrategy":"validation","validationCode":"static boolean isBalancedCollection(String s) {\n    int depth = 0; boolean inQuote = false;\n    for (int i = 0; i < s.length(); i++) {\n        char c = s.charAt(i);\n        if (c == '\\'' ) inQuote = !inQuote;\n        else if (!inQuote) {\n            if (c == '[' || c == '<') depth++;\n            if (c == ']' || c == '>') depth--;\n        }\n    }\n    return depth == 0 && !inQuote;\n}\nif (!isBalancedCollection(value)) throw new IllegalArgumentException(\"unbalanced collection literal: \" + value);\ncodec.parse(value);","typeGuard":"boolean isClosedCollection(String s) {\n    return s != null && !s.isEmpty() && isBalancedCollection(s);\n}","tryCatchPattern":"try {\n    T v = codec.parse(literal);\n} catch (InvalidTypeException e) {\n    log.error(\"Malformed collection literal: {}\", e.getMessage());\n    throw new IllegalArgumentException(\"Fix collection literal syntax\", e);\n}","preventionTips":["Count opening/closing brackets before parsing hand-built literals","Always escape single quotes inside string elements (double them)","Prefer codec.format(typedCollection) over hand-concatenating literals"],"tags":["cql","parsing","codec","invalid-literal"],"backgroundTag":"invalid-argument-format","analyzedSha":"88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1","analyzedAt":"2026-09-10T07:29:22.284Z","contentChangedAt":"2026-09-10T07:29:22.284Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}