{"record":{"id":"d30e4b476fe5130c","repo":"elastic/elasticsearch","slug":"failed-to-build-name-after-last-required-field","errorCode":null,"errorMessage":"failed to build [{name}] after last required field arrived","messagePattern":"failed to build \\[(.+?)\\] after last required field arrived","errorType":"exception","errorClass":"XContentParseException","httpStatus":null,"severity":"error","filePath":"libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java","lineNumber":585,"sourceCode":"                    + \"argument. If it doesn't have any it should use ObjectParser instead of ConstructingObjectParser. This is a bug \"\n                    + \"in the parser declaration.\";\n            // All missing constructor arguments were optional. Just build the target and return it.\n            buildTarget();\n            return targetObject;\n        }\n\n        private void buildTarget() {\n            try {\n                targetObject = builder.apply(constructorArgs, context);\n                if (queuedOrderedModeCallback != null) {\n                    queuedOrderedModeCallback.accept(targetObject);\n                }\n                while (queuedFieldsCount > 0) {\n                    queuedFieldsCount -= 1;\n                    queuedFields[queuedFieldsCount].accept(targetObject);\n                }\n            } catch (XContentParseException e) {\n                throw new XContentParseException(\n                    e.getLocation(),\n                    \"failed to build [\" + objectParser.getName() + \"] after last required field arrived\",\n                    e\n                );\n            } catch (Exception e) {\n                throw new XContentParseException(\n                    null,\n                    \"Failed to build [\" + objectParser.getName() + \"] after last required field arrived\",\n                    e\n                );\n            }\n        }\n    }\n\n    private static class ConstructorArgInfo {\n        final ParseField field;\n        final boolean required;\n","sourceCodeStart":567,"sourceCodeEnd":603,"githubUrl":"https://github.com/elastic/elasticsearch/blob/db6a809a667c081ca1dc7500389d26975573215f/libs/x-content/src/main/java/org/elasticsearch/xcontent/ConstructingObjectParser.java#L567-L603","documentation":"buildTarget invokes the builder BiFunction once all required constructor args arrive. If the builder (or any queued field consumer flushed immediately after) throws an XContentParseException, it is re-wrapped with the original location preserved and a message naming the parser. This is the 'XContentParseException-typed cause' branch; the original parse location is retained.","triggerScenarios":"The builder lambda internally re-parses or constructs a nested object that fails with XContentParseException (e.g. building a value object whose own construction parses further content). A queued ordered-mode callback throws an XContentParseException.","commonSituations":"ConstructingObjectParser whose builder delegates to another parser/registry call that fails. Builders that validate parsed sub-structures and reject them via XContentParseException.","solutions":["Examine the cause's getLocation() and message to find the nested parse failure, then fix the offending sub-content.","Ensure the builder lambda does not assume invariants the parser cannot guarantee; validate and fail with a clearer message before reaching buildTarget."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n    Value v = cop.apply(parser, ctx);\n} catch (XContentParseException e) {\n    if (e.getMessage().contains(\"failed to build [\") && e.getCause() instanceof XContentParseException nested) {\n        XContentLocation loc = nested.getLocation(); // precise nested failure point\n    }\n}","preventionTips":["Keep builder lambdas free of redundant re-parsing; do structural validation in field consumers that carry location.","Unit-test builders with boundary inputs (empty, max, malformed nested objects).","Surface nested cause messages to operators rather than only the outer wrapper."],"tags":["xcontent","constructing-object-parser","builder","deserialization"],"backgroundTag":null,"analyzedSha":"db6a809a667c081ca1dc7500389d26975573215f","analyzedAt":"2026-08-12T01:39:14.192Z","schemaVersion":2},"datasetVersion":"2026-08-12T13:17:24.610Z"}