From f1adbddae6e467b11da7c6ee18ffb5cabf3189b7 Mon Sep 17 00:00:00 2001 From: David Turner Date: Mon, 21 Nov 2022 15:44:01 +0000 Subject: [PATCH] Expose params to toXContentChunked as well as per-chunk Some responses change shape depending on the supplied `params`, and/or parse certain details out of `params`. By passing the `params` to `toXContentChunked` we can adjust the shape of the returned iterator and/or avoid duplicate parsing effort in ways that are not possible today where `params` is only made available to each leaf `ToXContent` object. --- .../discovery/StableMasterDisruptionIT.java | 2 +- .../snapshots/get/GetSnapshotsResponse.java | 2 +- .../snapshots/status/SnapshotStatus.java | 2 +- .../status/SnapshotsStatusResponse.java | 6 ++++-- .../mapping/get/GetMappingsResponse.java | 2 +- .../indices/recovery/RecoveryResponse.java | 2 +- .../segments/IndicesSegmentResponse.java | 2 +- .../settings/get/GetSettingsResponse.java | 2 +- .../fieldcaps/FieldCapabilitiesResponse.java | 2 +- .../nodes/BaseNodesXContentResponse.java | 2 +- .../common/xcontent/ChunkedToXContent.java | 4 ++-- .../org/elasticsearch/health/Diagnosis.java | 11 ++++++++--- .../elasticsearch/health/GetHealthAction.java | 4 ++-- .../health/HealthIndicatorResult.java | 9 +++++++-- .../rest/ChunkedRestResponseBody.java | 2 +- .../get/GetSnapshotsResponseTests.java | 3 ++- .../status/SnapshotsStatusResponseTests.java | 4 +++- .../mapping/get/GetMappingsResponseTests.java | 4 +++- .../recovery/RecoveryResponseTests.java | 3 ++- .../segments/IndicesSegmentResponseTests.java | 5 +++-- .../settings/get/GetSettingsResponseTests.java | 4 +++- .../MergedFieldCapabilitiesResponseTests.java | 6 ++++-- .../health/GetHealthResponseTests.java | 6 +++--- .../health/HealthIndicatorResultTests.java | 8 ++++---- .../rest/ChunkedRestResponseBodyTests.java | 18 ++++++++++-------- .../test/AbstractXContentTestCase.java | 2 +- 26 files changed, 71 insertions(+), 46 deletions(-) diff --git a/server/src/internalClusterTest/java/org/elasticsearch/discovery/StableMasterDisruptionIT.java b/server/src/internalClusterTest/java/org/elasticsearch/discovery/StableMasterDisruptionIT.java index cf8c44119b222..9508d550404bd 100644 --- a/server/src/internalClusterTest/java/org/elasticsearch/discovery/StableMasterDisruptionIT.java +++ b/server/src/internalClusterTest/java/org/elasticsearch/discovery/StableMasterDisruptionIT.java @@ -144,7 +144,7 @@ private void assertMasterStability(Client client, HealthStatus expectedStatus, M private String xContentToString(ChunkedToXContent xContent) throws IOException { XContentBuilder builder = JsonXContent.contentBuilder(); - xContent.toXContentChunked().forEachRemaining(xcontent -> { + xContent.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> { try { xcontent.toXContent(builder, ToXContent.EMPTY_PARAMS); } catch (IOException e) { diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java index 026435f3a9b25..f80ace56b62c0 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponse.java @@ -165,7 +165,7 @@ public void writeTo(StreamOutput out) throws IOException { } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params params) { return Iterators.concat(Iterators.single((b, p) -> { b.startObject(); b.startArray("snapshots"); diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java index d9b47f7ecc237..fa635094a1ba2 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotStatus.java @@ -190,7 +190,7 @@ public SnapshotStats getStats() { private static final String INCLUDE_GLOBAL_STATE = "include_global_state"; @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params params) { return Iterators.concat(Iterators.single((ToXContent) (b, p) -> { b.startObject() .field(SNAPSHOT, snapshot.getSnapshotId().getName()) diff --git a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponse.java b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponse.java index 13069119d3b9f..3ac90f882ab25 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponse.java @@ -89,11 +89,13 @@ public int hashCode() { } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params params) { return Iterators.concat( Iterators.single((ToXContent) (b, p) -> b.startObject().startArray("snapshots")), snapshots.stream() - .flatMap(s -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(s.toXContentChunked(), Spliterator.ORDERED), false)) + .flatMap( + s -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(s.toXContentChunked(params), Spliterator.ORDERED), false) + ) .iterator(), Iterators.single((b, p) -> b.endArray().endObject()) ); diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java index b2360de132387..e1db53e5e3e52 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponse.java @@ -67,7 +67,7 @@ public void writeTo(StreamOutput out) throws IOException { } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params outerParams) { return Iterators.concat( Iterators.single((b, p) -> b.startObject()), getMappings().entrySet().stream().map(indexEntry -> (ToXContent) (builder, params) -> { diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/recovery/RecoveryResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/recovery/RecoveryResponse.java index e53f17b68559e..1ec8f4ea90df6 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/recovery/RecoveryResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/recovery/RecoveryResponse.java @@ -65,7 +65,7 @@ public Map> shardRecoveryStates() { } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params params) { return Iterators.concat( Iterators.single((b, p) -> b.startObject()), shardRecoveryStates.entrySet() diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java index 0c5abb5788802..c0a7db8460433 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponse.java @@ -79,7 +79,7 @@ public void writeTo(StreamOutput out) throws IOException { } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params outerParams) { return Iterators.concat(Iterators.single(((builder, params) -> { builder.startObject(); RestActions.buildBroadcastShardsHeader(builder, params, this); diff --git a/server/src/main/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsResponse.java b/server/src/main/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsResponse.java index ad4c49c29a0e8..80c00a3224520 100644 --- a/server/src/main/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsResponse.java +++ b/server/src/main/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsResponse.java @@ -154,7 +154,7 @@ public String toString() { } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params params) { final boolean omitEmptySettings = indexToDefaultSettings.isEmpty(); return toXContentChunked(omitEmptySettings); } diff --git a/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesResponse.java b/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesResponse.java index f1bbc144cb4a1..7cd8f8adbdd28 100644 --- a/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesResponse.java +++ b/server/src/main/java/org/elasticsearch/action/fieldcaps/FieldCapabilitiesResponse.java @@ -152,7 +152,7 @@ private static void writeField(StreamOutput out, Map } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params params) { if (indexResponses.size() > 0) { throw new IllegalStateException("cannot serialize non-merged response"); } diff --git a/server/src/main/java/org/elasticsearch/action/support/nodes/BaseNodesXContentResponse.java b/server/src/main/java/org/elasticsearch/action/support/nodes/BaseNodesXContentResponse.java index 2922d33a85d40..4113788aafca8 100644 --- a/server/src/main/java/org/elasticsearch/action/support/nodes/BaseNodesXContentResponse.java +++ b/server/src/main/java/org/elasticsearch/action/support/nodes/BaseNodesXContentResponse.java @@ -33,7 +33,7 @@ protected BaseNodesXContentResponse(StreamInput in) throws IOException { } @Override - public final Iterator toXContentChunked() { + public final Iterator toXContentChunked(ToXContent.Params params) { return Iterators.concat(Iterators.single((b, p) -> { b.startObject(); RestActions.buildNodesHeader(b, p, this); diff --git a/server/src/main/java/org/elasticsearch/common/xcontent/ChunkedToXContent.java b/server/src/main/java/org/elasticsearch/common/xcontent/ChunkedToXContent.java index f565df41a7c0b..9fe5eefd45889 100644 --- a/server/src/main/java/org/elasticsearch/common/xcontent/ChunkedToXContent.java +++ b/server/src/main/java/org/elasticsearch/common/xcontent/ChunkedToXContent.java @@ -26,7 +26,7 @@ public interface ChunkedToXContent { * {@link ToXContent.Params} for each call until it is fully drained. * @return iterator over chunks of {@link ToXContent} */ - Iterator toXContentChunked(); + Iterator toXContentChunked(ToXContent.Params params); /** * Wraps the given instance in a {@link ToXContentObject} that will fully serialize the instance when serialized. @@ -35,7 +35,7 @@ public interface ChunkedToXContent { */ static ToXContentObject wrapAsXContentObject(ChunkedToXContent chunkedToXContent) { return (builder, params) -> { - Iterator serialization = chunkedToXContent.toXContentChunked(); + Iterator serialization = chunkedToXContent.toXContentChunked(params); while (serialization.hasNext()) { serialization.next().toXContent(builder, params); } diff --git a/server/src/main/java/org/elasticsearch/health/Diagnosis.java b/server/src/main/java/org/elasticsearch/health/Diagnosis.java index 2f3d44b1f6993..b3adddd1b6cc1 100644 --- a/server/src/main/java/org/elasticsearch/health/Diagnosis.java +++ b/server/src/main/java/org/elasticsearch/health/Diagnosis.java @@ -77,7 +77,7 @@ public Resource(Collection nodes) { } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params outerParams) { Iterator valuesIterator; if (nodes != null) { valuesIterator = nodes.stream().map(node -> (ToXContent) (builder, params) -> { @@ -147,11 +147,16 @@ public String getUniqueId() { } @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params outerParams) { Iterator resourcesIterator = Collections.emptyIterator(); if (affectedResources != null && affectedResources.size() > 0) { resourcesIterator = affectedResources.stream() - .flatMap(s -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(s.toXContentChunked(), Spliterator.ORDERED), false)) + .flatMap( + s -> StreamSupport.stream( + Spliterators.spliteratorUnknownSize(s.toXContentChunked(outerParams), Spliterator.ORDERED), + false + ) + ) .iterator(); } return Iterators.concat(Iterators.single((ToXContent) (builder, params) -> { diff --git a/server/src/main/java/org/elasticsearch/health/GetHealthAction.java b/server/src/main/java/org/elasticsearch/health/GetHealthAction.java index e8a8a823308ef..2029717b35451 100644 --- a/server/src/main/java/org/elasticsearch/health/GetHealthAction.java +++ b/server/src/main/java/org/elasticsearch/health/GetHealthAction.java @@ -93,7 +93,7 @@ public void writeTo(StreamOutput out) throws IOException { @Override @SuppressWarnings("unchecked") - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params outerParams) { return Iterators.concat(Iterators.single((ToXContent) (builder, params) -> { builder.startObject(); if (status != null) { @@ -111,7 +111,7 @@ public Iterator toXContentChunked() { // indicators however the affected resources which are the O(indices) fields are // flat mapped over all diagnoses within the indicator Iterators.single((ToXContent) (builder, params) -> builder.field(indicator.name())), - indicator.toXContentChunked() + indicator.toXContentChunked(outerParams) ) ) .toArray(Iterator[]::new) diff --git a/server/src/main/java/org/elasticsearch/health/HealthIndicatorResult.java b/server/src/main/java/org/elasticsearch/health/HealthIndicatorResult.java index 46ecabca15908..8ce2818c371c9 100644 --- a/server/src/main/java/org/elasticsearch/health/HealthIndicatorResult.java +++ b/server/src/main/java/org/elasticsearch/health/HealthIndicatorResult.java @@ -28,11 +28,16 @@ public record HealthIndicatorResult( List diagnosisList ) implements ChunkedToXContent { @Override - public Iterator toXContentChunked() { + public Iterator toXContentChunked(ToXContent.Params outerParams) { Iterator diagnosisIterator = Collections.emptyIterator(); if (diagnosisList != null && diagnosisList.isEmpty() == false) { diagnosisIterator = diagnosisList.stream() - .flatMap(s -> StreamSupport.stream(Spliterators.spliteratorUnknownSize(s.toXContentChunked(), Spliterator.ORDERED), false)) + .flatMap( + s -> StreamSupport.stream( + Spliterators.spliteratorUnknownSize(s.toXContentChunked(outerParams), Spliterator.ORDERED), + false + ) + ) .iterator(); } return Iterators.concat(Iterators.single((ToXContent) (builder, params) -> { diff --git a/server/src/main/java/org/elasticsearch/rest/ChunkedRestResponseBody.java b/server/src/main/java/org/elasticsearch/rest/ChunkedRestResponseBody.java index 746727e295557..f9c5aadc1f562 100644 --- a/server/src/main/java/org/elasticsearch/rest/ChunkedRestResponseBody.java +++ b/server/src/main/java/org/elasticsearch/rest/ChunkedRestResponseBody.java @@ -81,7 +81,7 @@ public void write(byte[] b, int off, int len) throws IOException { Streams.noCloseStream(out) ); - private final Iterator serialization = chunkedToXContent.toXContentChunked(); + private final Iterator serialization = chunkedToXContent.toXContentChunked(params); private BytesStream target; diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java index c2a3a265a33c0..bd7e416409ef9 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/get/GetSnapshotsResponseTests.java @@ -43,6 +43,7 @@ import static org.elasticsearch.snapshots.SnapshotInfo.INDEX_DETAILS_XCONTENT_PARAM; import static org.elasticsearch.test.AbstractXContentTestCase.chunkedXContentTester; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; import static org.hamcrest.CoreMatchers.containsString; public class GetSnapshotsResponseTests extends ESTestCase { @@ -179,7 +180,7 @@ public void testFromXContent() throws IOException { public void testToChunkedXContent() { final GetSnapshotsResponse response = createTestInstance(); - final Iterator serialization = response.toXContentChunked(); + final Iterator serialization = response.toXContentChunked(EMPTY_PARAMS); int chunks = 0; while (serialization.hasNext()) { serialization.next(); diff --git a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponseTests.java index df4fe643ab25c..26baa1a337eff 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/cluster/snapshots/status/SnapshotsStatusResponseTests.java @@ -17,6 +17,8 @@ import java.util.List; import java.util.function.Predicate; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; + public class SnapshotsStatusResponseTests extends AbstractChunkedSerializingTestCase { @Override @@ -58,7 +60,7 @@ public void testChunkCount() { // open and close chunk + one chunk per index chunksExpected += 2 + snapshot.getIndices().size(); } - final var iterator = instance.toXContentChunked(); + final var iterator = instance.toXContentChunked(EMPTY_PARAMS); int chunksSeen = 0; while (iterator.hasNext()) { iterator.next(); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java index 520737cc07c3a..0c58846483f50 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/mapping/get/GetMappingsResponseTests.java @@ -22,6 +22,8 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; + public class GetMappingsResponseTests extends AbstractWireSerializingTestCase { public void testCheckEqualsAndHashCode() { @@ -73,7 +75,7 @@ public void testChunkedXContentUsesChunkPerIndex() { .mapToObj(i -> "index-" + i) .collect(Collectors.toUnmodifiableMap(Function.identity(), k -> createMappingsForIndex())) ); - final var chunks = response.toXContentChunked(); + final var chunks = response.toXContentChunked(EMPTY_PARAMS); int chunkCount = 0; while (chunks.hasNext()) { chunks.next(); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/recovery/RecoveryResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/recovery/RecoveryResponseTests.java index 6cf36a8c12d2e..53a2201ec5f5a 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/recovery/RecoveryResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/recovery/RecoveryResponseTests.java @@ -23,6 +23,7 @@ import static java.util.Collections.emptyMap; import static java.util.Collections.emptySet; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; public class RecoveryResponseTests extends ESTestCase { @@ -57,7 +58,7 @@ public void testChunkedToXContent() { ), List.of() ); - final var iterator = recoveryResponse.toXContentChunked(); + final var iterator = recoveryResponse.toXContentChunked(EMPTY_PARAMS); int chunks = 0; while (iterator.hasNext()) { iterator.next(); diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java index efc68d2bbf976..2c211a66c7b28 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/segments/IndicesSegmentResponseTests.java @@ -22,6 +22,7 @@ import java.util.Collections; import java.util.List; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; import static org.elasticsearch.xcontent.XContentFactory.jsonBuilder; public class IndicesSegmentResponseTests extends ESTestCase { @@ -42,7 +43,7 @@ public void testToXContentSerialiationWithSortedFields() throws Exception { 0, Collections.emptyList() ); - var serialization = response.toXContentChunked(); + var serialization = response.toXContentChunked(EMPTY_PARAMS); try (XContentBuilder builder = jsonBuilder()) { while (serialization.hasNext()) { serialization.next().toXContent(builder, ToXContent.EMPTY_PARAMS); @@ -68,7 +69,7 @@ public void testSerializesOneChunkPerIndex() { Collections.emptyList() ); int chunks = 0; - final var iterator = response.toXContentChunked(); + final var iterator = response.toXContentChunked(EMPTY_PARAMS); while (iterator.hasNext()) { iterator.next(); chunks++; diff --git a/server/src/test/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsResponseTests.java b/server/src/test/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsResponseTests.java index 8e007bfcac39c..1fb3590c6a472 100644 --- a/server/src/test/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/admin/indices/settings/get/GetSettingsResponseTests.java @@ -21,6 +21,8 @@ import java.util.Set; import java.util.function.Predicate; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; + public class GetSettingsResponseTests extends AbstractChunkedSerializingTestCase { @Override @@ -76,7 +78,7 @@ protected Predicate getRandomFieldsExcludeFilter() { public void testOneChunkPerIndex() { final var instance = createTestInstance(); - final var iterator = instance.toXContentChunked(); + final var iterator = instance.toXContentChunked(EMPTY_PARAMS); int chunks = 0; while (iterator.hasNext()) { chunks++; diff --git a/server/src/test/java/org/elasticsearch/action/fieldcaps/MergedFieldCapabilitiesResponseTests.java b/server/src/test/java/org/elasticsearch/action/fieldcaps/MergedFieldCapabilitiesResponseTests.java index 94c339e417c0b..7954543c17940 100644 --- a/server/src/test/java/org/elasticsearch/action/fieldcaps/MergedFieldCapabilitiesResponseTests.java +++ b/server/src/test/java/org/elasticsearch/action/fieldcaps/MergedFieldCapabilitiesResponseTests.java @@ -21,6 +21,8 @@ import java.util.Map; import java.util.function.Predicate; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; + public class MergedFieldCapabilitiesResponseTests extends AbstractChunkedSerializingTestCase { @Override @@ -207,7 +209,7 @@ private static FieldCapabilitiesResponse createSimpleResponse() { public void testExpectedChunkSizes() { { final FieldCapabilitiesResponse instance = FieldCapabilitiesResponseTests.createResponseWithFailures(); - final var iterator = instance.toXContentChunked(); + final var iterator = instance.toXContentChunked(EMPTY_PARAMS); int chunks = 0; while (iterator.hasNext()) { iterator.next(); @@ -221,7 +223,7 @@ public void testExpectedChunkSizes() { } { final FieldCapabilitiesResponse instance = createTestInstance(); - final var iterator = instance.toXContentChunked(); + final var iterator = instance.toXContentChunked(EMPTY_PARAMS); int chunks = 0; while (iterator.hasNext()) { iterator.next(); diff --git a/server/src/test/java/org/elasticsearch/health/GetHealthResponseTests.java b/server/src/test/java/org/elasticsearch/health/GetHealthResponseTests.java index 454383d3ecf3f..0c16f22bc7656 100644 --- a/server/src/test/java/org/elasticsearch/health/GetHealthResponseTests.java +++ b/server/src/test/java/org/elasticsearch/health/GetHealthResponseTests.java @@ -13,7 +13,6 @@ import org.elasticsearch.common.xcontent.XContentHelper; import org.elasticsearch.health.GetHealthAction.Response; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xcontent.ToXContent; import org.elasticsearch.xcontent.XContentBuilder; import org.elasticsearch.xcontent.XContentFactory; @@ -24,6 +23,7 @@ import java.util.Locale; import java.util.Map; +import static org.elasticsearch.xcontent.ToXContent.EMPTY_PARAMS; import static org.hamcrest.Matchers.is; public class GetHealthResponseTests extends ESTestCase { @@ -35,9 +35,9 @@ public void testToXContent() throws IOException { Response response = new Response(ClusterName.DEFAULT, indicatorResults, true); XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); - response.toXContentChunked().forEachRemaining(xcontent -> { + response.toXContentChunked(EMPTY_PARAMS).forEachRemaining(xcontent -> { try { - xcontent.toXContent(builder, ToXContent.EMPTY_PARAMS); + xcontent.toXContent(builder, EMPTY_PARAMS); } catch (IOException e) { logger.error(e.getMessage(), e); fail(e.getMessage()); diff --git a/server/src/test/java/org/elasticsearch/health/HealthIndicatorResultTests.java b/server/src/test/java/org/elasticsearch/health/HealthIndicatorResultTests.java index df0cf83cd8bbe..8825e7d6f1134 100644 --- a/server/src/test/java/org/elasticsearch/health/HealthIndicatorResultTests.java +++ b/server/src/test/java/org/elasticsearch/health/HealthIndicatorResultTests.java @@ -70,7 +70,7 @@ public void testToXContent() throws Exception { HealthIndicatorResult result = new HealthIndicatorResult(name, status, symptom, details, impacts, diagnosisList); XContentBuilder builder = XContentFactory.jsonBuilder().prettyPrint(); - result.toXContentChunked().forEachRemaining(xcontent -> { + result.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> { try { xcontent.toXContent(builder, ToXContent.EMPTY_PARAMS); } catch (IOException e) { @@ -106,7 +106,7 @@ public void testToXContent() throws Exception { if (diagnosis1.affectedResources() != null) { XContentBuilder diagnosisXContent = XContentFactory.jsonBuilder().prettyPrint(); - diagnosis1.toXContentChunked().forEachRemaining(xcontent -> { + diagnosis1.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> { try { xcontent.toXContent(diagnosisXContent, ToXContent.EMPTY_PARAMS); } catch (IOException e) { @@ -131,7 +131,7 @@ public void testToXContent() throws Exception { expectedDiagnosis2.put("help_url", diagnosis2.definition().helpURL()); if (diagnosis2.affectedResources() != null) { XContentBuilder diagnosisXContent = XContentFactory.jsonBuilder().prettyPrint(); - diagnosis2.toXContentChunked().forEachRemaining(xcontent -> { + diagnosis2.toXContentChunked(ToXContent.EMPTY_PARAMS).forEachRemaining(xcontent -> { try { xcontent.toXContent(diagnosisXContent, ToXContent.EMPTY_PARAMS); } catch (IOException e) { @@ -199,7 +199,7 @@ public void testChunkCount() { // -> each Diagnosis yields 5 chunks => 10 chunks from both diagnosis // -> HealthIndicatorResult surrounds the diagnosis list by 2 chunks int chunksExpected = 12; - var iterator = result.toXContentChunked(); + var iterator = result.toXContentChunked(ToXContent.EMPTY_PARAMS); int chunksSeen = 0; while (iterator.hasNext()) { iterator.next(); diff --git a/server/src/test/java/org/elasticsearch/rest/ChunkedRestResponseBodyTests.java b/server/src/test/java/org/elasticsearch/rest/ChunkedRestResponseBodyTests.java index 72bd2c65fc6b9..de26b8fc88ae4 100644 --- a/server/src/test/java/org/elasticsearch/rest/ChunkedRestResponseBodyTests.java +++ b/server/src/test/java/org/elasticsearch/rest/ChunkedRestResponseBodyTests.java @@ -30,16 +30,18 @@ public class ChunkedRestResponseBodyTests extends ESTestCase { public void testEncodesChunkedXContentCorrectly() throws IOException { - final ChunkedToXContent chunkedToXContent = () -> Iterators.forArray(new ToXContent[] { (b, p) -> b.startObject(), (b, p) -> { - if (randomBoolean()) { - b.flush(); - } - b.mapContents(Map.of("foo", "bar", "some_other_key", "some_other_value")); - return b; - }, (b, p) -> b.stringListField("list_field", List.of("string", "otherString")).endObject() }); + final ChunkedToXContent chunkedToXContent = (ToXContent.Params outerParams) -> Iterators.forArray( + new ToXContent[] { (b, p) -> b.startObject(), (b, p) -> { + if (randomBoolean()) { + b.flush(); + } + b.mapContents(Map.of("foo", "bar", "some_other_key", "some_other_value")); + return b; + }, (b, p) -> b.stringListField("list_field", List.of("string", "otherString")).endObject() } + ); final XContent randomXContent = randomFrom(XContentType.values()).xContent(); final XContentBuilder builderDirect = XContentBuilder.builder(randomXContent); - var iter = chunkedToXContent.toXContentChunked(); + var iter = chunkedToXContent.toXContentChunked(ToXContent.EMPTY_PARAMS); while (iter.hasNext()) { iter.next().toXContent(builderDirect, ToXContent.EMPTY_PARAMS); } diff --git a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java index 157276b8ef108..28010f5b2f5b2 100644 --- a/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java +++ b/test/framework/src/main/java/org/elasticsearch/test/AbstractXContentTestCase.java @@ -91,7 +91,7 @@ public static XContentTester chunkedXContentTes ) { return new XContentTester<>(createParser, instanceSupplier, (testInstance, xContentType) -> { try (XContentBuilder builder = XContentBuilder.builder(xContentType.xContent())) { - var serialization = testInstance.toXContentChunked(); + var serialization = testInstance.toXContentChunked(toXContentParams); while (serialization.hasNext()) { serialization.next().toXContent(builder, toXContentParams); }