Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow passing versioned media types to 7.x server #63071

Merged
merged 2 commits into from
Oct 2, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would suggest to wrap this in if(mediaType.contains("vnd.elasticsearch"){ ...} else return mediaType. It is a micro-optimization, but will probably save a few CPU cycles. Also I think you can collapse the two replaceAll into a single line.

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));
}
}