{"record":{"id":"72cdc0664b06798d","repo":"grpc/grpc-java","slug":"value-s-for-key-s-is-not-an-integer","errorCode":null,"errorMessage":"value '%s' for key '%s' is not an integer","messagePattern":"value '(.+?)' for key '(.+?)' is not an integer","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"core/src/main/java/io/grpc/internal/JsonUtil.java","lineNumber":147,"sourceCode":"  public static Integer getNumberAsInteger(Map<String, ?> obj, String key) {\n    assert key != null;\n    if (!obj.containsKey(key)) {\n      return null;\n    }\n    Object value = obj.get(key);\n    if (value instanceof Double) {\n      Double d = (Double) value;\n      int i = d.intValue();\n      if (i != d) {\n        throw new ClassCastException(\"Number expected to be integer: \" + d);\n      }\n      return i;\n    }\n    if (value instanceof String) {\n      try {\n        return Integer.parseInt((String) value);\n      } catch (NumberFormatException e) {\n        throw new IllegalArgumentException(\n            String.format(\"value '%s' for key '%s' is not an integer\", value, key));\n      }\n    }\n    throw new IllegalArgumentException(\n        String.format(\"value '%s' for key '%s' is not an integer\", value, key));\n  }\n\n  /**\n   * Gets a number from an object for the given key, casted to an long.  If the key is not\n   * present, this returns null.  If the value does not represent a long integer, throws an\n   * exception.\n   */\n  public static Long getNumberAsLong(Map<String, ?> obj, String key) {\n    assert key != null;\n    if (!obj.containsKey(key)) {\n      return null;\n    }\n    Object value = obj.get(key);","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/grpc/grpc-java/blob/64daddc1f3d1975670f769f3e97bde8b2ba32d25/core/src/main/java/io/grpc/internal/JsonUtil.java#L129-L165","documentation":"getNumberAsInteger also accepts string-encoded integers. If the value is a String that Integer.parseInt cannot parse, it rethrows as IllegalArgumentException with the key and offending value. This keeps config parsing strict rather than letting a malformed string become a default or NumberFormatException.","triggerScenarios":"JsonUtil.getNumberAsInteger(map, key) where the value is a String like \"abc\", \"1.5\", \"12 3\", or \"\" (empty), or a numeric string with signs/whitespace that parseInt rejects.","commonSituations":"Service config JSON where numbers were quoted (\"retryAttempts\": \"three\" or \"5.0\"); values interpolated from env vars with stray whitespace or units like \"100ms\"; YAML/JSON mixups leaving values as strings.","solutions":["Fix the config value: use a valid 32-bit integer, either unquoted or as a clean numeric string (\"3\", not \"3.0\" or \"three\").","Trim/normalize the value in your config pipeline before it reaches gRPC parsing.","Validate config JSON with your own schema before passing it to gRPC so the failure surfaces with your own message."],"exampleFix":"// before\n{\"maxInboundMessageSize\": \"4 MB\"}\n// after\n{\"maxInboundMessageSize\": 4194304}","handlingStrategy":"validation","validationCode":"Object v = config.get(\"retryAttempts\");\nif (v instanceof String) {\n  try { Integer.parseInt((String) v); }\n  catch (NumberFormatException e) { throw new IllegalArgumentException(\"retryAttempts is not an integer string\"); }\n}","typeGuard":"boolean isIntString(Object v) {\n  if (!(v instanceof String)) return false;\n  try { Integer.parseInt((String) v); return true; } catch (NumberFormatException e) { return false; }\n}","tryCatchPattern":"try {\n  int n = JsonUtil.getNumberAsInteger(config, \"retryAttempts\");\n} catch (IllegalArgumentException e) {\n  log.error(\"Invalid integer value in config: \" + e.getMessage());\n}","preventionTips":["Avoid quoting numeric config values in JSON.","Trim values derived from env vars before injecting them into config.","Reject strings with units (\"100ms\") at config-load time."],"tags":["grpc","json","config","format"],"backgroundTag":"invalid-argument-format","analyzedSha":"64daddc1f3d1975670f769f3e97bde8b2ba32d25","analyzedAt":"2026-09-08T06:14:57.704Z","contentChangedAt":"2026-09-08T06:14:57.704Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}