NIFI-7844 - substring should return empty instead of throwing IndexOutOfBoundsException

This closes #4553

Signed-off-by: Mike Thomsen <mthomsen@apache.org>
This commit is contained in:
Pierre Villard 2020-09-24 12:34:30 +02:00 committed by Mike Thomsen
parent 6990f0d3a9
commit f32405ed16
No known key found for this signature in database
GPG Key ID: 88511C3D4CAD246F
3 changed files with 22 additions and 7 deletions

View File

@ -46,12 +46,16 @@ public class SubstringEvaluator extends StringEvaluator {
if (subjectValue == null) {
return new StringQueryResult("");
}
final int startIndexValue = startIndex.evaluate(evaluationContext).getValue().intValue();
if (endIndex == null) {
return new StringQueryResult(subjectValue.substring(startIndexValue));
} else {
final int endIndexValue = endIndex.evaluate(evaluationContext).getValue().intValue();
return new StringQueryResult(subjectValue.substring(startIndexValue, endIndexValue));
try {
final int startIndexValue = startIndex.evaluate(evaluationContext).getValue().intValue();
if (endIndex == null) {
return new StringQueryResult(subjectValue.substring(startIndexValue));
} else {
final int endIndexValue = endIndex.evaluate(evaluationContext).getValue().intValue();
return new StringQueryResult(subjectValue.substring(startIndexValue, endIndexValue));
}
} catch (IndexOutOfBoundsException e) {
return new StringQueryResult("");
}
}

View File

@ -1636,6 +1636,17 @@ public class TestQuery {
verifyEquals("${filename:substring(4)}", attributes, "-255");
}
@Test
public void testSubstringOOB() {
final Map<String, String> attributes = new HashMap<>();
attributes.put("filename", "file-255");
verifyEquals("${filename:substring(10, 20)}", attributes, "");
verifyEquals("${filename:substring(10)}", attributes, "");
verifyEquals("${filename:substring(-2)}", attributes, "");
verifyEquals("${filename:substring(2, -2)}", attributes, "");
}
@Test
public void testToRadix() {
final Map<String, String> attributes = new HashMap<>();

View File

@ -652,7 +652,7 @@ Each of the following functions manipulates a String in some way.
[.description]#If the _starting index_ is larger than the _ending index_, this function call will result in an error.#
[.description]#If the _starting index_ or the _ending index_ is greater than the length of the Subject or has a value
less than 0, this function call will result in an error.#
less than 0, this function call will return an empty string.#
*Subject Type*: [.subject]#String#