Skip to content

Commit 7ba94d8

Browse files
authored
Fix ingest pipeline _simulate api with empty docs never returns a res… (#52937)
1 parent 5494675 commit 7ba94d8

File tree

3 files changed

+46
-1
lines changed

3 files changed

+46
-1
lines changed

server/src/main/java/org/elasticsearch/action/ingest/SimulateExecutionService.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,13 @@ public void execute(SimulatePipelineRequest.Parsed request, ActionListener<Simul
6969
final AtomicInteger counter = new AtomicInteger();
7070
final List<SimulateDocumentResult> responses =
7171
new CopyOnWriteArrayList<>(new SimulateDocumentBaseResult[request.getDocuments().size()]);
72+
73+
if (request.getDocuments().isEmpty()) {
74+
l.onResponse(new SimulatePipelineResponse(request.getPipeline().getId(),
75+
request.isVerbose(), responses));
76+
return;
77+
}
78+
7279
int iter = 0;
7380
for (IngestDocument ingestDocument : request.getDocuments()) {
7481
final int index = iter;

server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,8 +173,15 @@ static Parsed parse(Map<String, Object> config, boolean verbose, IngestService i
173173
private static List<IngestDocument> parseDocs(Map<String, Object> config) {
174174
List<Map<String, Object>> docs =
175175
ConfigurationUtils.readList(null, null, config, Fields.DOCS);
176+
if (docs.isEmpty()) {
177+
throw new IllegalArgumentException("must specify at least one document in [docs]");
178+
}
176179
List<IngestDocument> ingestDocumentList = new ArrayList<>();
177-
for (Map<String, Object> dataMap : docs) {
180+
for (Object object : docs) {
181+
if ((object instanceof Map) == false) {
182+
throw new IllegalArgumentException("malformed [docs] section, should include an inner object");
183+
}
184+
Map<String, Object> dataMap = (Map<String, Object>) object;
178185
Map<String, Object> document = ConfigurationUtils.readMap(null, null,
179186
dataMap, Fields.SOURCE);
180187
String index = ConfigurationUtils.readStringOrIntProperty(null, null,

server/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestParsingTests.java

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.elasticsearch.action.ingest;
2121

22+
import org.elasticsearch.ElasticsearchParseException;
2223
import org.elasticsearch.index.VersionType;
2324
import org.elasticsearch.ingest.CompoundProcessor;
2425
import org.elasticsearch.ingest.IngestDocument;
@@ -45,6 +46,7 @@
4546
import static org.elasticsearch.ingest.IngestDocument.MetaData.ROUTING;
4647
import static org.elasticsearch.ingest.IngestDocument.MetaData.VERSION;
4748
import static org.elasticsearch.ingest.IngestDocument.MetaData.VERSION_TYPE;
49+
import static org.hamcrest.Matchers.containsString;
4850
import static org.hamcrest.Matchers.equalTo;
4951
import static org.hamcrest.Matchers.nullValue;
5052
import static org.mockito.Mockito.mock;
@@ -216,4 +218,33 @@ public void testNonExistentPipelineId() {
216218
() -> SimulatePipelineRequest.parseWithPipelineId(pipelineId, requestContent, false, ingestService));
217219
assertThat(e.getMessage(), equalTo("pipeline [" + pipelineId + "] does not exist"));
218220
}
221+
222+
public void testNotValidDocs() {
223+
Map<String, Object> requestContent = new HashMap<>();
224+
List<Map<String, Object>> docs = new ArrayList<>();
225+
Map<String, Object> pipelineConfig = new HashMap<>();
226+
List<Map<String, Object>> processors = new ArrayList<>();
227+
pipelineConfig.put("processors", processors);
228+
requestContent.put(Fields.DOCS, docs);
229+
requestContent.put(Fields.PIPELINE, pipelineConfig);
230+
Exception e1 = expectThrows(IllegalArgumentException.class,
231+
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
232+
assertThat(e1.getMessage(), equalTo("must specify at least one document in [docs]"));
233+
234+
List<String> stringList = new ArrayList<>();
235+
stringList.add("test");
236+
pipelineConfig.put("processors", processors);
237+
requestContent.put(Fields.DOCS, stringList);
238+
requestContent.put(Fields.PIPELINE, pipelineConfig);
239+
Exception e2 = expectThrows(IllegalArgumentException.class,
240+
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
241+
assertThat(e2.getMessage(), equalTo("malformed [docs] section, should include an inner object"));
242+
243+
docs.add(new HashMap<>());
244+
requestContent.put(Fields.DOCS, docs);
245+
requestContent.put(Fields.PIPELINE, pipelineConfig);
246+
Exception e3 = expectThrows(ElasticsearchParseException.class,
247+
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
248+
assertThat(e3.getMessage(), containsString("required property is missing"));
249+
}
219250
}

0 commit comments

Comments
 (0)