{"record":{"id":"d72e24c004bb2fc9","repo":"jwtk/jjwt","slug":"constant-s-does-not-exist-in-enum-type-s","errorCode":null,"errorMessage":"constant [%s] does not exist in enum type %s","messagePattern":"constant \\[(.+?)\\] does not exist in enum type (.+?)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"api/src/main/java/io/jsonwebtoken/lang/Objects.java","lineNumber":213,"sourceCode":"\n    /**\n     * Case insensitive alternative to {@link Enum#valueOf(Class, String)}.\n     *\n     * @param <E>        the concrete Enum type\n     * @param enumValues the array of all Enum constants in question, usually per Enum.values()\n     * @param constant   the constant to get the enum value of\n     * @return the enum constant of the specified enum type with the specified case-insensitive name\n     * @throws IllegalArgumentException if the given constant is not found in the given array\n     *                                  of enum values. Use {@link #containsConstant(Enum[], String)} as a guard to\n     *                                  avoid this exception.\n     */\n    public static <E extends Enum<?>> E caseInsensitiveValueOf(E[] enumValues, String constant) {\n        for (E candidate : enumValues) {\n            if (candidate.toString().equalsIgnoreCase(constant)) {\n                return candidate;\n            }\n        }\n        throw new IllegalArgumentException(\n                String.format(\"constant [%s] does not exist in enum type %s\",\n                        constant, enumValues.getClass().getComponentType().getName()));\n    }\n\n    /**\n     * Append the given object to the given array, returning a new array\n     * consisting of the input array contents plus the given object.\n     *\n     * @param array the array to append to (can be <code>null</code>)\n     * @param <A>   the type of each element in the specified {@code array}\n     * @param obj   the object to append\n     * @param <O>   the type of the specified object, which must be equal to or extend the <code>&lt;A&gt;</code> type.\n     * @return the new array (of the same component type; never <code>null</code>)\n     */\n    public static <A, O extends A> A[] addObjectToArray(A[] array, O obj) {\n        Class<?> compType = Object.class;\n        if (array != null) {\n            compType = array.getClass().getComponentType();","sourceCodeStart":195,"sourceCodeEnd":231,"githubUrl":"https://github.com/jwtk/jjwt/blob/fb71496164c71442d08adec4571d9616ed5e1b8d/api/src/main/java/io/jsonwebtoken/lang/Objects.java#L195-L231","documentation":"This IllegalArgumentException is thrown by Objects.caseInsensitiveValueOf when none of the supplied enum constants matches the given string case-insensitively via toString(). The library uses it to convert user-provided strings (e.g. algorithm names, header values) into enum values and fails fast when the string is not a valid constant.","triggerScenarios":"Calling caseInsensitiveValueOf(enumValues, constant) with a string that does not equal (ignoring case) any constant's toString(), e.g. a typo'd algorithm name like \"HS356\" or extra whitespace.","commonSituations":"Parsing user/config-supplied enum names into JWT signature algorithms, compression or claim enum types; typos, wrong case assumptions with trailing spaces, or strings from external systems.","solutions":["Fix the input string to match a valid enum constant (case-insensitive), e.g. \"HS256\"","Trim and normalize the string before calling caseInsensitiveValueOf","Pre-validate with a loop over the enum array checking toString().equalsIgnoreCase(input) to produce a friendlier message","Catch IllegalArgumentException and surface the list of valid constants to the user"],"exampleFix":"// before\nSigAlg alg = Objects.caseInsensitiveValueOf(SigAlg.values(), rawAlg); // throws on typo\n// after\nString normalized = rawAlg == null ? \"\" : rawAlg.trim();\nSigAlg alg = Objects.caseInsensitiveValueOf(SigAlg.values(), normalized);","handlingStrategy":"validation","validationCode":"boolean valid = java.util.Arrays.stream(SigAlg.values()).anyMatch(e -> e.toString().equalsIgnoreCase(input));\nif (!valid) throw new IllegalArgumentException(\"Invalid value: \" + input);","typeGuard":"static <E extends Enum<?>> boolean isValidConstant(E[] values, String s) {\n    return s != null && java.util.Arrays.stream(values).anyMatch(e -> e.toString().equalsIgnoreCase(s));\n}","tryCatchPattern":"try {\n    alg = Objects.caseInsensitiveValueOf(SigAlg.values(), input);\n} catch (IllegalArgumentException e) {\n    throw new IllegalArgumentException(\"Unknown algorithm '\" + input + \"', expected one of: \" + java.util.Arrays.toString(SigAlg.values()), e);\n}","preventionTips":["Trim and normalize user-supplied enum strings before conversion","Pre-validate against Enum.valueOf with a toUpperCase/known-list check","Return the list of valid constants in error messages","Add unit tests covering all external input strings mapped to enums"],"tags":["java","enum","argument-validation"],"backgroundTag":"invalid-enum-value","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"}