{"record":{"id":"c7f40f88e177ad82","repo":"spring-projects/spring-framework","slug":"cannot-convert-value-of-type-to-required-type","errorCode":null,"errorMessage":"Cannot convert value of type '{}' to required type '{}'[ for property '{}']: PropertyEditor [{}] returned inappropriate value of type '{}'","messagePattern":"Cannot convert value of type '(.+?)' to required type '(.+?)'\\[ for property '(.+?)'\\]: PropertyEditor \\[(.+?)\\] returned inappropriate value of type '(.+?)'","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java","lineNumber":266,"sourceCode":"\t\t\t\t\t// but editor couldn't produce the required type...\n\t\t\t\t\tTypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue);\n\t\t\t\t\tif (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) {\n\t\t\t\t\t\treturn (T) conversionService.convert(newValue, sourceTypeDesc, typeDescriptor);\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\t// Definitely doesn't match: throw IllegalArgumentException/IllegalStateException\n\t\t\t\tStringBuilder msg = new StringBuilder();\n\t\t\t\tmsg.append(\"Cannot convert value of type '\").append(ClassUtils.getDescriptiveType(newValue));\n\t\t\t\tmsg.append(\"' to required type '\").append(ClassUtils.getQualifiedName(requiredType)).append('\\'');\n\t\t\t\tif (propertyName != null) {\n\t\t\t\t\tmsg.append(\" for property '\").append(propertyName).append('\\'');\n\t\t\t\t}\n\t\t\t\tif (editor != null) {\n\t\t\t\t\tmsg.append(\": PropertyEditor [\").append(editor.getClass().getName()).append(\n\t\t\t\t\t\t\t\"] returned inappropriate value of type '\").append(\n\t\t\t\t\t\t\tClassUtils.getDescriptiveType(convertedValue)).append('\\'');\n\t\t\t\t\tthrow new IllegalArgumentException(msg.toString());\n\t\t\t\t}\n\t\t\t\telse {\n\t\t\t\t\tmsg.append(\": no matching editors or conversion strategy found\");\n\t\t\t\t\tthrow new IllegalStateException(msg.toString());\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\tif (conversionAttemptEx != null) {\n\t\t\tif (editor == null && !standardConversion && requiredType != null && Object.class != requiredType) {\n\t\t\t\tthrow conversionAttemptEx;\n\t\t\t}\n\t\t\tlogger.debug(\"Original ConversionService attempt failed - ignored since \" +\n\t\t\t\t\t\"PropertyEditor based conversion eventually succeeded\", conversionAttemptEx);\n\t\t}\n\n\t\treturn (T) convertedValue;\n\t}","sourceCodeStart":248,"sourceCodeEnd":284,"githubUrl":"https://github.com/spring-projects/spring-framework/blob/69bf83ad716d0cfc4b0520a19b4d8b24c79d1538/spring-beans/src/main/java/org/springframework/beans/TypeConverterDelegate.java#L248-L284","documentation":"IllegalArgumentException from TypeConverterDelegate when a registered PropertyEditor was found and invoked but its getValue() returned an object whose type is not assignable to the required type. This indicates the PropertyEditor itself is buggy or registered against the wrong source/required type. The message names the offending editor class and the actual type it returned.","triggerScenarios":"A custom PropertyEditor registered for requiredType X whose setAsText/setValue produces a Y; or a built-in editor applied to the wrong type. Reached at line 266 when, after all conversion attempts, ClassUtils.isAssignableValue(requiredType, convertedValue) is still false and an editor participated.","commonSituations":"Hand-written PropertyEditor that returns a primitive wrapper or a parsed subtype; editor registered via @InitBinder against the wrong field type; editor returning null for a primitive required type.","solutions":["Fix the PropertyEditor so getValue() returns an instance assignable to the required type.","Re-check that the editor is registered for the correct requiredType / propertyPath.","Replace the PropertyEditor with a Spring Converter / ConversionService converter, which is type-safe by signature."],"exampleFix":"// before\npublic class MyEditor extends PropertyEditorSupport {\n    public void setAsText(String t) { setValue(Integer.valueOf(t)); } // returns Integer, required Long\n}\n\n// after\npublic class MyEditor extends PropertyEditorSupport {\n    public void setAsText(String t) { setValue(Long.valueOf(t)); }\n}","handlingStrategy":"validation","validationCode":"// Sanity-check a PropertyEditor returns the required type\neditor.setAsText(sample);\nObject v = editor.getValue();\nif (!requiredType.isInstance(v)) {\n    throw new IllegalStateException(\"editor returns \" + v.getClass() + \" not \" + requiredType);\n}","typeGuard":"static boolean editorReturnsType(PropertyEditor e, Class<?> required, String sample) {\n    e.setAsText(sample); return required.isInstance(e.getValue());\n}","tryCatchPattern":"try {\n    wrapper.setPropertyValue(\"field\", raw);\n} catch (IllegalArgumentException ex) {\n    if (ex.getMessage().contains(\"returned inappropriate value\")) { /* fix/replace editor */ }\n    throw ex;\n}","preventionTips":["Unit-test custom PropertyEditors against representative inputs.","Prefer typed Converter implementations over PropertyEditor where possible.","Register editors against the exact required type, not a supertype."],"tags":["type-conversion","property-editor","binding"],"backgroundTag":null,"analyzedSha":"69bf83ad716d0cfc4b0520a19b4d8b24c79d1538","analyzedAt":"2026-08-09T15:32:58.770Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}