{"record":{"id":"f9ec44e2e92d58f9","repo":"jwtk/jjwt","slug":"cannot-cast-value-getclass-getname-to-col-f9ec44","errorCode":null,"errorMessage":"Cannot cast ${value.getClass().getName()} to ${COLLECTION_TYPE.getName()}<${IDIOMATIC_TYPE.getName()}>: At least one element is not an instance of ${IDIOMATIC_TYPE.getName()}","messagePattern":"Cannot cast (.+?) to (.+?)<(.+?)>: At least one element is not an instance of (.+?)","errorType":"validation","errorClass":"ClassCastException","httpStatus":null,"severity":"error","filePath":"impl/src/main/java/io/jsonwebtoken/impl/lang/DefaultParameter.java","lineNumber":83,"sourceCode":"\n    @SuppressWarnings(\"unchecked\")\n    @Override\n    public T cast(Object value) {\n        if (value != null) {\n            if (COLLECTION_TYPE != null) { // parameter represents a collection, ensure it and its elements are the expected type:\n                if (!COLLECTION_TYPE.isInstance(value)) {\n                    String msg = \"Cannot cast \" + value.getClass().getName() + \" to \" +\n                            COLLECTION_TYPE.getName() + \"<\" + IDIOMATIC_TYPE.getName() + \">\";\n                    throw new ClassCastException(msg);\n                }\n                Collection<?> c = COLLECTION_TYPE.cast(value);\n                if (!c.isEmpty()) {\n                    Object element = c.iterator().next();\n                    if (!IDIOMATIC_TYPE.isInstance(element)) {\n                        String msg = \"Cannot cast \" + value.getClass().getName() + \" to \" +\n                                COLLECTION_TYPE.getName() + \"<\" + IDIOMATIC_TYPE.getName() + \">: At least one \" +\n                                \"element is not an instance of \" + IDIOMATIC_TYPE.getName();\n                        throw new ClassCastException(msg);\n                    }\n                }\n            } else if (!IDIOMATIC_TYPE.isInstance(value)) {\n                String msg = \"Cannot cast \" + value.getClass().getName() + \" to \" + IDIOMATIC_TYPE.getName();\n                throw new ClassCastException(msg);\n            }\n        }\n        return (T) value;\n    }\n\n    @Override\n    public boolean isSecret() {\n        return SECRET;\n    }\n\n    @Override\n    public int hashCode() {\n        return this.ID.hashCode();","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/impl/src/main/java/io/jsonwebtoken/impl/lang/DefaultParameter.java#L65-L101","documentation":"DefaultParameter.cast, after confirming the value is the expected collection type, checks that its elements are instances of the declared element type (IDIOMATIC_TYPE). If at least one element is of the wrong type, a ClassCastException is thrown noting 'At least one element is not an instance of ...'. This only inspects the first element, so it detects a bad first element specifically.","triggerScenarios":"Assigning a Collection whose first element is not of the declared element type, e.g. a List<Object> containing an Integer into a Collection<String> parameter, or a deserialized heterogeneous JSON array.","commonSituations":"JSON arrays with mixed types ([\"a\", 1]); generics erasure letting List<Integer> slip into a List<String> slot; builders taking raw Collection types.","solutions":["Ensure every element matches the declared element type before assignment.","Convert elements explicitly (map each to String/Long as required) before setting the claim.","If the JWT payload genuinely contains mixed types, change the registered parameter element type to a common supertype (e.g. Object)."],"exampleFix":"// before\nList<Object> roles = Arrays.asList(\"admin\", 42);\njwt.claim(\"roles\", roles);\n// after\nList<String> roles = Arrays.asList(\"admin\", \"42\");\njwt.claim(\"roles\", roles);","handlingStrategy":"type-guard","validationCode":"boolean ok = value instanceof Collection\n    && ((Collection<?>) value).stream().allMatch(expectedElemType::isInstance);\nif (!ok) { /* convert or reject */ }","typeGuard":"static <T> boolean allInstanceOf(Collection<?> c, Class<T> t) {\n    return c.stream().allMatch(t::isInstance);\n}","tryCatchPattern":"try {\n    param.cast(collection);\n} catch (ClassCastException e) {\n    // inspect elements, convert or reject the collection\n}","preventionTips":["Never build claim collections from raw JSON arrays without validating element types","Map elements to the declared type explicitly before assignment","Reject or sanitize heterogeneous arrays at the ingestion boundary"],"tags":["classcast","collection","type-mismatch"],"backgroundTag":"type-mismatch","analyzedSha":"fb71496164c71442d08adec4571d9616ed5e1b8d","analyzedAt":"2026-09-09T00:33:09.982Z","contentChangedAt":"2026-09-09T00:33:09.982Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}