Skip to content

Commit 6180cf5

Browse files
authored
[7.x] Accept more ingest simulate params as ints or strings (#66197, #69238) (#69262)
1 parent 1e15fe5 commit 6180cf5

File tree

3 files changed

+57
-9
lines changed

3 files changed

+57
-9
lines changed

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

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,13 @@ private static List<IngestDocument> parseDocs(Map<String, Object> config) {
184184
dataMap, Metadata.ROUTING.getFieldName());
185185
Long version = null;
186186
if (dataMap.containsKey(Metadata.VERSION.getFieldName())) {
187-
version = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.VERSION.getFieldName());
187+
String versionValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null,
188+
dataMap, Metadata.VERSION.getFieldName());
189+
if (versionValue != null) {
190+
version = Long.valueOf(versionValue);
191+
} else {
192+
throw new IllegalArgumentException("[_version] cannot be null");
193+
}
188194
}
189195
VersionType versionType = null;
190196
if (dataMap.containsKey(Metadata.VERSION_TYPE.getFieldName())) {
@@ -194,12 +200,24 @@ private static List<IngestDocument> parseDocs(Map<String, Object> config) {
194200
IngestDocument ingestDocument =
195201
new IngestDocument(index, type, id, routing, version, versionType, document);
196202
if (dataMap.containsKey(Metadata.IF_SEQ_NO.getFieldName())) {
197-
Long ifSeqNo = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.IF_SEQ_NO.getFieldName());
203+
String ifSeqNoValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null,
204+
dataMap, Metadata.IF_SEQ_NO.getFieldName());
205+
if (ifSeqNoValue != null) {
206+
Long ifSeqNo = Long.valueOf(ifSeqNoValue);
198207
ingestDocument.setFieldValue(Metadata.IF_SEQ_NO.getFieldName(), ifSeqNo);
208+
} else {
209+
throw new IllegalArgumentException("[_if_seq_no] cannot be null");
210+
}
199211
}
200212
if (dataMap.containsKey(Metadata.IF_PRIMARY_TERM.getFieldName())) {
201-
Long ifPrimaryTerm = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.IF_PRIMARY_TERM.getFieldName());
213+
String ifPrimaryTermValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null,
214+
dataMap, Metadata.IF_PRIMARY_TERM.getFieldName());
215+
if (ifPrimaryTermValue != null) {
216+
Long ifPrimaryTerm = Long.valueOf(ifPrimaryTermValue);
202217
ingestDocument.setFieldValue(Metadata.IF_PRIMARY_TERM.getFieldName(), ifPrimaryTerm);
218+
} else {
219+
throw new IllegalArgumentException("[_if_primary_term] cannot be null");
220+
}
203221
}
204222
ingestDocumentList.add(ingestDocument);
205223
}

server/src/main/java/org/elasticsearch/ingest/ConfigurationUtils.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,34 @@ public static String readOptionalStringOrIntProperty(String processorType, Strin
139139
return readStringOrInt(processorType, processorTag, propertyName, value);
140140
}
141141

142+
private static String readStringOrLong(String processorType, String processorTag,
143+
String propertyName, Object value) {
144+
if (value == null) {
145+
return null;
146+
}
147+
if (value instanceof String) {
148+
return (String) value;
149+
} else if (value instanceof Long || value instanceof Integer) {
150+
return String.valueOf(value);
151+
}
152+
throw newConfigurationException(processorType, processorTag, propertyName,
153+
"property isn't a string or long, but of type [" + value.getClass().getName() + "]");
154+
}
155+
156+
/**
157+
* Returns and removes the specified property from the specified configuration map.
158+
*
159+
* If the property value isn't of type string or long a {@link ElasticsearchParseException} is thrown.
160+
*/
161+
public static String readOptionalStringOrLongProperty(String processorType, String processorTag,
162+
Map<String, Object> configuration, String propertyName) {
163+
Object value = configuration.remove(propertyName);
164+
if (value == null) {
165+
return null;
166+
}
167+
return readStringOrLong(processorType, processorTag, propertyName, value);
168+
}
169+
142170
public static Boolean readBooleanProperty(String processorType, String processorTag, Map<String, Object> configuration,
143171
String propertyName, boolean defaultValue) {
144172
Object value = configuration.remove(propertyName);

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

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -145,19 +145,21 @@ private void innerTestParseWithProvidedPipeline(boolean useExplicitType) throws
145145
IF_PRIMARY_TERM);
146146
for(IngestDocument.Metadata field : fields) {
147147
if (field == VERSION) {
148-
Long value = randomLong();
149-
doc.put(field.getFieldName(), value);
150-
expectedDoc.put(field.getFieldName(), value);
148+
Object value = randomBoolean() ? randomLong() : randomInt();
149+
doc.put(field.getFieldName(), randomBoolean() ? value : value.toString());
150+
long longValue = (long) value;
151+
expectedDoc.put(field.getFieldName(), longValue);
151152
} else if (field == VERSION_TYPE) {
152153
String value = VersionType.toString(
153154
randomFrom(VersionType.INTERNAL, VersionType.EXTERNAL, VersionType.EXTERNAL_GTE)
154155
);
155156
doc.put(field.getFieldName(), value);
156157
expectedDoc.put(field.getFieldName(), value);
157158
} else if (field == IF_SEQ_NO || field == IF_PRIMARY_TERM) {
158-
Long value = randomNonNegativeLong();
159-
doc.put(field.getFieldName(), value);
160-
expectedDoc.put(field.getFieldName(), value);
159+
Object value = randomBoolean() ? randomNonNegativeLong() : randomInt(1000);
160+
doc.put(field.getFieldName(), randomBoolean() ? value : value.toString());
161+
long longValue = (long) value;
162+
expectedDoc.put(field.getFieldName(), longValue);
161163
} else if (field == TYPE) {
162164
if (useExplicitType) {
163165
String value = randomAlphaOfLengthBetween(1, 10);

0 commit comments

Comments
 (0)