{"id":"df78cc0980d4452a","repo":"apache/kafka","slug":"string-must-be-non-empty","errorCode":null,"errorMessage":"String must be non-empty","messagePattern":"String must be non-empty","errorType":"validation","errorClass":"ConfigException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java","lineNumber":1231,"sourceCode":"            if (validators == null) return \"\";\n            StringBuilder desc = new StringBuilder();\n            for (Validator v: validators) {\n                if (desc.length() > 0) {\n                    desc.append(',').append(' ');\n                }\n                desc.append(v);\n            }\n            return desc.toString();\n        }\n    }\n\n    public static class NonEmptyString implements Validator {\n\n        @Override\n        public void ensureValid(String name, Object o) {\n            String s = (String) o;\n            if (s != null && s.isEmpty()) {\n                throw new ConfigException(name, o, \"String must be non-empty\");\n            }\n        }\n\n        @Override\n        public String toString() {\n            return \"non-empty string\";\n        }\n    }\n\n    public static class NonEmptyStringWithoutControlChars implements Validator {\n\n        public static NonEmptyStringWithoutControlChars nonEmptyStringWithoutControlChars() {\n            return new NonEmptyStringWithoutControlChars();\n        }\n\n        @Override\n        public void ensureValid(String name, Object value) {\n            String s = (String) value;","sourceCodeStart":1213,"sourceCodeEnd":1249,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/common/config/ConfigDef.java#L1213-L1249","documentation":"Thrown by NonEmptyString.ensureValid when the value is non-null but isEmpty(). Notably this validator allows null (it only checks `s != null && s.isEmpty()`), so it is used for configs that are optional but, when present, must be non-empty. Distinct from NonEmptyStringWithoutControlChars in that it does not check for ISO control characters.","triggerScenarios":"A ConfigKey validated with NonEmptyString (registered via .validator(new NonEmptyString())) is supplied the empty string \"\" in the properties Map. Triggered during ConfigDef.parse() when ensureValid runs.","commonSituations":"Optional string configs (e.g. client.id, sasl/mechanism strings in some modules, or component-specific identifiers) where the user wrote \"key=\" with nothing after the equals sign in a properties file, or env-var substitution produced an empty string.","solutions":["Either remove the key entirely (null is accepted by this validator) or set it to a non-empty value.","Fix properties-file lines that end with '=' and no value; comment them out (#) if unused.","Guard env-var substitution to skip the key when the variable is unset rather than emitting an empty string."],"exampleFix":"// before\nprops.put(\"client.id\", \"\");\n\n// after\n// option A: omit the key\n// option B: provide a real value\nprops.put(\"client.id\", \"order-service-producer\");","handlingStrategy":"validation","validationCode":"// NonEmptyString allows null (used when default is absent) but rejects empty strings:\nfor (Map.Entry<String, ?> e : props.entrySet()) {\n    Object v = e.getValue();\n    if (v instanceof String && ((String) v).isEmpty()) {\n        throw new IllegalArgumentException(e.getKey() + \" must be non-empty\");\n    }\n}","typeGuard":"// Represent as Optional<String> in your own config model; convert to Properties only when present:\nOptional<String> clientId = Optional.ofNullable(load(\"client.id\"));\nclientId.ifPresent(v -> props.put(\"client.id\", v));","tryCatchPattern":"try {\n    def.parse(props);\n} catch (ConfigException ce) {\n    if (ce.getMessage().equals(\"String must be non-empty\")) {\n        props.remove(ce.getName()); // treat empty as 'unset' if appropriate\n    } else throw ce;\n}","preventionTips":["Treat an empty string as 'unset' when building Properties — either omit the key or supply a real default.","Trim then check emptiness for any string loaded from a properties file.","Add a config linter that flags keys whose value is the literal empty string."],"tags":["config","kafka-client","validation","validator","empty-string"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}