Skip to content

Commit

Permalink
Allow passing versioned media types to 7.x server
Browse files Browse the repository at this point in the history
a follow up after elastic#63071 where it missed the XContentType.fromMediaType
method.
That method also have to remove the vendor specific substrings
(vnd.elasticsearch+ and compatible-with parameter) from mediaType value

relates elastic#51816
  • Loading branch information
pgomulka committed Nov 23, 2020
1 parent dde4695 commit 7e39e81
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ public static XContentType fromMediaTypeOrFormat(String mediaType) {
private static String removeVersionInMediaType(String mediaType) {
if (mediaType.contains("vnd.elasticsearch")) {
return mediaType.replaceAll("vnd.elasticsearch\\+", "")
.replaceAll("\\s*;\\s*compatible-with=\\d+", "");
.replaceAll("\\s*;\\s*compatible-with=\\d+", "")
.replaceAll("\\s*;\\s*",";"); // to allow matching using startsWith
}
return mediaType;
}
Expand All @@ -162,7 +163,9 @@ private static String removeVersionInMediaType(String mediaType) {
* HTTP header. This method will return {@code null} if no match is found
*/
public static XContentType fromMediaType(String mediaType) {
final String lowercaseMediaType = Objects.requireNonNull(mediaType, "mediaType cannot be null").toLowerCase(Locale.ROOT);
String lowercaseMediaType = Objects.requireNonNull(mediaType, "mediaType cannot be null").toLowerCase(Locale.ROOT);
lowercaseMediaType = removeVersionInMediaType(lowercaseMediaType);

for (XContentType type : values()) {
if (type.mediaTypeWithoutParameters().equals(lowercaseMediaType)) {
return type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,25 @@ public void testVersionedMediaType() throws Exception {

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"),
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+json ;compatible-with=7 ; charset=utf-8"),
equalTo(XContentType.JSON));
assertThat(XContentType.fromMediaTypeOrFormat("application/vnd.elasticsearch+json;charset=utf-8;compatible-with=7"),
equalTo(XContentType.JSON));

//we don't expect charset parameters when using fromMediaType
assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+json;compatible-with=7"),
equalTo(XContentType.JSON));
assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+yaml;compatible-with=7"),
equalTo(XContentType.YAML));
assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+cbor;compatible-with=7"),
equalTo(XContentType.CBOR));
assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+smile;compatible-with=7"),
equalTo(XContentType.SMILE));

assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+json ;compatible-with=7"),
equalTo(XContentType.JSON));
assertThat(XContentType.fromMediaType("application/vnd.elasticsearch+json ;compatible-with=7"),
equalTo(XContentType.JSON));

}
}

0 comments on commit 7e39e81

Please sign in to comment.