Skip to content

Commit c2c50d5

Browse files
authored
Make scripted search templates work with new mediaType from XContentType.JSON (#67677)
Stored scripts can have content_type option set, however when empty they default to XContentType.JSON#mediaType(). Commit 5e74f79 has changed this in master (ES8) method to return application/json;charset=utf-8 (previously application/json; charset=UTF-8) This means that when upgrading ES from version 7 to 8 stored script will fail when being used as the encoder is being matched with string equality (map key) This commit address this by adding back (in addition) the old application/json; charset=UTF-8 into the encoders map. closes #66986
1 parent d751c43 commit c2c50d5

File tree

4 files changed

+30
-36
lines changed

4 files changed

+30
-36
lines changed

modules/lang-mustache/src/main/java/org/elasticsearch/script/mustache/CustomMustacheFactory.java

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
import com.github.mustachejava.codes.DefaultMustache;
3131
import com.github.mustachejava.codes.IterableCode;
3232
import com.github.mustachejava.codes.WriteCode;
33-
import org.apache.lucene.search.highlight.DefaultEncoder;
3433
import org.elasticsearch.common.Strings;
3534
import org.elasticsearch.common.xcontent.XContentBuilder;
3635
import org.elasticsearch.common.xcontent.XContentType;
@@ -51,30 +50,31 @@
5150
import java.util.regex.Pattern;
5251

5352
public class CustomMustacheFactory extends DefaultMustacheFactory {
53+
static final String V7_JSON_MEDIA_TYPE_WITH_CHARSET = "application/json; charset=UTF-8";
54+
static final String JSON_MEDIA_TYPE_WITH_CHARSET = "application/json;charset=utf-8";
55+
static final String JSON_MEDIA_TYPE = "application/json";
56+
static final String PLAIN_TEXT_MEDIA_TYPE = "text/plain";
57+
static final String X_WWW_FORM_URLENCODED_MEDIA_TYPE = "application/x-www-form-urlencoded";
5458

55-
static final String JSON_MIME_TYPE_WITH_CHARSET = "application/json;charset=utf-8";
56-
static final String JSON_MIME_TYPE = "application/json";
57-
static final String PLAIN_TEXT_MIME_TYPE = "text/plain";
58-
static final String X_WWW_FORM_URLENCODED_MIME_TYPE = "application/x-www-form-urlencoded";
59-
60-
private static final String DEFAULT_MIME_TYPE = JSON_MIME_TYPE;
59+
private static final String DEFAULT_MEDIA_TYPE = JSON_MEDIA_TYPE;
6160

6261
private static final Map<String, Supplier<Encoder>> ENCODERS = Map.of(
63-
JSON_MIME_TYPE_WITH_CHARSET, JsonEscapeEncoder::new,
64-
JSON_MIME_TYPE, JsonEscapeEncoder::new,
65-
PLAIN_TEXT_MIME_TYPE, DefaultEncoder::new,
66-
X_WWW_FORM_URLENCODED_MIME_TYPE, UrlEncoder::new);
62+
V7_JSON_MEDIA_TYPE_WITH_CHARSET, JsonEscapeEncoder::new,
63+
JSON_MEDIA_TYPE_WITH_CHARSET, JsonEscapeEncoder::new,
64+
JSON_MEDIA_TYPE, JsonEscapeEncoder::new,
65+
PLAIN_TEXT_MEDIA_TYPE, DefaultEncoder::new,
66+
X_WWW_FORM_URLENCODED_MEDIA_TYPE, UrlEncoder::new);
6767

6868
private final Encoder encoder;
6969

70-
public CustomMustacheFactory(String mimeType) {
70+
public CustomMustacheFactory(String mediaType) {
7171
super();
7272
setObjectHandler(new CustomReflectionObjectHandler());
73-
this.encoder = createEncoder(mimeType);
73+
this.encoder = createEncoder(mediaType);
7474
}
7575

7676
public CustomMustacheFactory() {
77-
this(DEFAULT_MIME_TYPE);
77+
this(DEFAULT_MEDIA_TYPE);
7878
}
7979

8080
@Override
@@ -86,10 +86,10 @@ public void encode(String value, Writer writer) {
8686
}
8787
}
8888

89-
static Encoder createEncoder(String mimeType) {
90-
final Supplier<Encoder> supplier = ENCODERS.get(mimeType);
89+
static Encoder createEncoder(String mediaType) {
90+
final Supplier<Encoder> supplier = ENCODERS.get(mediaType);
9191
if (supplier == null) {
92-
throw new IllegalArgumentException("No encoder found for MIME type [" + mimeType + "]");
92+
throw new IllegalArgumentException("No encoder found for media type [" + mediaType + "]");
9393
}
9494
return supplier.get();
9595
}

modules/lang-mustache/src/test/java/org/elasticsearch/script/mustache/CustomMustacheFactoryTests.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828

2929
import static java.util.Collections.emptyMap;
3030
import static java.util.Collections.singletonMap;
31-
import static org.elasticsearch.script.mustache.CustomMustacheFactory.JSON_MIME_TYPE;
32-
import static org.elasticsearch.script.mustache.CustomMustacheFactory.PLAIN_TEXT_MIME_TYPE;
33-
import static org.elasticsearch.script.mustache.CustomMustacheFactory.X_WWW_FORM_URLENCODED_MIME_TYPE;
31+
import static org.elasticsearch.script.mustache.CustomMustacheFactory.JSON_MEDIA_TYPE;
32+
import static org.elasticsearch.script.mustache.CustomMustacheFactory.PLAIN_TEXT_MEDIA_TYPE;
33+
import static org.elasticsearch.script.mustache.CustomMustacheFactory.X_WWW_FORM_URLENCODED_MEDIA_TYPE;
3434
import static org.hamcrest.Matchers.equalTo;
3535
import static org.hamcrest.Matchers.instanceOf;
3636

@@ -40,33 +40,33 @@ public void testCreateEncoder() {
4040
{
4141
final IllegalArgumentException e =
4242
expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder("non-existent"));
43-
assertThat(e.getMessage(), equalTo("No encoder found for MIME type [non-existent]"));
43+
assertThat(e.getMessage(), equalTo("No encoder found for media type [non-existent]"));
4444
}
4545

4646
{
4747
final IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder(""));
48-
assertThat(e.getMessage(), equalTo("No encoder found for MIME type []"));
48+
assertThat(e.getMessage(), equalTo("No encoder found for media type []"));
4949
}
5050

5151
{
5252
final IllegalArgumentException e =
5353
expectThrows(IllegalArgumentException.class, () -> CustomMustacheFactory.createEncoder("test"));
54-
assertThat(e.getMessage(), equalTo("No encoder found for MIME type [test]"));
54+
assertThat(e.getMessage(), equalTo("No encoder found for media type [test]"));
5555
}
5656

57-
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MIME_TYPE_WITH_CHARSET),
57+
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MEDIA_TYPE_WITH_CHARSET),
5858
instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class));
59-
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MIME_TYPE),
59+
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.JSON_MEDIA_TYPE),
6060
instanceOf(CustomMustacheFactory.JsonEscapeEncoder.class));
61-
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.PLAIN_TEXT_MIME_TYPE),
61+
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.PLAIN_TEXT_MEDIA_TYPE),
6262
instanceOf(CustomMustacheFactory.DefaultEncoder.class));
63-
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.X_WWW_FORM_URLENCODED_MIME_TYPE),
63+
assertThat(CustomMustacheFactory.createEncoder(CustomMustacheFactory.X_WWW_FORM_URLENCODED_MEDIA_TYPE),
6464
instanceOf(CustomMustacheFactory.UrlEncoder.class));
6565
}
6666

