{"record":{"id":"637aa8f79e06c481","repo":"elastic/elasticsearch","slug":"pathprefix-must-not-be-empty","errorCode":null,"errorMessage":"pathPrefix must not be empty","messagePattern":"pathPrefix must not be empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"client/rest/src/main/java/org/elasticsearch/client/RestClientBuilder.java","lineNumber":216,"sourceCode":"     * For example, if this is set to \"/my/path\", then any client request will become <code>\"/my/path/\" + endpoint</code>.\n     * <p>\n     * In essence, every request's {@code endpoint} is prefixed by this {@code pathPrefix}. The path prefix is useful for when\n     * Elasticsearch is behind a proxy that provides a base path or a proxy that requires all paths to start with '/';\n     * it is not intended for other purposes and it should not be supplied in other scenarios.\n     *\n     * @throws NullPointerException if {@code pathPrefix} is {@code null}.\n     * @throws IllegalArgumentException if {@code pathPrefix} is empty or contains consecutive slashes.\n     */\n    public RestClientBuilder setPathPrefix(String pathPrefix) {\n        this.pathPrefix = cleanPathPrefix(pathPrefix);\n        return this;\n    }\n\n    public static String cleanPathPrefix(String pathPrefix) {\n        Objects.requireNonNull(pathPrefix, \"pathPrefix must not be null\");\n\n        if (pathPrefix.isEmpty()) {\n            throw new IllegalArgumentException(\"pathPrefix must not be empty\");\n        }\n\n        if (pathPrefix.contains(\"//\")) {\n            throw new IllegalArgumentException(\"pathPrefix is malformed. consecutive slashes are not allowed: [\" + pathPrefix + \"]\");\n        }\n\n        String cleanPathPrefix = pathPrefix;\n        if (cleanPathPrefix.startsWith(\"/\") == false) {\n            cleanPathPrefix = \"/\" + cleanPathPrefix;\n        }\n\n        // best effort to ensure that it looks like \"/base/path\" rather than \"/base/path/\"\n        if (cleanPathPrefix.endsWith(\"/\") && cleanPathPrefix.length() > 1) {\n            cleanPathPrefix = cleanPathPrefix.substring(0, cleanPathPrefix.length() - 1);\n        }\n        return cleanPathPrefix;\n    }\n","sourceCodeStart":198,"sourceCodeEnd":234,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/client/rest/src/main/java/org/elasticsearch/client/RestClientBuilder.java#L198-L234","documentation":"Validation in cleanPathPrefix: a non-null pathPrefix is required and must be non-empty. An empty string is rejected (separately from the null check) because it would silently produce ambiguous URIs.","triggerScenarios":"Calling RestClient.builder(...).setPathPrefix(\"\") after trimming user input that became empty.","commonSituations":"Config supplied a whitespace-only prefix that was trimmed to empty; conditional code calling setPathPrefix(prefix) without checking it was set; default-empty field concatenated into the builder.","solutions":["Only call setPathPrefix when the value is non-empty; skip the call entirely to keep the default (no prefix).","Trim and validate the prefix before passing: if (prefix != null && !prefix.isBlank()) builder.setPathPrefix(prefix.trim()).","Treat empty prefix as 'no prefix', not as an error."],"exampleFix":"// before\nbuilder.setPathPrefix(config.get(\"pathPrefix\", \"\")); // empty -> throws\n// after\nString prefix = config.get(\"pathPrefix\", \"\");\nif (prefix != null && !prefix.isBlank()) builder.setPathPrefix(prefix.trim());","handlingStrategy":"validation","validationCode":"String p = prefix == null ? null : prefix.trim();\nif (p != null && !p.isEmpty()) builder.setPathPrefix(p);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Treat empty/null prefix as 'no prefix' and skip the setter.","Trim at the boundary so whitespace-only never reaches the builder."],"tags":["rest-client","builder","path-prefix","validation"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T06:17:24.410Z"}