{"id":"2aff90fc8fd05a60","repo":"apache/kafka","slug":"expected-value-to-be-a-16-bit-integer-short-but","errorCode":null,"errorMessage":"Expected value to be a 16-bit integer (short), but it was a value.getClass().getName()","messagePattern":"Expected value to be a 16-bit integer \\(short\\), but it was a value\\.getClass\\(\\)\\.getName\\(\\)","errorType":"validation","errorClass":"ConfigException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java","lineNumber":750,"sourceCode":"                    if (value instanceof String)\n                        return trimmed;\n                    else\n                        throw new ConfigException(name, value, \"Expected value to be a string, but it was a \" + value.getClass().getName());\n                case INT:\n                    if (value instanceof Integer) {\n                        return value;\n                    } else if (value instanceof String) {\n                        return Integer.parseInt(trimmed);\n                    } else {\n                        throw new ConfigException(name, value, \"Expected value to be a 32-bit integer, but it was a \" + value.getClass().getName());\n                    }\n                case SHORT:\n                    if (value instanceof Short) {\n                        return value;\n                    } else if (value instanceof String) {\n                        return Short.parseShort(trimmed);\n                    } else {\n                        throw new ConfigException(name, value, \"Expected value to be a 16-bit integer (short), but it was a \" + value.getClass().getName());\n                    }\n                case LONG:\n                    if (value instanceof Integer)\n                        return ((Integer) value).longValue();\n                    if (value instanceof Long)\n                        return value;\n                    else if (value instanceof String)\n                        return Long.parseLong(trimmed);\n                    else\n                        throw new ConfigException(name, value, \"Expected value to be a 64-bit integer (long), but it was a \" + value.getClass().getName());\n                case DOUBLE:\n                    if (value instanceof Number)\n                        return ((Number) value).doubleValue();\n                    else if (value instanceof String)\n                        return Double.parseDouble(trimmed);\n                    else\n                        throw new ConfigException(name, value, \"Expected value to be a double, but it was a \" + value.getClass().getName());\n                case LIST:","sourceCodeStart":732,"sourceCodeEnd":768,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java#L732-L768","documentation":"Thrown by ConfigDef.parseType in the SHORT branch when the supplied value is neither a java.lang.Short nor a java.lang.String. Type.SHORT accepts a boxed Short (returned as-is) or a String parsed via Short.parseShort(trimmed); any other runtime type is rejected. Kafka declares only a handful of keys as Type.SHORT (for example the replication-related defaults in broker config), and because Java integer literals are int, this fires frequently when callers pass an unqualified int into a SHORT slot.","triggerScenarios":"Passing an Integer, Long, or non-String/non-Short object for a Type.SHORT config key such as replica.fetch.max.bytes (where applicable), message.max.bytes (INT, not SHORT - check the actual key), or any broker/client key declared with Type.SHORT; building config programmatically with int literals.","commonSituations":"Programmatic construction where a helper returns int/long and the value is placed in a SHORT slot; Java does not have short literals (an unqualified 1024 is an int), so cfg.put(\"some.short.key\", 1024) boxes to Integer and fails; YAML/JSON sources decoding numbers as Integer or Long.","solutions":["Box the value as a Short explicitly: (short) value or Short.valueOf(...), or pass it as a String so Short.parseShort handles it.","When reading config from a typed source, emit the value as a string for SHORT-declared keys.","Audit which keys are actually Type.SHORT in the relevant ConfigDef (most byte-size configs are INT or LONG) and only coerce those."],"exampleFix":"// before\nMap<String,Object> cfg = new HashMap<>();\ncfg.put(\"some.short.key\", 1024); // Integer -> ConfigException (SHORT branch)\n\n// after\nMap<String,Object> cfg = new HashMap<>();\ncfg.put(\"some.short.key\", (short) 1024);  // Short\ncfg.put(\"some.short.key\", \"1024\");        // or String, parsed by Short.parseShort","handlingStrategy":"type-guard","validationCode":"// A SHORT config (e.g. \"transaction.timeout.ms\", \"fetch.min.bytes\" where short) got neither Short nor numeric String.\nObject v = props.get(name);\nif (v != null) {\n    if (v instanceof Short) { /* ok */ }\n    else if (v instanceof String) {\n        try { Short.parseShort(((String) v).trim()); }\n        catch (NumberFormatException nfe) {\n            throw new IllegalArgumentException(\"Config '\" + name + \"' (SHORT) is not a 16-bit integer: \" + v, nfe);\n        }\n    } else if (v instanceof Integer) {\n        int i = (Integer) v;\n        if (i < Short.MIN_VALUE || i > Short.MAX_VALUE) {\n            throw new IllegalArgumentException(\"Config '\" + name + \"' (SHORT) out of range: \" + i);\n        }\n        props.put(name, (short) i);\n    } else if (v instanceof Number) {\n        props.put(name, ((Number) v).shortValue());\n    } else {\n        throw new IllegalArgumentException(\n            \"Config '\" + name + \"' (SHORT) must be Short or numeric String, got \" + v.getClass().getName());\n    }\n}","typeGuard":"static boolean isShortConfigValue(Object v) {\n    if (v == null) return true;\n    if (v instanceof Short) return true;\n    if (v instanceof String) {\n        try { Short.parseShort(((String) v).trim()); return true; }\n        catch (NumberFormatException e) { return false; }\n    }\n    return false; // Integer/Long are NOT accepted by ConfigDef.parseType SHORT\n}","tryCatchPattern":null,"preventionTips":["ConfigDef accepts only java.lang.Short or a numeric String for SHORT - an Integer value will throw; coerce first.","Range-check against [-32768, 32767] before assignment; short configs silently overflow if you cast blindly.","Prefer numeric String values in properties files so Kafka performs the parse with correct range semantics.","When bridging JSON/YAML numbers (often Long/Integer), explicitly narrow to short after a bounds check."],"tags":["config","short","type-coercion","programmatic"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}