Skip to content

Commit

Permalink
Allow passing versioned media types to 7.x server (#63071)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
pgomulka committed Oct 2, 2020
1 parent 614f4c1 commit eb630e5
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,8 @@ public static XContentType fromMediaTypeOrFormat(String mediaType) {
if (mediaType == null) {
return null;
}

mediaType = removeVersionInMediaType(mediaType);
for (XContentType type : values()) {
if (isSameMediaTypeOrFormatAs(mediaType, type)) {
return type;
Expand All @@ -137,6 +139,23 @@ public static XContentType fromMediaTypeOrFormat(String mediaType) {
return null;
}

/**
* Clients compatible with ES 7.x might start sending media types with versioned media type
* in a form of application/vnd.elasticsearch+json;compatible-with=7.
* This has to be removed in order to be used in 7.x server.
* The same client connecting using that media type will be able to communicate with ES 8 thanks to compatible API.
* @param mediaType - a media type used on Content-Type header, might contain versioned media type.
*
* @return a media type string without
*/
private static String removeVersionInMediaType(String mediaType) {
if (mediaType.contains("vnd.elasticsearch")) {
return mediaType.replaceAll("vnd.elasticsearch\\+", "")
.replaceAll("\\s*;\\s*compatible-with=\\d+", "");
}
return mediaType;
}

/**
* Attempts to match the given media type with the known {@link XContentType} values. This match is done in a case-insensitive manner.
* The provided media type should not include any parameters. This method is suitable for parsing part of the {@code Content-Type}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static org.hamcrest.Matchers.nullValue;

public class XContentTypeTests extends ESTestCase {

public void testFromJson() throws Exception {
String mediaType = "application/json";
XContentType expectedXContentType = XContentType.JSON;
Expand Down Expand Up @@ -84,4 +85,20 @@ public void testFromRubbish() throws Exception {
assertThat(XContentType.fromMediaTypeOrFormat("text/plain"), nullValue());
assertThat(XContentType.fromMediaTypeOrFormat("gobbly;goop"), nullValue());
}

public void testVersionedMediaType() throws Exception {
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+json;compatible-with=7"),
equalTo(XContentType.JSON));
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+yaml;compatible-with=7"),
equalTo(XContentType.YAML));
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+cbor;compatible-with=7"),
equalTo(XContentType.CBOR));
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+smile;compatible-with=7"),
equalTo(XContentType.SMILE));

assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+json ;compatible-with=7"),
equalTo(XContentType.JSON));
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+json ;compatible-with=7;charset=utf-8"),
equalTo(XContentType.JSON));
}
}

0 comments on commit eb630e5

Please sign in to comment.