{"record":{"id":"155d176828f4b5de","repo":"t8y2/dbx","slug":"index-option-name-must-be-a-non-empty-string","errorCode":null,"errorMessage":"Index option \"name\" must be a non-empty string","messagePattern":"Index option \"name\" must be a non-empty string","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":1019,"sourceCode":"        Document keys = requiredDocument(params, \"keys_json\", \"Index keys\");\n        if (keys.isEmpty()) {\n            throw new IllegalArgumentException(\"Index keys are required\");\n        }\n\n        Document index = new Document(\"key\", keys);\n        Document options = documentOrNull(params, \"options_json\");\n        if (options != null) {\n            if (options.containsKey(\"key\")) {\n                throw new IllegalArgumentException(\"Index options cannot contain \\\"key\\\"; specify index fields in keys JSON\");\n            }\n            index.putAll(options);\n        }\n        String name;\n        if (!index.containsKey(\"name\")) {\n            name = defaultIndexName(keys);\n            index.put(\"name\", name);\n        } else if (!(index.get(\"name\") instanceof String)) {\n            throw new IllegalArgumentException(\"Index option \\\"name\\\" must be a non-empty string\");\n        } else {\n            name = (String) index.get(\"name\");\n            if (name.isBlank()) {\n                throw new IllegalArgumentException(\"Index option \\\"name\\\" must be a non-empty string\");\n            }\n        }\n\n        c.getDatabase(database).runCommand(\n            new Document(\"createIndexes\", collection)\n                .append(\"indexes\", Collections.singletonList(index))\n        );\n        return Collections.singletonMap(\"name\", name);\n    }\n\n    private static Object createUser(JsonObject params) {\n        MongoClient client = requireClient();\n        String database = params.get(\"database\").getAsString();\n        client.getDatabase(database).runCommand(buildCreateUserCommand(params));","sourceCodeStart":1001,"sourceCodeEnd":1037,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L1001-L1037","documentation":"When the index spec's \"name\" option is present but is not a String (e.g. a number), createIndex throws IllegalArgumentException because MongoDB index names must be strings. The message says \"non-empty string\" though this specific throw only covers the wrong-type case; the blank check is handled separately.","triggerScenarios":"Calling createIndex with options_json like {\"name\": 42} or {\"name\": true}, or any non-string name value.","commonSituations":"Programmatic index builders that store the name as an integer; JSON schemas without type checks; templating errors substituting a numeric id as the index name.","solutions":["Change the \"name\" option to a string, e.g. {\"name\": \"userId_1\"}.","Omit the name entirely and let the agent derive the default name from the keys.","Add a type check on the name in whatever code builds the options document."],"exampleFix":"// before\n{\"keys_json\": {\"email\": 1}, \"options_json\": {\"name\": 42}}\n// after\n{\"keys_json\": {\"email\": 1}, \"options_json\": {\"name\": \"email_1\"}}","handlingStrategy":"validation","validationCode":"if (params.options_json && \"name\" in params.options_json && typeof params.options_json.name !== \"string\") {\n  params.options_json.name = String(params.options_json.name);\n}","typeGuard":"function hasStringIndexName(options) {\n  return options == null || !(\"name\" in options) || typeof options.name === \"string\";\n}","tryCatchPattern":"try {\n  await agent.createIndex({ database, collection, keys_json: keys, options_json: options });\n} catch (e) {\n  if (String(e.message).includes('\"name\" must be a non-empty string')) {\n    // coerce name to string or drop it and retry with the default name\n  } else throw e;\n}","preventionTips":["Coerce any computed index name with String(name) before passing.","Omit the name option unless a specific name is required.","Type-check option values in the index-builder helper."],"tags":["mongodb","index","validation","type-mismatch"],"backgroundTag":"invalid-parameter-type","analyzedSha":"c0390bff16418b651f4728520d99adf8ce48829a","analyzedAt":"2026-09-05T23:05:10.900Z","contentChangedAt":"2026-09-05T23:05:10.900Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}