{"record":{"id":"6c6025155419cafc","repo":"apache/dubbo","slug":"can-not-convert-string-s-to-char-when-convert-s","errorCode":null,"errorMessage":"CAN NOT convert String(%s) to char! when convert String to char, the String MUST only 1 char.","messagePattern":"CAN NOT convert String\\((.+?)\\) to char! when convert String to char, the String MUST only 1 char\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java","lineNumber":66,"sourceCode":"     * <p>\n     * Supported compatible type conversions include (primary types and corresponding wrappers are not listed):\n     * <ul>\n     * <li> String -> char, enum, Date\n     * <li> byte, short, int, long -> byte, short, int, long\n     * <li> float, double -> float, double\n     * </ul>\n     */\n    @SuppressWarnings({\"unchecked\", \"rawtypes\"})\n    public static Object compatibleTypeConvert(Object value, Class<?> type) {\n        if (value == null || type == null || type.isAssignableFrom(value.getClass())) {\n            return value;\n        }\n\n        if (value instanceof String) {\n            String string = (String) value;\n            if (char.class.equals(type) || Character.class.equals(type)) {\n                if (string.length() != 1) {\n                    throw new IllegalArgumentException(String.format(\n                            \"CAN NOT convert String(%s) to char!\"\n                                    + \" when convert String to char, the String MUST only 1 char.\",\n                            string));\n                }\n                return string.charAt(0);\n            }\n            if (type.isEnum()) {\n                return Enum.valueOf((Class<Enum>) type, string);\n            }\n            if (type == BigInteger.class) {\n                return new BigInteger(string);\n            }\n            if (type == BigDecimal.class) {\n                return new BigDecimal(string);\n            }\n            if (type == Short.class || type == short.class) {\n                return new Short(string);\n            }","sourceCodeStart":48,"sourceCodeEnd":84,"githubUrl":"https://github.com/apache/dubbo/blob/3a3043227f5571d25eb2889de5bca22f2914843b/dubbo-common/src/main/java/org/apache/dubbo/common/utils/CompatibleTypeUtils.java#L48-L84","documentation":"Thrown by CompatibleTypeUtils.compatibleTypeConvert when converting a String to char or java.lang.Character but the string's length is not exactly 1. A char holds a single Unicode character; a multi-char or empty string cannot be mapped to one, so the conversion is rejected rather than guessing (e.g. taking charAt(0) silently).","triggerScenarios":"compatibleTypeConvert(stringValue, char.class) or (stringValue, Character.class) where stringValue.length() != 1 — e.g. \"\", \"ab\", \"abc\". The method is used by Dubbo's RPC parameter deserialization when a String-typed value must populate a char field/param.","commonSituations":"An RPC method declares a char/Character parameter but the caller sends a longer string (or empty); a config value mapped to a char field carries multiple characters; JSON/form deserialization of a char from a free-text value. Mismatch between the producer's string and the consumer's char type.","solutions":["Send exactly one character for char/Character parameters; trim or validate the source string to length 1.","If the field should hold longer text, change its type to String on both sides.","Pre-validate: if (stringValue == null || stringValue.length() != 1) reject before the RPC/config binding."],"exampleFix":"// before\nObject c = CompatibleTypeUtils.compatibleTypeConvert(\"abc\", char.class); // throws\n\n// after\nObject c = CompatibleTypeUtils.compatibleTypeConvert(\"a\", char.class);\n// or change the target type to String if multi-char is intended","handlingStrategy":"validation","validationCode":"String s = /* ... */;\nif (s == null || s.length() != 1) {\n    throw new IllegalArgumentException(\"char conversion requires a single-character string: \" + s);\n}\nCompatibleTypeUtils.compatibleTypeConvert(s, char.class);","typeGuard":"static boolean isSingleChar(String s) {\n    return s != null && s.length() == 1;\n}","tryCatchPattern":null,"preventionTips":["Send exactly one character for char/Character parameters.","Change the field type to String if multi-char values are valid.","Validate char-bound values at the RPC/config boundary."],"tags":["type-conversion","rpc","validation","char"],"backgroundTag":null,"analyzedSha":"3a3043227f5571d25eb2889de5bca22f2914843b","analyzedAt":"2026-08-14T00:43:19.853Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}