apache/kafka · error · RuntimeException

${about}: value ${value} does not fit in a 16-bit signed int

Error message

${about}: value ${value} does not fit in a 16-bit signed integer.

What it means

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.

Source

Thrown at clients/src/main/java/org/apache/kafka/common/protocol/MessageUtil.java:89

                // It's more traditional to refer to bytes as unsigned,
                // so we support that here.
                value -= 128;
            } else {
                throw new RuntimeException(about + ": value " + value +
                    " does not fit in an 8-bit signed integer.");
            }
        }
        if (value < Byte.MIN_VALUE) {
            throw new RuntimeException(about + ": value " + value +
                " does not fit in an 8-bit signed integer.");
        }
        return (byte) value;
    }

    public static short jsonNodeToShort(JsonNode node, String about) {
        int value = jsonNodeToInt(node, about);
        if ((value < Short.MIN_VALUE) || (value > Short.MAX_VALUE)) {
            throw new RuntimeException(about + ": value " + value +
                " does not fit in a 16-bit signed integer.");
        }
        return (short) value;
    }

    public static int jsonNodeToUnsignedShort(JsonNode node, String about) {
        int value = jsonNodeToInt(node, about);
        if (value < 0 || value > UNSIGNED_SHORT_MAX) {
            throw new RuntimeException(about + ": value " + value +
                " does not fit in a 16-bit unsigned integer.");
        }
        return value;
    }

    public static long jsonNodeToUnsignedInt(JsonNode node, String about) {
        long value = jsonNodeToLong(node, about);
        if (value < 0 || value > UNSIGNED_INT_MAX) {
            throw new RuntimeException(about + ": value " + value +

View on GitHub (pinned to c31c9215e1)

Solutions

  1. Find the field named in 'about' and bring its value within [-32768, 32767].
  2. 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).
  3. Validate the JSON against the schema before sending.

Example fix

// before
{ "port": 70000 }

// after
{ "port": 9092 }
Defensive patterns

Strategy: validation

Validate before calling

// MessageUtil.jsonNodeToShort requires [Short.MIN_VALUE, Short.MAX_VALUE] = [-32768, 32767].
int value = node.asInt();
if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {
    throw new IllegalArgumentException(about + ": value " + value + " outside short range [-32768,32767]");
}
short s = MessageUtil.jsonNodeToShort(node, about);

Type guard

static Optional<Short> asShort(com.fasterxml.jackson.databind.JsonNode n) {
    if (n == null || !n.canConvertToInt()) return Optional.empty();
    int v = n.asInt();
    return (v >= Short.MIN_VALUE && v <= Short.MAX_VALUE) ? Optional.of((short) v) : Optional.empty();
}

Try / catch

try {
    short s = MessageUtil.jsonNodeToShort(node, about);
} catch (RuntimeException e) {
    log.error("{}: short field out of range", about, e);
    rejectMessage(about, e);
}

Prevention

When it happens

Trigger: 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.

Common situations: 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'.

Related errors


AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03). Data as JSON: /data/errors/405b07fc81975f28.json. Report an issue: GitHub.