Skip to content

Commit

Permalink
NIFI-12704 Avoid NPE in escapeJson() for Root Path
Browse files Browse the repository at this point in the history
This closes apache#8450

Signed-off-by: David Handermann <exceptionfactory@apache.org>
  • Loading branch information
EndzeitBegins authored and exceptionfactory committed Jun 17, 2024
1 parent bd1ad8f commit a52d6a8
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,10 @@ public static RecordPathSegment buildPath(final Tree tree, final RecordPathSegme
return new RootPath();
}
case CHILD_REFERENCE: {
if (tree.getChildCount() == 0) {
return new RootPath();
}

final Tree childTree = tree.getChild(0);
if (childTree == null) {
return new RootPath();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2287,6 +2287,17 @@ public void testCountAsFilter() {
assertEquals(48, fieldValues.get(0).getValue());
}

@Test
public void testRecordRootReferenceInFunction() {
final Record record = createSimpleRecord();

final FieldValue singleArgumentFieldValue = evaluateSingleFieldValue("escapeJson(/)", record);
assertEquals("{\"id\":48,\"name\":\"John Doe\",\"missing\":null}", singleArgumentFieldValue.getValue());
final FieldValue multipleArgumentsFieldValue = evaluateSingleFieldValue("mapOf(\"copy\",/)", record);
assertInstanceOf(MapRecord.class, multipleArgumentsFieldValue.getValue());
assertEquals(record.toString(), ((MapRecord) multipleArgumentsFieldValue.getValue()).getValue("copy"));
}

private List<RecordField> getDefaultFields() {
final List<RecordField> fields = new ArrayList<>();
fields.add(new RecordField("id", RecordFieldType.INT.getDataType()));
Expand Down Expand Up @@ -2339,4 +2350,11 @@ private Record createSimpleRecord() {
return new MapRecord(schema, values);
}

private static FieldValue evaluateSingleFieldValue(RecordPath recordPath, Record record) {
return recordPath.evaluate(record).getSelectedFields().findFirst().get();
}

private static FieldValue evaluateSingleFieldValue(String path, Record record) {
return evaluateSingleFieldValue(RecordPath.compile(path), record);
}
}

0 comments on commit a52d6a8

Please sign in to comment.