Skip to content

Commit 3c954b5

Browse files
authored
Fix ingest pipeline _simulate api with empty docs never returns a res… (#52937) (#53436)
1 parent 5496609 commit 3c954b5

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;
@@ -46,6 +47,7 @@
4647
import static org.elasticsearch.ingest.IngestDocument.MetaData.TYPE;
4748
import static org.elasticsearch.ingest.IngestDocument.MetaData.VERSION;
4849
import static org.elasticsearch.ingest.IngestDocument.MetaData.VERSION_TYPE;
50+
import static org.hamcrest.Matchers.containsString;
4951
import static org.hamcrest.Matchers.equalTo;
5052
import static org.hamcrest.Matchers.nullValue;
5153
import static org.mockito.Mockito.mock;
@@ -257,4 +259,33 @@ public void testNonExistentPipelineId() {
257259
() -> SimulatePipelineRequest.parseWithPipelineId(pipelineId, requestContent, false, ingestService));
258260
assertThat(e.getMessage(), equalTo("pipeline [" + pipelineId + "] does not exist"));
259261
}
262+
263+
public void testNotValidDocs() {
264+
Map<String, Object> requestContent = new HashMap<>();
265+
List<Map<String, Object>> docs = new ArrayList<>();
266+
Map<String, Object> pipelineConfig = new HashMap<>();
267+
List<Map<String, Object>> processors = new ArrayList<>();
268+
pipelineConfig.put("processors", processors);
269+
requestContent.put(Fields.DOCS, docs);
270+
requestContent.put(Fields.PIPELINE, pipelineConfig);
271+
Exception e1 = expectThrows(IllegalArgumentException.class,
272+
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
273+
assertThat(e1.getMessage(), equalTo("must specify at least one document in [docs]"));
274+
275+
List<String> stringList = new ArrayList<>();
276+
stringList.add("test");
277+
pipelineConfig.put("processors", processors);
278+
requestContent.put(Fields.DOCS, stringList);
279+
requestContent.put(Fields.PIPELINE, pipelineConfig);
280+
Exception e2 = expectThrows(IllegalArgumentException.class,
281+
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
282+
assertThat(e2.getMessage(), equalTo("malformed [docs] section, should include an inner object"));
283+
284+
docs.add(new HashMap<>());
285+
requestContent.put(Fields.DOCS, docs);
286+
requestContent.put(Fields.PIPELINE, pipelineConfig);
287+
Exception e3 = expectThrows(ElasticsearchParseException.class,
288+
() -> SimulatePipelineRequest.parse(requestContent, false, ingestService));
289+
assertThat(e3.getMessage(), containsString("required property is missing"));
290+
}
260291
}

0 commit comments

Comments
 (0)