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 #65362

Merged
merged 3 commits into from
Nov 25, 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 @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

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

not sure I understand the comment ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

matching in fromMediaTypeOrFormat can use startsWith so that
application/json; charset=utf-8 can be matched. however this assumes that ; is immediately following mediaTypeWithoutParameters

I don't want to refactor this in 7.x, the intention of .replaceAll("\\s*;\\s*",";"); was to make it slightly easier if you provided application/vnd.elasticsearch+json;compatible-with=7 ; charset=utf-8 with accidental space.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have updated the comment with a reference to a method

}
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,23 @@ 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));

}
}