diff --git a/server/src/main/java/org/elasticsearch/index/mapper/NestedObjectMapper.java b/server/src/main/java/org/elasticsearch/index/mapper/NestedObjectMapper.java index 4f1cf4689c2d7..4bc633296a832 100644 --- a/server/src/main/java/org/elasticsearch/index/mapper/NestedObjectMapper.java +++ b/server/src/main/java/org/elasticsearch/index/mapper/NestedObjectMapper.java @@ -450,23 +450,20 @@ public boolean hasValue() { @Override public void write(XContentBuilder b) throws IOException { assert (children != null && children.size() > 0); - switch (children.size()) { - case 1 -> { - b.startObject(simpleName()); - leafStoredFieldLoader.advanceTo(children.get(0)); - leafSourceLoader.write(leafStoredFieldLoader, children.get(0), b); + if (children.size() == 1) { + b.startObject(simpleName()); + leafStoredFieldLoader.advanceTo(children.get(0)); + leafSourceLoader.write(leafStoredFieldLoader, children.get(0), b); + b.endObject(); + } else { + b.startArray(simpleName()); + for (int childId : children) { + b.startObject(); + leafStoredFieldLoader.advanceTo(childId); + leafSourceLoader.write(leafStoredFieldLoader, childId, b); b.endObject(); } - default -> { - b.startArray(simpleName()); - for (int childId : children) { - b.startObject(); - leafStoredFieldLoader.advanceTo(childId); - leafSourceLoader.write(leafStoredFieldLoader, childId, b); - b.endObject(); - } - b.endArray(); - } + b.endArray(); } } diff --git a/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java b/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java index 9531181013d56..0486022620398 100644 --- a/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/index/mapper/MapperServiceTestCase.java @@ -802,6 +802,7 @@ private void roundTripSyntheticSource(DocumentMapper mapper, String syntheticSou try (Directory roundTripDirectory = newDirectory()) { RandomIndexWriter roundTripIw = indexWriterForSyntheticSource(roundTripDirectory); ParsedDocument doc = mapper.parse(new SourceToParse("1", new BytesArray(syntheticSource), XContentType.JSON)); + // Process root and nested documents in the same way as the normal indexing chain (assuming a single document) doc.updateSeqID(0, 0); doc.version().setLongValue(0); roundTripIw.addDocuments(doc.docs());