Skip to content

Commit f8636e5

Browse files
authored
Support content type application/x-ndjson in DeprecationRestHandler (#36025)
org.elasticsearch.rest.RestController#hasContentType checks to see if the RestHandler supports the `application/x-ndjson` Content-Type. DeprecationRestHandler is a wrapper around the real RestHandler, and prior to this change would always return `false` due to the interface's default supportsContentStream(). This prevents API's that use multi-line JSON from properly being deprecated resulting in an HTTP 406 error. This change ensures that the DeprecationRestHandler honors the supportsContentStream() of the wrapped RestHandler. Relates to #35958
1 parent 95d9cef commit f8636e5

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

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

+5
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,11 @@ public void handleRequest(RestRequest request, RestChannel channel, NodeClient c
6262
handler.handleRequest(request, channel, client);
6363
}
6464

65+
@Override
66+
public boolean supportsContentStream() {
67+
return handler.supportsContentStream();
68+
}
69+
6570
/**
6671
* This does a very basic pass at validating that a header's value contains only expected characters according to RFC-5987, and those
6772
* that it references.

server/src/test/java/org/elasticsearch/rest/DeprecationRestHandlerTests.java

+20-2
Original file line numberDiff line numberDiff line change
@@ -24,22 +24,30 @@
2424
import org.elasticsearch.common.logging.DeprecationLogger;
2525
import org.elasticsearch.test.ESTestCase;
2626

27+
import org.junit.Before;
2728
import org.mockito.InOrder;
2829

2930
import static org.mockito.Mockito.inOrder;
3031
import static org.mockito.Mockito.mock;
32+
import static org.mockito.Mockito.when;
3133

3234
/**
3335
* Tests {@link DeprecationRestHandler}.
3436
*/
3537
public class DeprecationRestHandlerTests extends ESTestCase {
3638

37-
private final RestHandler handler = mock(RestHandler.class);
39+
private RestHandler handler;
3840
/**
3941
* Note: Headers should only use US ASCII (and this inevitably becomes one!).
4042
*/
4143
private final String deprecationMessage = randomAlphaOfLengthBetween(1, 30);
42-
private final DeprecationLogger deprecationLogger = mock(DeprecationLogger.class);
44+
private DeprecationLogger deprecationLogger;
45+
46+
@Before
47+
public void setup() {
48+
handler = mock(RestHandler.class);
49+
deprecationLogger = mock(DeprecationLogger.class);
50+
}
4351

4452
public void testNullHandler() {
4553
expectThrows(NullPointerException.class, () -> new DeprecationRestHandler(null, deprecationMessage, deprecationLogger));
@@ -114,6 +122,16 @@ public void testInvalidHeaderValueEmpty() {
114122
expectThrows(IllegalArgumentException.class, () -> DeprecationRestHandler.requireValidHeader(blank));
115123
}
116124

125+
public void testSupportsContentStreamTrue() {
126+
when(handler.supportsContentStream()).thenReturn(true);
127+
assertTrue(new DeprecationRestHandler(handler, deprecationMessage, deprecationLogger).supportsContentStream());
128+
}
129+
130+
public void testSupportsContentStreamFalse() {
131+
when(handler.supportsContentStream()).thenReturn(false);
132+
assertFalse(new DeprecationRestHandler(handler, deprecationMessage, deprecationLogger).supportsContentStream());
133+
}
134+
117135
/**
118136
* {@code ASCIIHeaderGenerator} only uses characters expected to be valid in headers (simplified US-ASCII).
119137
*/

0 commit comments

Comments
 (0)