{"id":"405b07fc81975f28","repo":"apache/kafka","slug":"about-value-value-does-not-fit-in-a-16-bit","errorCode":null,"errorMessage":"${about}: value ${value} does not fit in a 16-bit signed integer.","messagePattern":"(.+?): value (.+?) does not fit in a 16-bit signed integer\\.","errorType":"exception","errorClass":"RuntimeException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java","lineNumber":89,"sourceCode":"                // It's more traditional to refer to bytes as unsigned,\n                // so we support that here.\n                value -= 128;\n            } else {\n                throw new RuntimeException(about + \": value \" + value +\n                    \" does not fit in an 8-bit signed integer.\");\n            }\n        }\n        if (value < Byte.MIN_VALUE) {\n            throw new RuntimeException(about + \": value \" + value +\n                \" does not fit in an 8-bit signed integer.\");\n        }\n        return (byte) value;\n    }\n\n    public static short jsonNodeToShort(JsonNode node, String about) {\n        int value = jsonNodeToInt(node, about);\n        if ((value < Short.MIN_VALUE) || (value > Short.MAX_VALUE)) {\n            throw new RuntimeException(about + \": value \" + value +\n                \" does not fit in a 16-bit signed integer.\");\n        }\n        return (short) value;\n    }\n\n    public static int jsonNodeToUnsignedShort(JsonNode node, String about) {\n        int value = jsonNodeToInt(node, about);\n        if (value < 0 || value > UNSIGNED_SHORT_MAX) {\n            throw new RuntimeException(about + \": value \" + value +\n                \" does not fit in a 16-bit unsigned integer.\");\n        }\n        return value;\n    }\n\n    public static long jsonNodeToUnsignedInt(JsonNode node, String about) {\n        long value = jsonNodeToLong(node, about);\n        if (value < 0 || value > UNSIGNED_INT_MAX) {\n            throw new RuntimeException(about + \": value \" + value +","sourceCodeStart":71,"sourceCodeEnd":107,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java#L71-L107","documentation":"Thrown by MessageUtil.jsonNodeToShort as a RuntimeException when the parsed integer is outside [Short.MIN_VALUE, Short.MAX_VALUE] (-32768..32767). It guards JSON-driven deserialization of any protocol field typed as a signed 16-bit short; values outside that range cannot be stored losslessly and are rejected rather than silently truncated.","triggerScenarios":"A JSON field mapped to a signed-short protocol message field receives an integer > 32767 or < -32768; jsonNodeToInt parses it and the bounds check fails.","commonSituations":"Topic/replication or api-version fields configured via JSON fixtures with out-of-range numbers; an externally produced JSON config whose author assumed a wider type; typos like '40000' for a value meant to be '4000'.","solutions":["Find the field named in 'about' and bring its value within [-32768, 32767].","If the value is genuinely larger, widen the target field (int) in the message definition or use an unsigned mapping via jsonNodeToUnsignedShort where the protocol allows it (max 65535).","Validate the JSON against the schema before sending."],"exampleFix":"// before\n{ \"port\": 70000 }\n\n// after\n{ \"port\": 9092 }","handlingStrategy":"validation","validationCode":"// MessageUtil.jsonNodeToShort requires [Short.MIN_VALUE, Short.MAX_VALUE] = [-32768, 32767].\nint value = node.asInt();\nif (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {\n    throw new IllegalArgumentException(about + \": value \" + value + \" outside short range [-32768,32767]\");\n}\nshort s = MessageUtil.jsonNodeToShort(node, about);","typeGuard":"static Optional<Short> asShort(com.fasterxml.jackson.databind.JsonNode n) {\n    if (n == null || !n.canConvertToInt()) return Optional.empty();\n    int v = n.asInt();\n    return (v >= Short.MIN_VALUE && v <= Short.MAX_VALUE) ? Optional.of((short) v) : Optional.empty();\n}","tryCatchPattern":"try {\n    short s = MessageUtil.jsonNodeToShort(node, about);\n} catch (RuntimeException e) {\n    log.error(\"{}: short field out of range\", about, e);\n    rejectMessage(about, e);\n}","preventionTips":["Validate JSON int fields that map to `short` against [-32768, 32767] before serialization.","Common Kafka short fields: api keys, error codes, versions; keep hand-authored JSON within these bounds.","Use schema validation (e.g. JSON Schema with minimum/maximum) at the ingestion boundary."],"tags":["protocol","json","serialization","out-of-range"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}