Skip to content

Commit d397d3b

Browse files
authored
[7.x] Configurable media_type for mustache template encoding on append processor (#76386)
1 parent 793cad0 commit d397d3b

File tree

3 files changed

+39
-6
lines changed

3 files changed

+39
-6
lines changed

docs/reference/ingest/processors/append.asciidoc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,13 @@ Accepts a single value or an array of values.
1515
[options="header"]
1616
|======
1717
| Name | Required | Default | Description
18-
| `field` | yes | - | The field to be appended to. Supports <<template-snippets,template snippets>>.
19-
| `value` | yes | - | The value to be appended. Supports <<template-snippets,template snippets>>.
18+
| `field` | yes | - | The field to be appended to. Supports <<template-snippets,template snippets>>.
19+
| `value` | yes | - | The value to be appended. Supports <<template-snippets,template snippets>>.
2020
| `allow_duplicates` | no | true | If `false`, the processor does not append
2121
values already present in the field.
22+
| `media_type` | no | `application/json` | The media type for encoding `value`. Applies only when `value` is a
23+
<<template-snippets,template snippet>>. Must be one of `application/json`, `text/plain`, or
24+
`application/x-www-form-urlencoded`.
2225
include::common-options.asciidoc[]
2326
|======
2427

modules/ingest-common/src/main/java/org/elasticsearch/ingest/common/AppendProcessor.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import org.elasticsearch.ingest.IngestDocument;
1414
import org.elasticsearch.ingest.Processor;
1515
import org.elasticsearch.ingest.ValueSource;
16+
import org.elasticsearch.script.Script;
1617
import org.elasticsearch.script.ScriptService;
1718
import org.elasticsearch.script.TemplateScript;
1819

@@ -71,10 +72,15 @@ public AppendProcessor create(Map<String, Processor.Factory> registry, String pr
7172
String field = ConfigurationUtils.readStringProperty(TYPE, processorTag, config, "field");
7273
Object value = ConfigurationUtils.readObject(TYPE, processorTag, config, "value");
7374
boolean allowDuplicates = ConfigurationUtils.readBooleanProperty(TYPE, processorTag, config, "allow_duplicates", true);
74-
TemplateScript.Factory compiledTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag,
75-
"field", field, scriptService);
76-
return new AppendProcessor(processorTag, description, compiledTemplate, ValueSource.wrap(value, scriptService),
77-
allowDuplicates);
75+
TemplateScript.Factory compiledTemplate = ConfigurationUtils.compileTemplate(TYPE, processorTag, "field", field, scriptService);
76+
String mediaType = ConfigurationUtils.readMediaTypeProperty(TYPE, processorTag, config, "media_type", "application/json");
77+
return new AppendProcessor(
78+
processorTag,
79+
description,
80+
compiledTemplate,
81+
ValueSource.wrap(value, scriptService, org.elasticsearch.core.Map.of(Script.CONTENT_TYPE_OPTION, mediaType)),
82+
allowDuplicates
83+
);
7884
}
7985
}
8086
}

modules/ingest-common/src/test/java/org/elasticsearch/ingest/common/AppendProcessorFactoryTests.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import org.elasticsearch.ElasticsearchException;
1212
import org.elasticsearch.ElasticsearchParseException;
13+
import org.elasticsearch.ingest.ConfigurationUtils;
1314
import org.elasticsearch.ingest.TestTemplateService;
1415
import org.elasticsearch.test.ESTestCase;
1516
import org.junit.Before;
@@ -20,6 +21,7 @@
2021
import java.util.Map;
2122

2223
import static org.hamcrest.CoreMatchers.equalTo;
24+
import static org.hamcrest.Matchers.containsString;
2325

2426
public class AppendProcessorFactoryTests extends ESTestCase {
2527

@@ -92,4 +94,26 @@ public void testInvalidMustacheTemplate() throws Exception {
9294
assertThat(exception.getMessage(), equalTo("java.lang.RuntimeException: could not compile script"));
9395
assertThat(exception.getMetadata("es.processor_tag").get(0), equalTo(processorTag));
9496
}
97+
98+
public void testMediaType() throws Exception {
99+
// valid media type
100+
String expectedMediaType = randomFrom(ConfigurationUtils.VALID_MEDIA_TYPES);
101+
Map<String, Object> config = new HashMap<>();
102+
config.put("field", "field1");
103+
config.put("value", "value1");
104+
config.put("media_type", expectedMediaType);
105+
String processorTag = randomAlphaOfLength(10);
106+
AppendProcessor appendProcessor = factory.create(null, processorTag, null, config);
107+
assertThat(appendProcessor.getTag(), equalTo(processorTag));
108+
109+
// invalid media type
110+
expectedMediaType = randomValueOtherThanMany(m -> Arrays.asList(ConfigurationUtils.VALID_MEDIA_TYPES).contains(m),
111+
() -> randomAlphaOfLengthBetween(5, 9));
112+
final Map<String, Object> config2 = new HashMap<>();
113+
config2.put("field", "field1");
114+
config2.put("value", "value1");
115+
config2.put("media_type", expectedMediaType);
116+
ElasticsearchException e = expectThrows(ElasticsearchException.class, () -> factory.create(null, processorTag, null, config2));
117+
assertThat(e.getMessage(), containsString("property does not contain a supported media type [" + expectedMediaType + "]"));
118+
}
95119
}

0 commit comments

Comments
 (0)