From 6180cf5c03384a734a6dc945ac318406b468b8f3 Mon Sep 17 00:00:00 2001 From: Przemko Robakowski Date: Fri, 19 Feb 2021 14:30:18 +0100 Subject: [PATCH] [7.x] Accept more ingest simulate params as ints or strings (#66197, #69238) (#69262) --- .../ingest/SimulatePipelineRequest.java | 24 ++++++++++++++-- .../ingest/ConfigurationUtils.java | 28 +++++++++++++++++++ .../SimulatePipelineRequestParsingTests.java | 14 ++++++---- 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java b/server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java index a3c781071da10..9dd2d9580d03c 100644 --- a/server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java +++ b/server/src/main/java/org/elasticsearch/action/ingest/SimulatePipelineRequest.java @@ -184,7 +184,13 @@ private static List parseDocs(Map config) { dataMap, Metadata.ROUTING.getFieldName()); Long version = null; if (dataMap.containsKey(Metadata.VERSION.getFieldName())) { - version = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.VERSION.getFieldName()); + String versionValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null, + dataMap, Metadata.VERSION.getFieldName()); + if (versionValue != null) { + version = Long.valueOf(versionValue); + } else { + throw new IllegalArgumentException("[_version] cannot be null"); + } } VersionType versionType = null; if (dataMap.containsKey(Metadata.VERSION_TYPE.getFieldName())) { @@ -194,12 +200,24 @@ private static List parseDocs(Map config) { IngestDocument ingestDocument = new IngestDocument(index, type, id, routing, version, versionType, document); if (dataMap.containsKey(Metadata.IF_SEQ_NO.getFieldName())) { - Long ifSeqNo = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.IF_SEQ_NO.getFieldName()); + String ifSeqNoValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null, + dataMap, Metadata.IF_SEQ_NO.getFieldName()); + if (ifSeqNoValue != null) { + Long ifSeqNo = Long.valueOf(ifSeqNoValue); ingestDocument.setFieldValue(Metadata.IF_SEQ_NO.getFieldName(), ifSeqNo); + } else { + throw new IllegalArgumentException("[_if_seq_no] cannot be null"); + } } if (dataMap.containsKey(Metadata.IF_PRIMARY_TERM.getFieldName())) { - Long ifPrimaryTerm = (Long) ConfigurationUtils.readObject(null, null, dataMap, Metadata.IF_PRIMARY_TERM.getFieldName()); + String ifPrimaryTermValue = ConfigurationUtils.readOptionalStringOrLongProperty(null, null, + dataMap, Metadata.IF_PRIMARY_TERM.getFieldName()); + if (ifPrimaryTermValue != null) { + Long ifPrimaryTerm = Long.valueOf(ifPrimaryTermValue); ingestDocument.setFieldValue(Metadata.IF_PRIMARY_TERM.getFieldName(), ifPrimaryTerm); + } else { + throw new IllegalArgumentException("[_if_primary_term] cannot be null"); + } } ingestDocumentList.add(ingestDocument); } diff --git a/server/src/main/java/org/elasticsearch/ingest/ConfigurationUtils.java b/server/src/main/java/org/elasticsearch/ingest/ConfigurationUtils.java index 2f4c8a42e807e..95e9f21d517eb 100644 --- a/server/src/main/java/org/elasticsearch/ingest/ConfigurationUtils.java +++ b/server/src/main/java/org/elasticsearch/ingest/ConfigurationUtils.java @@ -139,6 +139,34 @@ public static String readOptionalStringOrIntProperty(String processorType, Strin return readStringOrInt(processorType, processorTag, propertyName, value); } + private static String readStringOrLong(String processorType, String processorTag, + String propertyName, Object value) { + if (value == null) { + return null; + } + if (value instanceof String) { + return (String) value; + } else if (value instanceof Long || value instanceof Integer) { + return String.valueOf(value); + } + throw newConfigurationException(processorType, processorTag, propertyName, + "property isn't a string or long, but of type [" + value.getClass().getName() + "]"); + } + + /** + * Returns and removes the specified property from the specified configuration map. + * + * If the property value isn't of type string or long a {@link ElasticsearchParseException} is thrown. + */ + public static String readOptionalStringOrLongProperty(String processorType, String processorTag, + Map configuration, String propertyName) { + Object value = configuration.remove(propertyName); + if (value == null) { + return null; + } + return readStringOrLong(processorType, processorTag, propertyName, value); + } + public static Boolean readBooleanProperty(String processorType, String processorTag, Map configuration, String propertyName, boolean defaultValue) { Object value = configuration.remove(propertyName); diff --git a/server/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestParsingTests.java b/server/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestParsingTests.java index eb959d4dc16e4..2e382f33b57d7 100644 --- a/server/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestParsingTests.java +++ b/server/src/test/java/org/elasticsearch/action/ingest/SimulatePipelineRequestParsingTests.java @@ -145,9 +145,10 @@ private void innerTestParseWithProvidedPipeline(boolean useExplicitType) throws IF_PRIMARY_TERM); for(IngestDocument.Metadata field : fields) { if (field == VERSION) { - Long value = randomLong(); - doc.put(field.getFieldName(), value); - expectedDoc.put(field.getFieldName(), value); + Object value = randomBoolean() ? randomLong() : randomInt(); + doc.put(field.getFieldName(), randomBoolean() ? value : value.toString()); + long longValue = (long) value; + expectedDoc.put(field.getFieldName(), longValue); } else if (field == VERSION_TYPE) { String value = VersionType.toString( randomFrom(VersionType.INTERNAL, VersionType.EXTERNAL, VersionType.EXTERNAL_GTE) @@ -155,9 +156,10 @@ private void innerTestParseWithProvidedPipeline(boolean useExplicitType) throws doc.put(field.getFieldName(), value); expectedDoc.put(field.getFieldName(), value); } else if (field == IF_SEQ_NO || field == IF_PRIMARY_TERM) { - Long value = randomNonNegativeLong(); - doc.put(field.getFieldName(), value); - expectedDoc.put(field.getFieldName(), value); + Object value = randomBoolean() ? randomNonNegativeLong() : randomInt(1000); + doc.put(field.getFieldName(), randomBoolean() ? value : value.toString()); + long longValue = (long) value; + expectedDoc.put(field.getFieldName(), longValue); } else if (field == TYPE) { if (useExplicitType) { String value = randomAlphaOfLengthBetween(1, 10);