{"record":{"id":"2051fb9bf4546936","repo":"apache/druid","slug":"key-s-is-required-in-map-s","errorCode":null,"errorMessage":"Key[%s] is required in map[%s]","messagePattern":"Key\\[(.+?)\\] is required in map\\[(.+?)\\]","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"processing/src/main/java/org/apache/druid/java/util/common/MapUtils.java","lineNumber":39,"sourceCode":"\nimport java.util.Map;\n\n/**\n */\npublic class MapUtils\n{\n  public static String getString(Map<String, Object> in, String key)\n  {\n    return getString(in, key, null);\n  }\n\n  public static String getString(Map<String, Object> in, String key, String defaultValue)\n  {\n    Object retVal = in.get(key);\n\n    if (retVal == null) {\n      if (defaultValue == null) {\n        throw new IAE(\"Key[%s] is required in map[%s]\", key, in);\n      }\n\n      return defaultValue;\n    }\n\n    return retVal.toString();\n  }\n\n  public static int getInt(Map<String, Object> in, String key, Integer defaultValue)\n  {\n    Object retVal = in.get(key);\n\n    if (retVal == null) {\n      if (defaultValue == null) {\n        throw new IAE(\"Key[%s] is required in map[%s]\", key, in);\n      }\n\n      return defaultValue;","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/apache/druid/blob/9b90983fd291f26935af934383ce360473179e4d/processing/src/main/java/org/apache/druid/java/util/common/MapUtils.java#L21-L57","documentation":"MapUtils.getString(map, key, defaultValue) throws this IAE when the key is absent (or mapped to null) AND the supplied defaultValue is null — i.e. the caller requested a required key by passing no default. The message shows the missing key and the whole map to aid diagnosis. If a non-null default is given, it is returned instead of throwing.","triggerScenarios":"Calling MapUtils.getString(specMap, \"dataSource\") where 'dataSource' is missing and the third argument is null, or explicitly MapUtils.getString(map, key, null); also nested lookups where an outer key exists but the inner map lacks the requested key.","commonSituations":"Ingestion spec or task config maps missing mandatory fields (dataSource, topic, intervals); JSON payloads from clients omitting required properties; schema drift after a Druid version adds a required key.","solutions":["Add the missing key to the map/config so it satisfies the contract (e.g. set 'dataSource' in the spec)","Pass a non-null default if the field is actually optional: MapUtils.getString(map, key, \"default\")","Validate required keys up front (Preconditions/checkArgument or a schema validator) to fail with a better message","Log/print the map contents shown in the error to spot typos in key names (case sensitivity matters)"],"exampleFix":"// before\nString ds = MapUtils.getString(config, \"dataSource\"); // IAE if absent\n// after\nString ds = MapUtils.getString(config, \"dataSource\", \"default-datasource\");","handlingStrategy":"validation","validationCode":"if (!config.containsKey(\"dataSource\")) {\n  throw new IllegalArgumentException(\"'dataSource' is required in config\");\n}","typeGuard":"static String requireString(Map<String, Object> map, String key) {\n  Object v = map.get(key);\n  if (v == null) throw new IllegalArgumentException(\"missing key: \" + key);\n  return v.toString();\n}","tryCatchPattern":"try {\n  dataSource = MapUtils.getString(config, \"dataSource\");\n} catch (IllegalArgumentException e) {\n  throw new SpecValidationException(\"Missing required 'dataSource' field in ingestion spec\", e);\n}","preventionTips":["Decide per key whether it is required (null default) or optional (explicit default)","Validate config maps against a schema at load time","Watch key spelling and case — the error prints the whole map for comparison","For optional fields always pass a non-null default explicitly"],"tags":["map","required-key","configuration"],"backgroundTag":"missing-required-argument","analyzedSha":"9b90983fd291f26935af934383ce360473179e4d","analyzedAt":"2026-09-07T13:32:30.957Z","contentChangedAt":"2026-09-07T13:32:30.957Z","schemaVersion":2},"datasetVersion":"2026-09-17T15:17:12.973Z"}