From 85dfd700ab8988a59a1423cc94a39814ef82935d Mon Sep 17 00:00:00 2001 From: Mark Payne Date: Mon, 22 May 2017 09:45:35 -0400 Subject: [PATCH] NIFI-3951: Fixed bug that calculated the index incorrectly when filtering for ArrayIndexPath --- .../org/apache/nifi/record/path/paths/ArrayIndexPath.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/paths/ArrayIndexPath.java b/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/paths/ArrayIndexPath.java index 287ae2d3bc94..3e8186810b02 100644 --- a/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/paths/ArrayIndexPath.java +++ b/nifi-commons/nifi-record-path/src/main/java/org/apache/nifi/record/path/paths/ArrayIndexPath.java @@ -42,16 +42,20 @@ public Stream evaluate(final RecordPathEvaluationContext context) { return parentResult .filter(Filters.fieldTypeFilter(RecordFieldType.ARRAY)) - .filter(fieldValue -> fieldValue.getValue() != null && ((Object[]) fieldValue.getValue()).length >= Math.abs(index) - 1) + .filter(fieldValue -> fieldValue.getValue() != null && ((Object[]) fieldValue.getValue()).length > getArrayIndex(((Object[]) fieldValue.getValue()).length)) .map(fieldValue -> { final ArrayDataType arrayDataType = (ArrayDataType) fieldValue.getField().getDataType(); final DataType elementDataType = arrayDataType.getElementType(); final RecordField arrayField = new RecordField(fieldValue.getField().getFieldName(), elementDataType); final Object[] values = (Object[]) fieldValue.getValue(); - final int arrayIndex = index < 0 ? values.length + index : index; + final int arrayIndex = getArrayIndex(values.length); final RecordField elementField = new RecordField(arrayField.getFieldName() + "[" + arrayIndex + "]", elementDataType); final FieldValue result = new ArrayIndexFieldValue(values[arrayIndex], elementField, fieldValue, arrayIndex); return result; }); } + + private int getArrayIndex(final int arrayLength) { + return index < 0 ? arrayLength + index : index; + } }