{"record":{"id":"0a8de17397622538","repo":"apple/pkl","slug":"cannot-convert-pkl-base-int-s-to-java-lang-shor","errorCode":null,"errorMessage":"Cannot convert pkl.base#Int `%s` to java.lang.Short because it is outside range `%s..%s`","messagePattern":"Cannot convert pkl\\.base#Int `(.+?)` to java\\.lang\\.Short because it is outside range `(.+?)\\.\\.(.+?)`","errorType":"exception","errorClass":"ConversionException","httpStatus":null,"severity":"error","filePath":"pkl-config-java/src/main/java/org/pkl/config/java/mapper/Conversions.java","lineNumber":63,"sourceCode":"              throw new ConversionException(\n                  String.format(\n                      \"Cannot convert pkl.base#Int `%s` to java.lang.Byte because it is outside range `%s..%s`\",\n                      value, Byte.MIN_VALUE, Byte.MAX_VALUE));\n            }\n            return value.byteValue();\n          });\n\n  /**\n   * Conversion from {@code pkl.base#Int} to {@link Short}. Throws {@link ConversionException} if\n   * the value is too large.\n   */\n  public static final Conversion<Long, Short> pIntToShort =\n      Conversion.of(\n          PClassInfo.Int,\n          short.class,\n          (value, mapper) -> {\n            if (value < Short.MIN_VALUE || value > Short.MAX_VALUE) {\n              throw new ConversionException(\n                  String.format(\n                      \"Cannot convert pkl.base#Int `%s` to java.lang.Short because it is outside range `%s..%s`\",\n                      value, Short.MIN_VALUE, Short.MAX_VALUE));\n            }\n            return value.shortValue();\n          });\n\n  /**\n   * Conversion from {@code pkl.base#Int} to {@link Integer}. Throws {@link ConversionException} if\n   * the value is too large.\n   */\n  public static final Conversion<Long, Integer> pIntToInteger =\n      Conversion.of(\n          PClassInfo.Int,\n          int.class,\n          (value, mapper) -> {\n            if (value < Integer.MIN_VALUE || value > Integer.MAX_VALUE) {\n              throw new ConversionException(","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/apple/pkl/blob/f3efcbfc9b60d30053b0536d664948d7aa1b8673/pkl-config-java/src/main/java/org/pkl/config/java/mapper/Conversions.java#L45-L81","documentation":"Conversions.pIntToShort narrows a 64-bit Pkl Int to a Java short and throws ConversionException when the value is outside Short.MIN_VALUE..Short.MAX_VALUE (-32768..32767). This guards against silent truncation when mapping Pkl config integers onto Java short fields.","triggerScenarios":"Mapping a Pkl Int property to a Java short/Short field where the value exceeds the 16-bit range, e.g. via JavaMapper or generated config classes during decode.","commonSituations":"Large counters, ports above 32767, timestamps, or IDs stored in .pkl where Java code declares short; Pkl schema loosened to plain Int while Java mapping still uses Short.","solutions":["Correct the value in the .pkl file so it fits in -32768..32767.","Widen the Java field/mapping to int or long and update the conversion to pIntToInteger/pIntToLong.","Add a Pkl-level type constraint (IntMatching(-32768..32767)) so invalid data is rejected at evaluation time.","Pre-validate the value in code before mapping."],"exampleFix":"// before\nshort timeout = mapper.getShort(\"timeoutMs\"); // throws for 60000\n// after\nint timeout = mapper.getInt(\"timeoutMs\"); // or long","handlingStrategy":"validation","validationCode":"// validate before mapping to short\nstatic short checkedToShort(long value) {\n  if (value < Short.MIN_VALUE || value > Short.MAX_VALUE)\n    throw new IllegalArgumentException(\"value \" + value + \" outside short range\");\n  return (short) value;\n}","typeGuard":"static boolean fitsInShort(long v) { return v >= -32768 && v <= 32767; }","tryCatchPattern":"try {\n  Short s = mapper.map(value, Short.class);\n} catch (ConversionException e) {\n  log.error(\"Pkl Int out of short range: {}\", e.getMessage());\n  throw new ConfigValidationException(\"expected short-sized value, got: \" + value, e);\n}","preventionTips":["Constrain the Pkl property with IntMatching(-32768..32767)","Prefer int/long Java fields; reserve short for genuinely 16-bit data","Watch values like ports (>32767) and durations that commonly overflow shorts","Add round-trip tests with boundary values"],"tags":["conversion","range","short","pkl","mapping"],"backgroundTag":"value-out-of-range","analyzedSha":"f3efcbfc9b60d30053b0536d664948d7aa1b8673","analyzedAt":"2026-09-08T13:10:45.570Z","contentChangedAt":"2026-09-08T13:10:45.570Z","schemaVersion":2},"datasetVersion":"2026-09-14T16:17:12.679Z"}