{"record":{"id":"c58bec322aa3dd71","repo":"apple/pkl","slug":"cannot-convert-pkl-object-to-java-object-npkl-typ","errorCode":null,"errorMessage":"Cannot convert Pkl object to Java object.%nPkl type             : %s%nJava type            : %s%nMissing Pkl property : %s%nActual Pkl properties: %s","messagePattern":"Cannot convert Pkl object to Java object\\.%nPkl type             : (.+?)%nJava type            : (.+?)%nMissing Pkl property : (.+?)%nActual Pkl properties: (.+?)","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-java/src/main/java/org/pkl/config/java/mapper/PObjectToDataObject.java","lineNumber":196,"sourceCode":"\n    @Override\n    public T convert(Composite value, ValueMapper valueMapper) {\n      var properties = value.getProperties();\n      var args = new Object[parameters.size()];\n      var i = 0;\n\n      for (var param : parameters) {\n        var property = properties.get(param.first);\n        if (property == null) {\n          var message =\n              String.format(\n                  \"Cannot convert Pkl object to Java object.\"\n                      + \"%nPkl type             : %s\"\n                      + \"%nJava type            : %s\"\n                      + \"%nMissing Pkl property : %s\"\n                      + \"%nActual Pkl properties: %s\",\n                  value.getClassInfo(), targetType.getTypeName(), param.first, properties.keySet());\n          throw new ConversionException(message);\n        }\n\n        try {\n          var cachedPropertyType = cachedPropertyTypes[i];\n          if (!cachedPropertyType.isExactClassOf(property)) {\n            cachedPropertyType = PClassInfo.forValue(property);\n            cachedPropertyTypes[i] = cachedPropertyType;\n            cachedConverters[i] = valueMapper.getConverter(cachedPropertyType, param.second);\n          }\n          var cachedConverter = cachedConverters[i];\n          assert cachedConverter != null;\n          args[i] = cachedConverter.convert(property, valueMapper);\n          i += 1;\n        } catch (ConversionException e) {\n          throw new ConversionException(\n              String.format(\n                  \"Error converting property `%s` in Pkl object of type `%s` \"\n                      + \"to equally named constructor parameter in Java class `%s`: \"","sourceCodeStart":178,"sourceCodeEnd":214,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-java/src/main/java/org/pkl/config/java/mapper/PObjectToDataObject.java#L178-L214","documentation":"When converting a Pkl object (Composite) to a Java data object, every constructor parameter must be matched by an equally named Pkl property. This error is thrown when a required Pkl property is absent from the object being converted; the message lists the Pkl type, target Java type, the missing property name, and the properties actually present.","triggerScenarios":"Calling Converter.convert (via ValueMapper/TypeMapping) on a Pkl Composite that lacks a property whose name equals a Java constructor parameter — e.g. the Pkl module was amended/renamed, the property is nullable-and-absent, or the Java class was updated with a new constructor parameter.","commonSituations":"Pkl schema and Java data class drifted out of sync after code regeneration; property renamed in Pkl but not in the Java constructor; missing ConstructorProperties/@Named annotations causing a wrong parameter name that doesn't match any Pkl property; consuming a Pkl object with `...` amendments that removed a property.","solutions":["Add the missing property (with a value) to the Pkl object, or set a default in the Pkl class so it is always present.","Align the Java class with the Pkl schema: regenerate the data classes with pkl-codegen-java or rename the constructor parameter to match the Pkl property name.","Verify the Java constructor exposes correct parameter names (compile with -parameters or use @java.beans.ConstructorProperties) so names match the Pkl properties exactly.","If the property is genuinely optional in Java, change the Pkl type to have a default value, or write a custom Converter for the type."],"exampleFix":"// before: Pkl object missing property\nserver { host = \"localhost\" }\n// Java: Server(String host, int port)\n\n// after: provide the missing property\nserver {\n  host = \"localhost\"\n  port = 8080\n}","handlingStrategy":"validation","validationCode":"// before converting, check all constructor params exist as Pkl properties\nvar missing = paramNames.stream()\n    .filter(n -> !pklObject.getProperties().containsKey(n))\n    .toList();\nif (!missing.isEmpty()) throw new IllegalStateException(\"Missing Pkl properties: \" + missing);","typeGuard":null,"tryCatchPattern":"try {\n  return converter.convert(pklObject, valueMapper);\n} catch (ConversionException e) {\n  if (e.getMessage().contains(\"Missing Pkl property\")) {\n    throw new ConfigSchemaMismatch(e.getMessage(), e);\n  }\n  throw e;\n}","preventionTips":["Regenerate Java data classes with pkl-codegen-java whenever the Pkl schema changes","Give Pkl properties default values so they are always present in evaluations","Compile with -parameters or use @ConstructorProperties for reliable name matching","Add a smoke test that converts a representative Pkl object to each data class"],"tags":["config-mapping","missing-property","type-mapping","java","pkl"],"backgroundTag":"missing-required-config-field","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"}