6767
public void testJsonEscapeEncoder() {
6868
final ScriptEngine engine = new MustacheScriptEngine();
69-
final Map<String, String> params = randomBoolean() ? singletonMap(Script.CONTENT_TYPE_OPTION, JSON_MIME_TYPE) : emptyMap();
69+
final Map<String, String> params = randomBoolean() ? singletonMap(Script.CONTENT_TYPE_OPTION, JSON_MEDIA_TYPE) : emptyMap();
7070

7171
TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);
7272

@@ -76,7 +76,7 @@ public void testJsonEscapeEncoder() {
7676

7777
public void testDefaultEncoder() {
7878
final ScriptEngine engine = new MustacheScriptEngine();
79-
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, PLAIN_TEXT_MIME_TYPE);
79+
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, PLAIN_TEXT_MEDIA_TYPE);
8080

8181
TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);
8282

@@ -86,7 +86,7 @@ public void testDefaultEncoder() {
8686

8787
public void testUrlEncoder() {
8888
final ScriptEngine engine = new MustacheScriptEngine();
89-
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, X_WWW_FORM_URLENCODED_MIME_TYPE);
89+
final Map<String, String> params = singletonMap(Script.CONTENT_TYPE_OPTION, X_WWW_FORM_URLENCODED_MEDIA_TYPE);
9090

9191
TemplateScript.Factory compiled = engine.compile(null, "{\"field\": \"{{value}}\"}", TemplateScript.CONTEXT, params);
9292

qa/rolling-upgrade/src/test/resources/rest-api-spec/test/mixed_cluster/10_basic.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
---
22
"Verify that we can still find things with the template":
3-
- skip:
4-
version: "all"
5-
reason: "Awaits fix: https://github.com/elastic/elasticsearch/issues/66986"
63

74
- do:
85
search_template:

qa/rolling-upgrade/src/test/resources/rest-api-spec/test/upgraded_cluster/10_basic.yml

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
11
---
22
"Verify that we can still find things with the template":
3-
- skip:
4-
version: "all"
5-
reason: "Awaits fix: https://github.com/elastic/elasticsearch/issues/66986"
63

74
- do:
85
search_template:

0 commit comments

Comments
 (0)