{"record":{"id":"c924891605bf0e22","repo":"apache/cassandra","slug":"invalid-type-for-map-key-expecting-s-but-got-s","errorCode":null,"errorMessage":"Invalid type for map key, expecting %s but got %s","messagePattern":"Invalid type for map key, expecting (.+?) but got (.+?)","errorType":"exception","errorClass":"InvalidTypeException","httpStatus":null,"severity":"error","filePath":"src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java","lineNumber":2465,"sourceCode":"        {\n            if (value == null) return null;\n            int i = 0;\n            ByteBuffer[] bbs = new ByteBuffer[2 * value.size()];\n            for (Map.Entry<K, V> entry : value.entrySet())\n            {\n                ByteBuffer bbk;\n                K key = entry.getKey();\n                if (key == null)\n                {\n                    throw new NullPointerException(\"Map keys cannot be null\");\n                }\n                try\n                {\n                    bbk = keyCodec.serialize(key, protocolVersion);\n                }\n                catch (ClassCastException e)\n                {\n                    throw new InvalidTypeException(\n                    String.format(\n                    \"Invalid type for map key, expecting %s but got %s\",\n                    keyCodec.getJavaType(), key.getClass()),\n                    e);\n                }\n                ByteBuffer bbv;\n                V v = entry.getValue();\n                if (v == null)\n                {\n                    throw new NullPointerException(\"Map values cannot be null\");\n                }\n                try\n                {\n                    bbv = valueCodec.serialize(v, protocolVersion);\n                }\n                catch (ClassCastException e)\n                {\n                    throw new InvalidTypeException(","sourceCodeStart":2447,"sourceCodeEnd":2483,"githubUrl":"https://github.com/apache/cassandra/blob/88fd0f6a0eaed8943f05ac9e8f947882b8ddc8f1/src/java/org/apache/cassandra/cql3/functions/types/TypeCodec.java#L2447-L2483","documentation":"Thrown by MapCodec.serialize() when a map key cannot be serialized by the key codec — the ClassCastException from keyCodec.serialize is wrapped in an InvalidTypeException naming the expected Java type and the actual key class. The map's runtime key type does not match the codec's declared key type.","triggerScenarios":"Calling MapCodec.serialize() with a Map whose keys are of the wrong Java type, e.g. a Map<Long,String> passed to a MapCodec<Integer,String>, or raw-typed/mixed maps containing heterogeneous keys.","commonSituations":"Raw-typed maps bypassing generics; mixing Integer and Long keys after numeric parsing; passing a map built for a different column definition; generic type erasure hiding the mismatch until runtime.","solutions":["Ensure the Map's key type matches keyCodec.getJavaType() (e.g. build Map<Integer,String> for an int-keyed map)","Check the actual column type and obtain the codec via TypeCodec.map(keyType, valueType)","Convert keys explicitly (e.g. ((Number)k).intValue()) before serializing"],"exampleFix":"// before\nMap<Long,String> m = ...;\nMapCodec<Integer,String> codec = TypeCodec.map(INT, TEXT);\ncodec.serialize(m, version); // InvalidTypeException\n// after\nMap<Integer,String> fixed = new HashMap<>();\nm.forEach((k,v) -> fixed.put(k.intValue(), v));\ncodec.serialize(fixed, version);","handlingStrategy":"type-guard","validationCode":"for (Object k : map.keySet()) {\n    if (!keyCodec.getJavaType().getRawType().isInstance(k))\n        throw new IllegalArgumentException(\"Key \" + k + \" is \" + k.getClass() + \", expected \" + keyCodec.getJavaType());\n}\ncodec.serialize(map, protocolVersion);","typeGuard":"static <K,V> boolean keysMatch(Map<?,?> m, TypeCodec<K,V> codec) {\n    return m.keySet().stream().allMatch(k ->\n        codec.getJavaType().getRawType().isInstance(k));\n}","tryCatchPattern":"try {\n    codec.serialize(map, protocolVersion);\n} catch (InvalidTypeException e) {\n    if (e.getMessage().startsWith(\"Invalid type for map key\")) {\n        Map<Object,Object> converted = convertKeys(map, keyCodec);\n        return codec.serialize(converted, protocolVersion);\n    }\n    throw e;\n}","preventionTips":["Use fully parameterized generics (no raw Map) so mismatches surface at compile time","Verify the codec's declared types match the column definition (TypeCodec.map(keyType, valType))","Convert numeric key types explicitly (Integer vs Long)"],"tags":["map","type-mismatch","serialization","codec"],"backgroundTag":"type-mismatch","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"}