NIFI-12704 Avoid NPE in escapeJson() for Root Path

This closes #8450

Signed-off-by: David Handermann <exceptionfactory@apache.org>
This commit is contained in:
EndzeitBegins 2024-02-25 09:19:58 +01:00 committed by exceptionfactory
parent bd1ad8f9f4
commit a52d6a8214
No known key found for this signature in database
2 changed files with 22 additions and 0 deletions

View File

@ -130,6 +130,10 @@ public class RecordPathCompiler {
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();

View File

@ -2287,6 +2287,17 @@ public class TestRecordPath {
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()));
@ -2339,4 +2350,11 @@ public class TestRecordPath {
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);
}
}