{"record":{"id":"e6632446415d21c3","repo":"t8y2/dbx","slug":"mongodb-createuser-requires-a-non-empty-user-name","errorCode":null,"errorMessage":"MongoDB createUser requires a non-empty user name","messagePattern":"MongoDB createUser requires a non-empty user name","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java","lineNumber":1045,"sourceCode":"        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));\n        return Collections.singletonMap(\"affected_rows\", 1);\n    }\n\n    static Document buildCreateUserCommand(JsonObject params) {\n        Document user = requiredDocument(params, \"user_json\", \"User document\");\n        Object username = user.remove(\"user\");\n        if (!(username instanceof String) || ((String) username).isBlank()) {\n            throw new IllegalArgumentException(\"MongoDB createUser requires a non-empty user name\");\n        }\n        if (user.containsKey(\"createUser\") || user.containsKey(\"writeConcern\")) {\n            throw new IllegalArgumentException(\"MongoDB createUser user document contains reserved command fields\");\n        }\n\n        Document command = new Document(\"createUser\", username);\n        command.putAll(user);\n        Document writeConcern = documentOrNull(params, \"write_concern_json\");\n        if (writeConcern != null) {\n            command.put(\"writeConcern\", writeConcern);\n        }\n        return command;\n    }\n\n    private static Document requiredDocument(JsonObject params, String key, String label) {\n        Document document = documentOrNull(params, key);\n        if (document == null) {\n            throw new IllegalArgumentException(label + \" are required\");","sourceCodeStart":1027,"sourceCodeEnd":1063,"githubUrl":"https://github.com/t8y2/dbx/blob/c0390bff16418b651f4728520d99adf8ce48829a/agents/drivers/mongodb/src/main/java/com/dbx/agent/mongodb/MongoAgent.java#L1027-L1063","documentation":"buildCreateUserCommand constructs a createUser command from a user_json document. The \"user\" field inside that document is mandatory and must be a non-blank string, because it becomes the command's createUser value. If it's missing, not a string, or blank, the agent throws IllegalArgumentException before talking to the server.","triggerScenarios":"Calling createUser with user_json missing the \"user\" field, e.g. {\"pwd\": \"s3cret\", \"roles\": []}, or with {\"user\": \"\"} / a non-string user value.","commonSituations":"Templates omitting the username; form/config submissions with an empty username; forgetting that the agent requires the name inside user_json rather than as a top-level parameter.","solutions":["Add a non-empty \"user\" field to user_json, e.g. {\"user\": \"appUser\", \"pwd\": \"...\", \"roles\": [\"readWrite\"]}.","Ensure the value is a string, not a number or object.","Trim the username input and reject blank values before building the document."],"exampleFix":"// before\n{\"database\": \"admin\", \"user_json\": {\"pwd\": \"s3cret\", \"roles\": [\"readWrite\"]}}\n// after\n{\"database\": \"admin\", \"user_json\": {\"user\": \"appUser\", \"pwd\": \"s3cret\", \"roles\": [\"readWrite\"]}}","handlingStrategy":"validation","validationCode":"const u = params.user_json || {};\nif (typeof u.user !== \"string\" || u.user.trim() === \"\") {\n  throw new Error(\"user_json.user must be a non-empty string\");\n}","typeGuard":"function hasValidUsername(userJson) {\n  return userJson != null && typeof userJson.user === \"string\" && userJson.user.trim() !== \"\";\n}","tryCatchPattern":"try {\n  await agent.createUser({ database, user_json: doc });\n} catch (e) {\n  if (String(e.message).includes(\"requires a non-empty user name\")) {\n    console.error(\"Add a non-empty 'user' field to user_json\");\n  } else throw e;\n}","preventionTips":["Always include {\"user\": \"<name>\"} as the first field of user_json.","Trim and non-empty-check usernames collected from forms/env.","Remember the username lives inside user_json, not as a top-level parameter."],"tags":["mongodb","createuser","validation","missing-field"],"backgroundTag":"missing-required-parameter","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"}