{"record":{"id":"2919a2bf46bcfcb0","repo":"apple/pkl","slug":"cannot-convert-string-s-to-enum-value-of-type","errorCode":null,"errorMessage":"Cannot convert String `%s` to Enum value of type `%s`.","messagePattern":"Cannot convert String `(.+?)` to Enum value of type `(.+?)`\\.","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-java/src/main/java/org/pkl/config/java/mapper/PStringToEnum.java","lineNumber":79,"sourceCode":"        for (Enum<?> value : values) {\n          enumValuesByName.put(value.name(), value);\n        }\n      }\n    }\n\n    @Override\n    public Enum<?> convert(String value, ValueMapper valueMapper) {\n      var enumValue = enumValuesByName.get(value);\n      if (enumValue == null) {\n        enumValue = enumValuesByName.get(CodeGeneratorUtils.toEnumConstantName(value));\n        if (enumValue != null) {\n          enumValuesByName.put(value, enumValue);\n        }\n      }\n      if (enumValue != null) {\n        return enumValue;\n      }\n      throw new ConversionException(\n          String.format(\n              \"Cannot convert String `%s` to Enum value of type `%s`.\",\n              value, enumType.getTypeName()));\n    }\n  }\n}\n","sourceCodeStart":61,"sourceCodeEnd":86,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-java/src/main/java/org/pkl/config/java/mapper/PStringToEnum.java#L61-L86","documentation":"PStringToEnum converts a Pkl String to a Java enum constant by (case-sensitive) name lookup, with a fallback normalization via CodeGeneratorUtils.toEnumConstantName. This error is thrown when neither the raw string nor its normalized form matches any constant of the target enum type.","triggerScenarios":"Converter.convert receiving a String whose value is not an existing enum constant name of enumType — e.g. Pkl config contains \"KiB\" but the Java enum declares KIB, a typo like \"prodction\", or a value added to the Pkl schema but not yet to the Java enum (or vice versa).","commonSituations":"Case mismatch between config value and enum constant; enum extended in Pkl but Java classes not regenerated; hand-written enums with names that don't follow the codegen naming convention; localized or free-text config values where a constrained enum is expected.","solutions":["Correct the string in the Pkl config to exactly match an enum constant name of the target type (the message names the type).","Regenerate the Java enum with pkl-codegen-java if new values were added to the Pkl schema.","Match the naming convention expected by the converter (PascalCase Pkl value like \"KiB\" normalizes to KIB); rename the config value accordingly.","Register a custom Converter if you need lenient/alias mapping between strings and enum constants."],"exampleFix":"// before\nunit = \"kiloBYTES\"\n\n// after: matches enum constant KIB via normalization\nunit = \"KiB\"  // or exactly \"KIB\"","handlingStrategy":"validation","validationCode":"// check the string maps to an enum constant before converting\nboolean isValidEnumValue(String s, Class<? extends Enum<?>> type) {\n  for (Enum<?> c : type.getEnumConstants()) {\n    if (c.name().equals(s)) return true;\n    String norm = CodeGeneratorUtils.toEnumConstantName(s);\n    if (norm != null && c.name().equals(norm)) return true;\n  }\n  return false;\n}","typeGuard":"static boolean isEnumConstant(String s, Class<? extends Enum<?>> type) {\n  return java.util.Arrays.stream(type.getEnumConstants())\n      .anyMatch(c -> c.name().equals(s));\n}","tryCatchPattern":"try {\n  return enumConverter.convert(configValue, valueMapper);\n} catch (ConversionException e) {\n  if (e.getMessage().startsWith(\"Cannot convert String\")) {\n    throw new ConfigLoadException(\n        \"Value '\" + configValue + \"' is not a valid \" + enumType.getSimpleName(), e);\n  }\n  throw e;\n}","preventionTips":["Use a Pkl enum type in the schema instead of a bare String so invalid values fail at evaluation time","Regenerate Java enums from the Pkl schema whenever enum values change","Match the codegen naming convention (Pkl \"KiB\" -> constant KIB)","List allowed values in config error output or docs to reduce typos"],"tags":["enum","string-conversion","type-mapping","java","pkl"],"backgroundTag":"invalid-enum-value","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}