From 7e39e815df929c8be3d8fd658a9cf66002b358d6 Mon Sep 17 00:00:00 2001 From: pgomulka Date: Mon, 23 Nov 2020 13:20:40 +0100 Subject: [PATCH 1/2] Allow passing versioned media types to 7.x server a follow up after #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 #51816 --- .../common/xcontent/XContentType.java | 7 +++++-- .../common/xcontent/XContentTypeTests.java | 20 ++++++++++++++++++- 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/libs/x-content/src/main/java/org/elasticsearch/common/xcontent/XContentType.java b/libs/x-content/src/main/java/org/elasticsearch/common/xcontent/XContentType.java index d058ff7608718..e75817d07ab38 100644 --- a/libs/x-content/src/main/java/org/elasticsearch/common/xcontent/XContentType.java +++ b/libs/x-content/src/main/java/org/elasticsearch/common/xcontent/XContentType.java @@ -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; } @@ -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; diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java b/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java index 0dc2be69348bd..39cc0b7f6a1b3 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java @@ -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)); + } } From 5d822231c00903b77cd176a8ef964b29251c5ec1 Mon Sep 17 00:00:00 2001 From: pgomulka Date: Mon, 23 Nov 2020 14:42:35 +0100 Subject: [PATCH 2/2] remove redundant test case --- .../org/elasticsearch/common/xcontent/XContentTypeTests.java | 2 -- 1 file changed, 2 deletions(-) diff --git a/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java b/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java index 39cc0b7f6a1b3..88a2ee033f59b 100644 --- a/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java +++ b/server/src/test/java/org/elasticsearch/common/xcontent/XContentTypeTests.java @@ -115,8 +115,6 @@ public void testVersionedMediaType() throws Exception { 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)); } }