{"record":{"id":"b411cd5a963a5fb0","repo":"elastic/elasticsearch","slug":"value-text-is-out-of-range-for-a-short","errorCode":null,"errorMessage":"Value [${text}] is out of range for a short","messagePattern":"Value \\[(.+?)\\] is out of range for a short","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java","lineNumber":130,"sourceCode":"    }\n\n    protected abstract boolean doBooleanValue() throws IOException;\n\n    @Override\n    public short shortValue() throws IOException {\n        return shortValue(DEFAULT_NUMBER_COERCE_POLICY);\n    }\n\n    /**\n     * Parses a {@code short} from its decimal string representation, using the same semantics as the\n     * document-indexing path. The value is first parsed as a {@code double} and then narrowed; values\n     * outside [{@value Short#MIN_VALUE}, {@value Short#MAX_VALUE}] throw\n     * {@link IllegalArgumentException}.\n     */\n    public static short parseShort(String text) {\n        double doubleValue = Double.parseDouble(text);\n        if (doubleValue < Short.MIN_VALUE || doubleValue > Short.MAX_VALUE) {\n            throw new IllegalArgumentException(\"Value [\" + text + \"] is out of range for a short\");\n        }\n        return (short) doubleValue;\n    }\n\n    @Override\n    public short shortValue(boolean coerce) throws IOException {\n        Token token = currentToken();\n        if (token == Token.VALUE_STRING) {\n            checkCoerceString(coerce, Short.class);\n            XContentString numericText = optimizedText();\n            checkNumericStringLength(numericText.stringLength());\n            return parseShort(numericText.string());\n        }\n        short result = doShortValue();\n        ensureNumberConversion(coerce, result, Short.class);\n        return result;\n    }\n","sourceCodeStart":112,"sourceCodeEnd":148,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/src/main/java/org/elasticsearch/xcontent/support/AbstractXContentParser.java#L112-L148","documentation":"Thrown by AbstractXContentParser.parseShort() when the decimal string representation, parsed as a double, falls outside the short range [-32768, 32767]. The method parses the string as a double first (to handle scientific notation and decimals uniformly) and then range-checks against Short.MIN_VALUE and Short.MAX_VALUE before narrowing.","triggerScenarios":"Calling shortValue() on a parser whose current value is a numeric string or number exceeding 32767 or below -32768. For example, parsing a field value of \"99999\" or \"-40000\" as a short.","commonSituations":"Field mapped as short receiving values outside the valid range. Port numbers, small counters, or legacy fields that used short but now receive larger values. Client not validating range before sending.","solutions":["Ensure the value is within [-32768, 32767].","Change the field mapping from short to integer or long if values can exceed the short range.","Validate the value range client-side before submitting.","Round or clamp the value if it should always fit in a short."],"exampleFix":"// before — value exceeds short range\n{ \"port\": 99999 }\n\n// after — value within short range\n{ \"port\": 8080 }","handlingStrategy":"validation","validationCode":"// Before sending, verify the value fits in short range\npublic static void validateShortRange(Object value, String fieldName) {\n    if (value instanceof Number n) {\n        short s = n.shortValue();\n        if (s < Short.MIN_VALUE || s > Short.MAX_VALUE || n.doubleValue() < Short.MIN_VALUE || n.doubleValue() > Short.MAX_VALUE) {\n            throw new IllegalArgumentException(fieldName + \" value \" + n + \" is out of range for short [-32768, 32767]\");\n        }\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    short value = parser.shortValue();\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"out of range for a short\")) {\n        throw new BadRequestException(e.getMessage());\n    }\n    throw e;\n}","preventionTips":["Validate numeric ranges client-side against the field's target type before sending.","Use integer or long field types instead of short if values may exceed 32767.","Document the short range constraint in API specs for fields mapped as short."],"tags":["parsing","xcontent","numeric","overflow","short","range"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}