{"record":{"id":"b91d37dbcc31672c","repo":"theonedev/onedev","slug":"cannot-convert-s-to-enum-constant-of-type-s","errorCode":null,"errorMessage":"Cannot convert '%s' to enum constant of type '%s'.","messagePattern":"Cannot convert '(.+?)' to enum constant of type '(.+?)'\\.","errorType":"validation","errorClass":"StringValueConversionException","httpStatus":null,"severity":"error","filePath":"server-core/src/main/java/org/apache/wicket/util/string/Strings.java","lineNumber":1581,"sourceCode":"\t *\n\t * @param value\n\t *            the value to convert to an enum value\n\t * @param enumClass\n\t *            the enum type\n\t * @return an enum value\n\t */\n\tpublic static <T extends Enum<T>> T toEnum(final CharSequence value, final Class<T> enumClass)\n\t{\n\t\tArgs.notNull(enumClass, \"enumClass\");\n\t\tArgs.notNull(value, \"value\");\n\n\t\ttry\n\t\t{\n\t\t\treturn Enum.valueOf(enumClass, value.toString());\n\t\t}\n\t\tcatch (Exception e)\n\t\t{\n\t\t\tthrow new StringValueConversionException(\n\t\t\t\t\tString.format(\"Cannot convert '%s' to enum constant of type '%s'.\", value, enumClass), e);\n\t\t}\n\t}\n\n\t/**\n\t * Returns the original string if this one is not empty (i.e. {@link #isEmpty(CharSequence)} returns false), \n\t * otherwise the default one is returned. The default string might be itself an empty one.\n\t * \n\t * @param originalString\n\t * \t\t\t\tthe original sting value\n\t * @param defaultValue\n\t * \t\t\t\tthe default string to return if the original is empty\n\t * @return \tthe original string value if not empty, the default one otherwise\n\t */\n\tpublic static String defaultIfEmpty(String originalString, String defaultValue)\n\t{\n\t\treturn isEmpty(originalString) ? defaultValue : originalString;\t\t\n\t}","sourceCodeStart":1563,"sourceCodeEnd":1599,"githubUrl":"https://github.com/theonedev/onedev/blob/d44925c47c37992c828ea673a5f9620539bc3ff2/server-core/src/main/java/org/apache/wicket/util/string/Strings.java#L1563-L1599","documentation":"Strings.toEnum converts a string value to an enum constant via Enum.valueOf(enumClass, value). If value is null, not a valid constant name, or (in newer JDKs) not an exact case match, the exception is wrapped in a StringValueConversionException with the message 'Cannot convert %s to enum constant of type %s'. The original failure (NullPointerException/IllegalArgumentException) is chained as the cause.","triggerScenarios":"Calling Strings.toEnum(MyEnum.class, s) with a string that does not exactly match an enum constant name — wrong case, extra whitespace, renamed constants, or a null/absent value.","commonSituations":"Config values written in lowercase ('debug' vs 'DEBUG'); enum constants renamed after a Wicket/dependency upgrade; persisted old values no longer present in the enum; whitespace from property files.","solutions":["Make the input exactly match a constant name (trim, then correct the case)","Use a case-insensitive lookup: Arrays.stream(EnumClass.values()).filter(e -> e.name().equalsIgnoreCase(v.trim())).findFirst()","Handle the absent value before conversion to avoid the NullPointerException cause","Update stored config/data to use current constant names after enum changes"],"exampleFix":"// before\nMode m = Strings.toEnum(Mode.class, cfg.get(\"mode\")); // \"debug\" -> throw\n// after\nString v = cfg.get(\"mode\");\nMode m = Mode.valueOf(v == null ? \"DEBUG\" : v.trim().toUpperCase(Locale.ROOT));","handlingStrategy":"validation","validationCode":"// Java\nstatic <T extends Enum<T>> T safeToEnum(Class<T> type, String v, T fallback) {\n    if (v == null) return fallback;\n    for (T e : type.getEnumConstants()) {\n        if (e.name().equalsIgnoreCase(v.trim())) return e;\n    }\n    return fallback;\n}","typeGuard":null,"tryCatchPattern":"try {\n    mode = Strings.toEnum(Mode.class, value);\n} catch (StringValueConversionException e) {\n    log.warn(\"Unknown enum value '{}' for type {}\", value, Mode.class.getSimpleName());\n    mode = Mode.DEFAULT;\n}","preventionTips":["Trim and canonicalize case before conversion — Enum.valueOf is case-sensitive","Search values() case-insensitively when accepting user/config input","Migrate stored values when enum constants are renamed","Reject unknown values at config-load time with clear messages"],"tags":["wicket","string-conversion","enum"],"backgroundTag":"invalid-enum-value","analyzedSha":"d44925c47c37992c828ea673a5f9620539bc3ff2","analyzedAt":"2026-09-06T07:18:27.995Z","contentChangedAt":"2026-09-06T07:18:27.995Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}