elastic/elasticsearch · error · XContentParseException

named objects are not supported for this parser

Error message

named objects are not supported for this parser

What it means

NamedXContentRegistry.lookupParser throws when the registry is entirely empty (no categories registered for the active REST API version) and a named object is requested. This is a guard to give a better hint than a generic lookup miss: an empty registry (e.g. NamedXContentRegistry.EMPTY) can never resolve any named object.

Source

Thrown at libs/x-content/src/main/java/org/elasticsearch/xcontent/NamedXContentRegistry.java:171

     * Returns {@code true} if this registry is able to {@link #parseNamedObject parse} the referenced object, false otherwise.
     * Note: This method does not throw exceptions, even if the {@link RestApiVersion} or {@code categoryClass} are unknown.
     */
    public boolean hasParser(Class<?> categoryClass, String name, RestApiVersion apiVersion) {
        final Map<Class<?>, Map<String, Entry>> versionMap = registry.get(apiVersion);
        if (versionMap == null) {
            return false;
        }
        final Map<String, Entry> parsers = versionMap.get(categoryClass);
        return parsers != null && parsers.containsKey(name);
    }

    // scope for testing
    public <T> Entry lookupParser(Class<T> categoryClass, String name, XContentParser parser) {
        Map<String, Entry> parsers = registry.getOrDefault(parser.getRestApiVersion(), emptyMap()).get(categoryClass);
        if (parsers == null) {
            if (registry.isEmpty()) {
                // The "empty" registry will never work so we throw a better exception as a hint.
                throw new XContentParseException("named objects are not supported for this parser");
            }
            throw new XContentParseException("unknown named object category [" + categoryClass.getName() + "]");
        }
        Entry entry = parsers.get(name);
        if (entry == null) {
            throw new NamedObjectNotFoundException(parser.getTokenLocation(), "unknown field [" + name + "]", parsers.keySet());
        }
        if (false == entry.name.match(name, parser.getDeprecationHandler())) {
            /* Note that this shouldn't happen because we already looked up the entry using the names but we need to call `match` anyway
             * because it is responsible for logging deprecation warnings. */
            throw new XContentParseException(
                parser.getTokenLocation(),
                "unable to parse " + categoryClass.getSimpleName() + " with name [" + name + "]: parser didn't match"
            );
        }
        return entry;
    }

View on GitHub (pinned to db6a809a66)

Solutions

  1. Build the parser with a non-empty NamedXContentRegistry containing the required category/name entries (e.g. the cluster's getNamedXContentRegistry()).
  2. Ensure modules/plugins contributing named content are loaded so the registry is populated before parsing.
  3. In tests, construct a NamedXContentRegistry with the minimal List<Entry> needed rather than using EMPTY.

Example fix

// before
XContentParser p = XContentType.JSON.xContent().createParser(NamedXContentRegistry.EMPTY, in);

// after
NamedXContentRegistry reg = new NamedXContentRegistry(List.of(
    new NamedXContentRegistry.Entry(Aggregation.class, new ParseField("my_agg"), p -> ...)));
XContentParser p = XContentType.JSON.xContent().createParser(reg, in);
Defensive patterns

Strategy: validation

Validate before calling

// before parsing named content, ensure the registry is non-empty and has the category
if (registry.isEmpty()) {
    registry = buildRegistryWithKnownCategories(); // never pass EMPTY for named content
}

Try / catch

try {
    p.parse(parser, ctx);
} catch (XContentParseException e) {
    if (e.getMessage().equals("named objects are not supported for this parser")) {
        // rebuild parser with a populated NamedXContentRegistry
    }
}

Prevention

When it happens

Trigger: Calling parser.namedObject(...) or parsing content that references a named object while the parser was built with an empty NamedXContentRegistry. Common when constructing a parser for tests with the default/no-arg registry, or when a module that should register named content is not loaded.

Common situations: Test parsers using NamedXContentRegistry.EMPTY inadvertently. A plugin/module failing to register its NamedXContent entries. Misconfigured parser factory that drops the registry.

Related errors


AI-assisted analysis of elastic/elasticsearch@db6a809a66 (2026-08-12). Data as JSON: /api/errors/10268d5261eb47ff. Report an issue: GitHub.