mirror of https://github.com/apache/nifi.git
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:
parent
6990f0d3a9
commit
f32405ed16
|
@ -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("");
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -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<>();
|
||||
|
|
|
@ -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#
|
||||
|
|
Loading…
Reference in New Issue