{"record":{"id":"04b2fd7accb58367","repo":"apache/pulsar","slug":"invalid-topic-name-topicname-topic-name-mus","errorCode":null,"errorMessage":"Invalid topic name: '${topicName}'. Topic name must not have leading or trailing whitespace.","messagePattern":"Invalid topic name: '(.+?)'\\. Topic name must not have leading or trailing whitespace\\.","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java","lineNumber":136,"sourceCode":"     * allowed.\n     *\n     * <p>This is only meant to be checked on topic creation paths, so topics which already have such a name\n     * remain listable, readable and deletable.\n     */\n    public static boolean isValidForCreation(TopicName topicName) {\n        String topic = topicName.toString();\n        return topic.equals(StringUtils.trim(topic));\n    }\n\n    /**\n     * Validates that a topic name can be used to create a new topic.\n     *\n     * @throws IllegalArgumentException if the topic name cannot be used to create a topic\n     * @see #isValidForCreation(TopicName)\n     */\n    public static void validateTopicNameForCreation(TopicName topicName) {\n        if (!isValidForCreation(topicName)) {\n            throw new IllegalArgumentException(\"Invalid topic name: '\" + topicName\n                    + \"'. Topic name must not have leading or trailing whitespace.\");\n        }\n    }\n\n    public static String getPartitionPattern(String topic) {\n        return \"^\" + Pattern.quote(get(topic).getPartitionedTopicName().toString()) + \"-partition-[0-9]+$\";\n    }\n\n    public static String getPattern(String topic) {\n        return \"^\" + Pattern.quote(get(topic).getPartitionedTopicName().toString()) + \"$\";\n    }\n\n    @SuppressFBWarnings(\"DCN_NULLPOINTER_EXCEPTION\")\n    private TopicName(String completeTopicName) {\n        try {\n            // The topic name can be in two different forms, one is fully qualified topic name,\n            // the other one is short topic name\n            int index = completeTopicName.indexOf(\"://\");","sourceCodeStart":118,"sourceCodeEnd":154,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/naming/TopicName.java#L118-L154","documentation":"TopicName.validateTopicNameForCreation(topicName) enforces that a topic name intended for creating a new topic has no leading or trailing whitespace, as delegated to TopicName.isValidForCreation. If the check fails it throws an IllegalArgumentException naming the offending topic name. This prevents creating topics whose names contain invisible padding that causes lookup/management mismatches.","triggerScenarios":"Calling validateTopicNameForCreation on a TopicName built from a string with leading/trailing spaces, e.g. ' persistent://public/default/my-topic ' or a non-persistent name with trailing whitespace from user input or a config file.","commonSituations":"Topic names copy-pasted from docs or terminals with trailing spaces; YAML/properties values with unquoted trailing whitespace; shell scripts interpolating variables that carry stray spaces; admin CLI arguments pasted with whitespace.","solutions":["Trim the topic-name string before constructing the TopicName: topicName.trim().","Fix the configuration/script source so the value is quoted or stripped of whitespace.","Call TopicName.isValidForCreation(topicName) yourself first and reject or sanitize bad input with a clear message.","If the whitespace is intentional in an existing topic's name, address the root data problem rather than bypassing the validation."],"exampleFix":"// before\nTopicName t = TopicName.get(\" persistent://public/default/my-topic \");\nTopicName.validateTopicNameForCreation(t);\n// after\nString raw = \" persistent://public/default/my-topic \".trim();\nTopicName t = TopicName.get(raw);\nTopicName.validateTopicNameForCreation(t);","handlingStrategy":"validation","validationCode":"String raw = input.trim();\nTopicName t = TopicName.get(raw);\nif (!TopicName.isValidForCreation(t)) {\n    throw new IllegalArgumentException(\"Topic name has surrounding whitespace: '\" + input + \"'\");\n}\nTopicName.validateTopicNameForCreation(t);","typeGuard":"static boolean isCleanForCreation(String topic) {\n    return topic != null && topic.trim().equals(topic) && !topic.isEmpty();\n}","tryCatchPattern":"try {\n    TopicName.validateTopicNameForCreation(topicName);\n} catch (IllegalArgumentException e) {\n    log.error(\"Refusing to create topic: {}\", e.getMessage());\n    throw e; // do not silently trim — surface the bad input\n}","preventionTips":["Trim all topic-name inputs at the boundary (CLI args, REST params, config parsing).","Quote YAML/properties values so trailing whitespace is not silently dropped or preserved inconsistently.","Run TopicName.isValidForCreation as a pre-flight check before any create-topic admin call."],"tags":["pulsar","topic","validation","whitespace","illegal-argument"],"backgroundTag":"invalid-topic-name","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}