{"record":{"id":"01aac221b0bc85f2","repo":"elastic/elasticsearch","slug":"filters-cannot-be-null-or-empty","errorCode":null,"errorMessage":"filters cannot be null or empty","messagePattern":"filters cannot be null or empty","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/filtering/FilterPathBasedFilter.java","lineNumber":52,"sourceCode":"     * Marker value that should be used to indicate that none of the\n     * property names/values matches one of the filter paths.\n     */\n    private static final TokenFilter NO_MATCHING = new TokenFilter() {\n        @Override\n        public String toString() {\n            return \"NO_MATCHING\";\n        }\n    };\n\n    private final FilterPath[] filters;\n\n    private final boolean inclusive;\n\n    private final boolean matchFieldNamesWithDots;\n\n    public FilterPathBasedFilter(FilterPath[] filters, boolean inclusive, boolean matchFieldNamesWithDots) {\n        if (filters == null || filters.length == 0) {\n            throw new IllegalArgumentException(\"filters cannot be null or empty\");\n        }\n        this.inclusive = inclusive;\n        this.filters = filters;\n        this.matchFieldNamesWithDots = matchFieldNamesWithDots;\n    }\n\n    public FilterPathBasedFilter(Set<String> filters, boolean inclusive) {\n        this(FilterPath.compile(filters), inclusive, false);\n    }\n\n    /**\n     * Evaluates if a property name matches one of the given filter paths.\n     */\n    private TokenFilter evaluate(String name, FilterPath[] filterPaths) {\n        if (filterPaths != null) {\n            List<FilterPath> nextFilters = new ArrayList<>();\n            for (FilterPath filter : filterPaths) {\n                boolean matches = filter.matches(name, nextFilters, matchFieldNamesWithDots);","sourceCodeStart":34,"sourceCodeEnd":70,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/impl/src/main/java/org/elasticsearch/xcontent/provider/filtering/FilterPathBasedFilter.java#L34-L70","documentation":"FilterPathBasedFilter requires at least one FilterPath. The constructor checks filters == null || filters.length == 0 and throws IllegalArgumentException. An empty filter set is meaningless for include/exclude filtering, so it is rejected up front rather than behaving as a pass-through.","triggerScenarios":"Constructing FilterPathBasedFilter with an empty array or null filter set, or via the Set-based constructor with an empty Set (FilterPath.compile would be called on an empty set).","commonSituations":"Building a source_filter / filter_path request from a user query that resolved to no fields; programmatic removal of all filter entries leaving an empty collection; defaulting to empty when a config value is absent instead of falling back to a meaningful default.","solutions":["Guard the caller: only build the filter when the set is non-empty","Pass null/no filter rather than an empty array if your API distinguishes them","Provide a meaningful default filter when the user supplied none"],"exampleFix":"// before\nnew FilterPathBasedFilter(new FilterPath[0], true, false); // throws\n// after\nFilterPath[] paths = FilterPath.compile(includes);\nif (paths == null || paths.length == 0) {\n    // skip filtering entirely\n} else {\n    new FilterPathBasedFilter(paths, true, false);\n}","handlingStrategy":"validation","validationCode":"if (filters == null || filters.length == 0) {\n    // no filtering - return identity filter or skip\n    return;\n}\nnew FilterPathBasedFilter(filters, inclusive, matchDots);","typeGuard":"static boolean hasFilters(FilterPath[] f) {\n    return f != null && f.length > 0;\n}","tryCatchPattern":"try { new FilterPathBasedFilter(paths, true, false); }\ncatch (IllegalArgumentException e) { /* skip filtering */ }","preventionTips":["Distinguish 'no filter' from 'empty filter' in your API","Short-circuit when the user supplied no fields","Default to null rather than an empty collection"],"tags":["x-content","filtering","input-validation"],"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T08:17:17.861Z"}