Skip to content

Commit 8240945

Browse files
authored
Remove unused ChunkedToXContent#toXContentChunkedV7 (elastic#114728)
We don't support the v7 REST API in v9, so this commit removes the now-unused `ChunkedToXContent#toXContentChunkedV7` method. It also introduces a similar `ChunkedToXContent#toXContentChunkedV8` method for implementations to use for v8 REST API compatibility.
1 parent 6620be3 commit 8240945

File tree

6 files changed

+33
-39
lines changed

6 files changed

+33
-39
lines changed

server/src/main/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponse.java

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -92,17 +92,12 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerP
9292
if (emitState(outerParams)) {
9393
deprecationLogger.critical(DeprecationCategory.API, "reroute_cluster_state", STATE_FIELD_DEPRECATION_MESSAGE);
9494
}
95-
return toXContentChunkedV7(outerParams);
96-
}
97-
98-
@Override
99-
public Iterator<? extends ToXContent> toXContentChunkedV7(ToXContent.Params params) {
100-
return ChunkedToXContent.builder(params).object(b -> {
95+
return ChunkedToXContent.builder(outerParams).object(b -> {
10196
b.field(ACKNOWLEDGED_KEY, isAcknowledged());
102-
if (emitState(params)) {
97+
if (emitState(outerParams)) {
10398
b.xContentObject("state", state);
10499
}
105-
if (params.paramAsBoolean("explain", false)) {
100+
if (outerParams.paramAsBoolean("explain", false)) {
106101
b.append(explanations);
107102
}
108103
});

server/src/main/java/org/elasticsearch/common/xcontent/ChunkedToXContent.java

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,27 @@ static ChunkedToXContentBuilder builder(ToXContent.Params params) {
3434
return new ChunkedToXContentBuilder(params);
3535
}
3636

37+
/**
38+
* Create an iterator of {@link ToXContent} chunks for a REST response for the given {@link RestApiVersion}. Each chunk is serialized
39+
* with the same {@link XContentBuilder} and {@link ToXContent.Params}, which is also the same as the {@link ToXContent.Params} passed
40+
* as the {@code params} argument. For best results, all chunks should be {@code O(1)} size. The last chunk in the iterator must always
41+
* yield at least one byte of output. See also {@link ChunkedToXContentHelper} for some handy utilities.
42+
* <p>
43+
* Note that chunked response bodies cannot send deprecation warning headers once transmission has started, so implementations must
44+
* check for deprecated feature use before returning.
45+
* <p>
46+
* By default, delegates to {@link #toXContentChunked} or {#toXContentChunkedV8}.
47+
*
48+
* @return iterator over chunks of {@link ToXContent}
49+
*/
50+
default Iterator<? extends ToXContent> toXContentChunked(RestApiVersion restApiVersion, ToXContent.Params params) {
51+
return switch (restApiVersion) {
52+
case V_7 -> throw new AssertionError("v7 API not supported");
53+
case V_8 -> toXContentChunkedV8(params);
54+
case V_9 -> toXContentChunked(params);
55+
};
56+
}
57+
3758
/**
3859
* Create an iterator of {@link ToXContent} chunks for a REST response. Each chunk is serialized with the same {@link XContentBuilder}
3960
* and {@link ToXContent.Params}, which is also the same as the {@link ToXContent.Params} passed as the {@code params} argument. For
@@ -48,20 +69,20 @@ static ChunkedToXContentBuilder builder(ToXContent.Params params) {
4869
Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params);
4970

5071
/**
51-
* Create an iterator of {@link ToXContent} chunks for a response to the {@link RestApiVersion#V_7} API. Each chunk is serialized with
72+
* Create an iterator of {@link ToXContent} chunks for a response to the {@link RestApiVersion#V_8} API. Each chunk is serialized with
5273
* the same {@link XContentBuilder} and {@link ToXContent.Params}, which is also the same as the {@link ToXContent.Params} passed as the
5374
* {@code params} argument. For best results, all chunks should be {@code O(1)} size. The last chunk in the iterator must always yield
5475
* at least one byte of output. See also {@link ChunkedToXContentHelper} for some handy utilities.
5576
* <p>
56-
* Similar to {@link #toXContentChunked} but for the {@link RestApiVersion#V_7} API. By default this method delegates to {@link
77+
* Similar to {@link #toXContentChunked} but for the {@link RestApiVersion#V_8} API. By default this method delegates to {@link
5778
* #toXContentChunked}.
5879
* <p>
5980
* Note that chunked response bodies cannot send deprecation warning headers once transmission has started, so implementations must
6081
* check for deprecated feature use before returning.
6182
*
6283
* @return iterator over chunks of {@link ToXContent}
6384
*/
64-
default Iterator<? extends ToXContent> toXContentChunkedV7(ToXContent.Params params) {
85+
default Iterator<? extends ToXContent> toXContentChunkedV8(ToXContent.Params params) {
6586
return toXContentChunked(params);
6687
}
6788

server/src/main/java/org/elasticsearch/rest/ChunkedRestResponseBodyPart.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import org.elasticsearch.core.CheckedConsumer;
2020
import org.elasticsearch.core.IOUtils;
2121
import org.elasticsearch.core.Releasables;
22-
import org.elasticsearch.core.RestApiVersion;
2322
import org.elasticsearch.core.Streams;
2423
import org.elasticsearch.logging.LogManager;
2524
import org.elasticsearch.logging.Logger;
@@ -129,9 +128,10 @@ public void write(byte[] b, int off, int len) throws IOException {
129128
Streams.noCloseStream(out)
130129
);
131130

132-
private final Iterator<? extends ToXContent> serialization = builder.getRestApiVersion() == RestApiVersion.V_7
133-
? chunkedToXContent.toXContentChunkedV7(params)
134-
: chunkedToXContent.toXContentChunked(params);
131+
private final Iterator<? extends ToXContent> serialization = chunkedToXContent.toXContentChunked(
132+
builder.getRestApiVersion(),
133+
params
134+
);
135135

136136
private BytesStream target;
137137

server/src/main/java/org/elasticsearch/rest/StreamingXContentResponse.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
import org.elasticsearch.core.RefCounted;
2626
import org.elasticsearch.core.Releasable;
2727
import org.elasticsearch.core.Releasables;
28-
import org.elasticsearch.core.RestApiVersion;
2928
import org.elasticsearch.core.Streams;
3029
import org.elasticsearch.transport.Transports;
3130
import org.elasticsearch.xcontent.ToXContent;
@@ -125,9 +124,7 @@ public void close() {
125124
}
126125

127126
private Iterator<? extends ToXContent> getChunksIterator(StreamingFragment fragment) {
128-
return xContentBuilder.getRestApiVersion() == RestApiVersion.V_7
129-
? fragment.fragment().toXContentChunkedV7(params)
130-
: fragment.fragment().toXContentChunked(params);
127+
return fragment.fragment().toXContentChunked(xContentBuilder.getRestApiVersion(), params);
131128
}
132129

133130
/**

server/src/test/java/org/elasticsearch/action/admin/cluster/reroute/ClusterRerouteResponseTests.java

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
import org.elasticsearch.xcontent.ToXContent;
3838

3939
import java.io.IOException;
40-
import java.util.Iterator;
4140
import java.util.List;
4241
import java.util.Map;
4342
import java.util.Objects;
@@ -323,21 +322,6 @@ private void assertXContent(
323322

324323
AbstractChunkedSerializingTestCase.assertChunkCount(response, params, o -> expectedChunks[0]);
325324
assertCriticalWarnings(criticalDeprecationWarnings);
326-
327-
// check the v7 API too
328-
AbstractChunkedSerializingTestCase.assertChunkCount(new ChunkedToXContent() {
329-
@Override
330-
public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params outerParams) {
331-
return response.toXContentChunkedV7(outerParams);
332-
}
333-
334-
@Override
335-
public boolean isFragment() {
336-
return response.isFragment();
337-
}
338-
}, params, o -> expectedChunks[0]++);
339-
// the v7 API should not emit any deprecation warnings
340-
assertCriticalWarnings();
341325
}
342326

343327
private static ClusterRerouteResponse createClusterRerouteResponse(ClusterState clusterState) {

x-pack/plugin/inference/src/main/java/org/elasticsearch/xpack/inference/rest/ServerSentEventsRestActionListener.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import org.elasticsearch.common.xcontent.ChunkedToXContentHelper;
2424
import org.elasticsearch.core.IOUtils;
2525
import org.elasticsearch.core.Releasables;
26-
import org.elasticsearch.core.RestApiVersion;
2726
import org.elasticsearch.core.Streams;
2827
import org.elasticsearch.rest.ChunkedRestResponseBodyPart;
2928
import org.elasticsearch.rest.RestChannel;
@@ -299,9 +298,7 @@ private ServerSentEventResponseBodyPart(ServerSentEvents event, ChunkedToXConten
299298
this.xContentBuilder = new LazyInitializable<>(
300299
() -> channel.newBuilder(channel.request().getXContentType(), null, true, Streams.noCloseStream(out))
301300
);
302-
this.serialization = channel.request().getRestApiVersion() == RestApiVersion.V_7
303-
? item.toXContentChunkedV7(params)
304-
: item.toXContentChunked(params);
301+
this.serialization = item.toXContentChunked(channel.request().getRestApiVersion(), params);
305302
}
306303

307304
@Override

0 commit comments

Comments
 (0)