Skip to content

Commit 49d4b26

Browse files
authored
Ingest: Support integer and long hex values in convert (#32213)
This commit adds checks for hex formatted strings in the convert processor, allowing strings like `0x1` to be parsed as integer `1`. closes #32182
1 parent be40a69 commit 49d4b26

File tree

2 files changed

+64
-2
lines changed

2 files changed

+64
-2
lines changed

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

+10-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ enum Type {
4242
@Override
4343
public Object convert(Object value) {
4444
try {
45-
return Integer.parseInt(value.toString());
45+
String strValue = value.toString();
46+
if (strValue.startsWith("0x") || strValue.startsWith("-0x")) {
47+
return Integer.decode(strValue);
48+
}
49+
return Integer.parseInt(strValue);
4650
} catch(NumberFormatException e) {
4751
throw new IllegalArgumentException("unable to convert [" + value + "] to integer", e);
4852
}
@@ -52,7 +56,11 @@ public Object convert(Object value) {
5256
@Override
5357
public Object convert(Object value) {
5458
try {
55-
return Long.parseLong(value.toString());
59+
String strValue = value.toString();
60+
if (strValue.startsWith("0x") || strValue.startsWith("-0x")) {
61+
return Long.decode(strValue);
62+
}
63+
return Long.parseLong(strValue);
5664
} catch(NumberFormatException e) {
5765
throw new IllegalArgumentException("unable to convert [" + value + "] to long", e);
5866
}

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

+54
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,33 @@ public void testConvertInt() throws Exception {
4949
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
5050
}
5151

52+
public void testConvertIntHex() throws Exception {
53+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
54+
int randomInt = randomInt();
55+
String intString = randomInt < 0 ? "-0x" + Integer.toHexString(-randomInt) : "0x" + Integer.toHexString(randomInt);
56+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, intString);
57+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
58+
processor.execute(ingestDocument);
59+
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(randomInt));
60+
}
61+
62+
public void testConvertIntLeadingZero() throws Exception {
63+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
64+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "010");
65+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
66+
processor.execute(ingestDocument);
67+
assertThat(ingestDocument.getFieldValue(fieldName, Integer.class), equalTo(10));
68+
}
69+
70+
public void testConvertIntHexError() {
71+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
72+
String value = "0x" + randomAlphaOfLengthBetween(1, 10);
73+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, value);
74+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.INTEGER, false);
75+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument));
76+
assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to integer"));
77+
}
78+
5279
public void testConvertIntList() throws Exception {
5380
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
5481
int numItems = randomIntBetween(1, 10);
@@ -92,6 +119,33 @@ public void testConvertLong() throws Exception {
92119
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
93120
}
94121

122+
public void testConvertLongHex() throws Exception {
123+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
124+
long randomLong = randomLong();
125+
String longString = randomLong < 0 ? "-0x" + Long.toHexString(-randomLong) : "0x" + Long.toHexString(randomLong);
126+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, longString);
127+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
128+
processor.execute(ingestDocument);
129+
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(randomLong));
130+
}
131+
132+
public void testConvertLongLeadingZero() throws Exception {
133+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
134+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, "010");
135+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
136+
processor.execute(ingestDocument);
137+
assertThat(ingestDocument.getFieldValue(fieldName, Long.class), equalTo(10L));
138+
}
139+
140+
public void testConvertLongHexError() {
141+
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
142+
String value = "0x" + randomAlphaOfLengthBetween(1, 10);
143+
String fieldName = RandomDocumentPicks.addRandomField(random(), ingestDocument, value);
144+
Processor processor = new ConvertProcessor(randomAlphaOfLength(10), fieldName, fieldName, Type.LONG, false);
145+
IllegalArgumentException e = expectThrows(IllegalArgumentException.class, () -> processor.execute(ingestDocument));
146+
assertThat(e.getMessage(), equalTo("unable to convert [" + value + "] to long"));
147+
}
148+
95149
public void testConvertLongList() throws Exception {
96150
IngestDocument ingestDocument = RandomDocumentPicks.randomIngestDocument(random());
97151
int numItems = randomIntBetween(1, 10);

0 commit comments

Comments
 (0)