Skip to content

Commit eb630e5

Browse files
authored
Allow passing versioned media types to 7.x server (#63071)
7.x client can pass media type with a version which will return a 7.x version of the api in ES 8. In ES server 7 this media type shoulld be accepted but it serve the same version of the API (7x) relates #61427
1 parent 614f4c1 commit eb630e5

File tree

2 files changed

+36
-0
lines changed

2 files changed

+36
-0
lines changed

libs/x-content/src/main/java/org/elasticsearch/common/xcontent/XContentType.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,8 @@ public static XContentType fromMediaTypeOrFormat(String mediaType) {
124124
if (mediaType == null) {
125125
return null;
126126
}
127+
128+
mediaType = removeVersionInMediaType(mediaType);
127129
for (XContentType type : values()) {
128130
if (isSameMediaTypeOrFormatAs(mediaType, type)) {
129131
return type;
@@ -137,6 +139,23 @@ public static XContentType fromMediaTypeOrFormat(String mediaType) {
137139
return null;
138140
}
139141

142+
/**
143+
* Clients compatible with ES 7.x might start sending media types with versioned media type
144+
* in a form of application/vnd.elasticsearch+json;compatible-with=7.
145+
* This has to be removed in order to be used in 7.x server.
146+
* The same client connecting using that media type will be able to communicate with ES 8 thanks to compatible API.
147+
* @param mediaType - a media type used on Content-Type header, might contain versioned media type.
148+
*
149+
* @return a media type string without
150+
*/
151+
private static String removeVersionInMediaType(String mediaType) {
152+
if (mediaType.contains("vnd.elasticsearch")) {
153+
return mediaType.replaceAll("vnd.elasticsearch\\+", "")
154+
.replaceAll("\\s*;\\s*compatible-with=\\d+", "");
155+
}
156+
return mediaType;
157+
}
158+
140159
/**
141160
* Attempts to match the given media type with the known {@link XContentType} values. This match is done in a case-insensitive manner.
142161
* The provided media type should not include any parameters. This method is suitable for parsing part of the {@code Content-Type}

server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import static org.hamcrest.Matchers.nullValue;
2727

2828
public class XContentTypeTests extends ESTestCase {
29+
2930
public void testFromJson() throws Exception {
3031
String mediaType = "application/json";
3132
XContentType expectedXContentType = XContentType.JSON;
@@ -84,4 +85,20 @@ public void testFromRubbish() throws Exception {
8485
assertThat(XContentType.fromMediaTypeOrFormat("text/plain"), nullValue());
8586
assertThat(XContentType.fromMediaTypeOrFormat("gobbly;goop"), nullValue());
8687
}
88+
89+
public void testVersionedMediaType() throws Exception {
90+
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+json;compatible-with=7"),
91+
equalTo(XContentType.JSON));
92+
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+yaml;compatible-with=7"),
93+
equalTo(XContentType.YAML));
94+
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+cbor;compatible-with=7"),
95+
equalTo(XContentType.CBOR));
96+
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+smile;compatible-with=7"),
97+
equalTo(XContentType.SMILE));
98+
99+
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+json ;compatible-with=7"),
100+
equalTo(XContentType.JSON));
101+
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+json ;compatible-with=7;charset=utf-8"),
102+
equalTo(XContentType.JSON));
103+
}
87104
}

0 commit comments

Comments
 (0)