This is an automated email from the ASF dual-hosted git repository.

exceptionfactory pushed a commit to branch support/nifi-1.x
in repository https://gitbox.apache.org/repos/asf/nifi.git


The following commit(s) were added to refs/heads/support/nifi-1.x by this push:
     new 88c026d731 NIFI-13335 Added ability for the XMLRecordReader to handle 
where an array of data has different types. (#9358)
88c026d731 is described below

commit 88c026d73143376b6e044ba7f926f09f3a08e131
Author: dan-s1 <[email protected]>
AuthorDate: Thu Oct 10 09:14:59 2024 -0400

    NIFI-13335 Added ability for the XMLRecordReader to handle where an array 
of data has different types. (#9358)
    
    Signed-off-by: David Handermann <[email protected]>
---
 .../nifi-record-serialization-services/pom.xml     |  1 +
 .../java/org/apache/nifi/xml/XMLRecordReader.java  |  1 +
 .../java/org/apache/nifi/xml/TestXMLReader.java    | 26 ++++++++++++++++++++++
 .../apache/nifi/xml/TestXMLReaderProcessor.java    | 18 +++++++++++++--
 .../xml/dataWithArrayOfDifferentTypes.xml          |  5 +++++
 5 files changed, 49 insertions(+), 2 deletions(-)

diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/pom.xml
 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/pom.xml
index 89c251c21a..87f032285a 100755
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/pom.xml
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/pom.xml
@@ -245,6 +245,7 @@
 
                         <exclude>src/test/resources/text/testschema</exclude>
 
+                        
<exclude>src/test/resources/xml/dataWithArrayOfDifferentTypes.xml</exclude>
                         
<exclude>src/test/resources/xml/field_with_sub-element.xml</exclude>
                         <exclude>src/test/resources/xml/people.xml</exclude>
                         <exclude>src/test/resources/xml/people2.xml</exclude>
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/xml/XMLRecordReader.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/xml/XMLRecordReader.java
index 8ecb4a2f3c..7c7b691958 100644
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/xml/XMLRecordReader.java
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/xml/XMLRecordReader.java
@@ -547,6 +547,7 @@ public class XMLRecordReader implements RecordReader {
             case BOOLEAN:
             case BYTE:
             case CHAR:
+            case CHOICE:
             case DECIMAL:
             case DOUBLE:
             case FLOAT:
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReader.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReader.java
index 93d855ff74..dcbb05c232 100644
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReader.java
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReader.java
@@ -46,11 +46,18 @@ public class TestXMLReader {
     private final String EVALUATE_IS_ARRAY = "xml.stream.is.array";
 
     private TestRunner setup(Map<PropertyDescriptor, String> 
xmlReaderProperties) throws InitializationException {
+        return setup(xmlReaderProperties, null);
+    }
+
+    private TestRunner setup(Map<PropertyDescriptor, String> 
xmlReaderProperties, String recordFieldNameToGetAsString) throws 
InitializationException {
         TestRunner runner = 
TestRunners.newTestRunner(TestXMLReaderProcessor.class);
         XMLReader reader = new XMLReader();
 
         runner.addControllerService("xml_reader", reader);
         runner.setProperty(TestXMLReaderProcessor.XML_READER, "xml_reader");
+        if (recordFieldNameToGetAsString != null) {
+            
runner.setProperty(TestXMLReaderProcessor.RECORD_FIELD_TO_GET_AS_STRING, 
recordFieldNameToGetAsString);
+        }
 
         for (Map.Entry<PropertyDescriptor, String> entry : 
xmlReaderProperties.entrySet()) {
             runner.setProperty(reader, entry.getKey(), entry.getValue());
@@ -298,4 +305,23 @@ public class TestXMLReader {
         String actualContent = out.getContent();
         assertEquals(expectedContent, actualContent);
     }
+
+    @Test
+    void testInferSchemaWhereNameValuesHasMixedTypes() throws Exception {
+        final Map<PropertyDescriptor, String> xmlReaderProperties = new 
HashMap<>();
+        xmlReaderProperties.put(SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, 
SchemaInferenceUtil.INFER_SCHEMA.getValue());
+        xmlReaderProperties.put(XMLReader.RECORD_FORMAT, 
XMLReader.RECORD_SINGLE.getValue());
+        xmlReaderProperties.put(XMLReader.PARSE_XML_ATTRIBUTES, "true");
+        xmlReaderProperties.put(XMLReader.CONTENT_FIELD_NAME, "Value");
+        TestRunner runner = setup(xmlReaderProperties, "Data");
+
+        final InputStream is = new 
FileInputStream("src/test/resources/xml/dataWithArrayOfDifferentTypes.xml");
+        runner.enqueue(is);
+        runner.run();
+
+        final MockFlowFile out = 
runner.getFlowFilesForRelationship(TestXMLReaderProcessor.SUCCESS).get(0);
+        final String expectedContent = "[MapRecord[{Name=Param1, 
Value=String1}], MapRecord[{Name=Param2, Value=2}], MapRecord[{Name=Param3, 
Value=String3}]]";
+        final String actualContent = out.getContent();
+        assertEquals(expectedContent, actualContent);
+    }
 }
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReaderProcessor.java
 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReaderProcessor.java
index b4ee4ab7cf..9f8e3b7440 100644
--- 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReaderProcessor.java
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReaderProcessor.java
@@ -24,6 +24,7 @@ import org.apache.nifi.processor.ProcessContext;
 import org.apache.nifi.processor.ProcessSession;
 import org.apache.nifi.processor.Relationship;
 import org.apache.nifi.processor.exception.ProcessException;
+import org.apache.nifi.processor.util.StandardValidators;
 import org.apache.nifi.serialization.RecordReader;
 import org.apache.nifi.serialization.RecordReaderFactory;
 import org.apache.nifi.serialization.record.Record;
@@ -31,6 +32,7 @@ import org.apache.nifi.util.StringUtils;
 
 import java.io.InputStream;
 import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.HashSet;
 import java.util.List;
 import java.util.Set;
@@ -44,12 +46,20 @@ public class TestXMLReaderProcessor extends 
AbstractProcessor {
             .required(true)
             .build();
 
+    static final PropertyDescriptor RECORD_FIELD_TO_GET_AS_STRING = new 
PropertyDescriptor.Builder()
+            .name("record_field_to_get_as_string")
+            .description("record_field_to_get_as_string")
+            .addValidator(StandardValidators.NON_BLANK_VALIDATOR)
+            .required(false)
+            .build();
+
     public static final Relationship SUCCESS = new 
Relationship.Builder().name("success").description("success").build();
 
     @Override
     public void onTrigger(ProcessContext context, ProcessSession session) 
throws ProcessException {
         FlowFile flowFile = session.get();
         final RecordReaderFactory readerFactory = 
context.getProperty(XML_READER).asControllerService(RecordReaderFactory.class);
+        final String recordFieldAsString = 
context.getProperty(RECORD_FIELD_TO_GET_AS_STRING).getValue();
 
         final List<String> records = new ArrayList<>();
 
@@ -57,7 +67,11 @@ public class TestXMLReaderProcessor extends 
AbstractProcessor {
              final RecordReader reader = 
readerFactory.createRecordReader(flowFile, in, getLogger())) {
             Record record;
             while ((record = reader.nextRecord()) != null) {
-                records.add(record.toString());
+                if (recordFieldAsString == null) {
+                    records.add(record.toString());
+                } else {
+                    records.add(record.getAsString(recordFieldAsString));
+                }
             }
         } catch (Exception e) {
             e.printStackTrace();
@@ -69,7 +83,7 @@ public class TestXMLReaderProcessor extends AbstractProcessor 
{
 
     @Override
     protected List<PropertyDescriptor> getSupportedPropertyDescriptors() {
-        return new ArrayList<PropertyDescriptor>() {{ add(XML_READER); }};
+        return Arrays.asList(XML_READER, RECORD_FIELD_TO_GET_AS_STRING);
     }
 
     @Override
diff --git 
a/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/resources/xml/dataWithArrayOfDifferentTypes.xml
 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/resources/xml/dataWithArrayOfDifferentTypes.xml
new file mode 100644
index 0000000000..1bd2b218cd
--- /dev/null
+++ 
b/nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/resources/xml/dataWithArrayOfDifferentTypes.xml
@@ -0,0 +1,5 @@
+<UserData>
+    <Data Name="Param1">String1</Data>
+    <Data Name="Param2">2</Data>
+    <Data Name="Param3">String3</Data>
+</UserData>
\ No newline at end of file

Reply via email to