You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@nifi.apache.org by GitBox <gi...@apache.org> on 2022/04/04 15:25:30 UTC

[GitHub] [nifi] simonbence commented on a diff in pull request #5896: NIFI-9832: Fix disappearing XML element content when the element has attribute

simonbence commented on code in PR #5896:
URL: https://github.com/apache/nifi/pull/5896#discussion_r841863124


##########
nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/main/java/org/apache/nifi/xml/inference/XmlRecordSource.java:
##########
@@ -132,7 +134,7 @@ private XmlNode readNext(final StartElement startElement) throws XMLStreamExcept
         } else {
             final String textContent = content.toString().trim();
             if (!textContent.equals("")) {
-                childNodes.put("value", new XmlTextNode("value", textContent));
+                childNodes.put(contentFieldName, new XmlTextNode(contentFieldName, textContent));

Review Comment:
   If "content field name" property is not set, the value is still lot. I would suggest to extend the description of the propery with information about this.



##########
nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReader.java:
##########
@@ -31,80 +33,99 @@
 import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import static junit.framework.TestCase.assertEquals;
 
 public class TestXMLReader {
 
-    private XMLReader reader;
-
     private final String ATTRIBUTE_PREFIX = "attribute_prefix";
     private final String CONTENT_NAME = "content_field";
     private final String EVALUATE_IS_ARRAY = "xml.stream.is.array";
 
-    public TestRunner setup(String filePath) throws InitializationException, IOException {
-
+    private TestRunner setup(Map<PropertyDescriptor, String> xmlReaderProperties) throws InitializationException {
         TestRunner runner = TestRunners.newTestRunner(TestXMLReaderProcessor.class);
-        reader = new XMLReader();
+        XMLReader reader = new XMLReader();
+
         runner.addControllerService("xml_reader", reader);
         runner.setProperty(TestXMLReaderProcessor.XML_READER, "xml_reader");
 
-        final String outputSchemaText = new String(Files.readAllBytes(Paths.get(filePath)));
-        runner.setProperty(reader, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
-        runner.setProperty(reader, SchemaAccessUtils.SCHEMA_TEXT, outputSchemaText);
+        for (Map.Entry<PropertyDescriptor, String> entry : xmlReaderProperties.entrySet()) {
+            runner.setProperty(reader, entry.getKey(), entry.getValue());
+        }
 
+        runner.enableControllerService(reader);
         return runner;
     }
 
     @Test
-    public void testRecordFormat() throws IOException, InitializationException {
-        TestRunner runner = setup("src/test/resources/xml/testschema");
-
-        runner.setProperty(reader, XMLReader.RECORD_FORMAT, XMLReader.RECORD_EVALUATE);
-
-        runner.enableControllerService(reader);
-
+    public void testRecordFormatDeterminedBasedOnAttribute() throws IOException, InitializationException {
+        // GIVEN
+        String outputSchemaPath = "src/test/resources/xml/testschema";
+        String outputSchemaText = new String(Files.readAllBytes(Paths.get(outputSchemaPath)));
+
+        Map<PropertyDescriptor, String> xmlReaderProperties = new HashMap<>();
+        xmlReaderProperties.put(SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY.getValue());
+        xmlReaderProperties.put(SchemaAccessUtils.SCHEMA_TEXT, outputSchemaText);
+        xmlReaderProperties.put(XMLReader.RECORD_FORMAT, XMLReader.RECORD_EVALUATE.getValue());
+        TestRunner runner = setup(xmlReaderProperties);
+
+        // WHEN
         InputStream is = new FileInputStream("src/test/resources/xml/people.xml");
         runner.enqueue(is, Collections.singletonMap(EVALUATE_IS_ARRAY, "true"));
         runner.run();
 
+        // THEN
         List<MockFlowFile> flowFile = runner.getFlowFilesForRelationship(TestXMLReaderProcessor.SUCCESS);
         List<String> records = Arrays.asList((new String(runner.getContentAsByteArray(flowFile.get(0)))).split("\n"));
 
         assertEquals(4, records.size());
     }
 
     @Test
-    public void testRecordFormat2() throws IOException, InitializationException {
-        TestRunner runner = setup("src/test/resources/xml/testschema");
-
-        runner.setProperty(reader, XMLReader.RECORD_FORMAT, XMLReader.RECORD_ARRAY);
-
-        runner.enableControllerService(reader);
-
+    public void testRecordFormatArray() throws IOException, InitializationException {

Review Comment:
   Using the following input: 
   
   `<record>
     <num>123</num>
     <software favorite="true">Apache NiFi</software>
   </record> `
   
   and the value "body" for "content field" property,  With JsonRecordSetWriter I got the following result:
   
   `[{"num":123,"software":{"favorite":true,"body":"Apache NiFi"},"favorite":null}]`
   
   Is the favorite with the null value expected?



##########
nifi-nar-bundles/nifi-standard-services/nifi-record-serialization-services-bundle/nifi-record-serialization-services/src/test/java/org/apache/nifi/xml/TestXMLReader.java:
##########
@@ -31,80 +33,99 @@
 import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.Collections;
+import java.util.HashMap;
 import java.util.List;
+import java.util.Map;
 
 import static junit.framework.TestCase.assertEquals;
 
 public class TestXMLReader {
 
-    private XMLReader reader;
-
     private final String ATTRIBUTE_PREFIX = "attribute_prefix";
     private final String CONTENT_NAME = "content_field";
     private final String EVALUATE_IS_ARRAY = "xml.stream.is.array";
 
-    public TestRunner setup(String filePath) throws InitializationException, IOException {
-
+    private TestRunner setup(Map<PropertyDescriptor, String> xmlReaderProperties) throws InitializationException {
         TestRunner runner = TestRunners.newTestRunner(TestXMLReaderProcessor.class);
-        reader = new XMLReader();
+        XMLReader reader = new XMLReader();
+
         runner.addControllerService("xml_reader", reader);
         runner.setProperty(TestXMLReaderProcessor.XML_READER, "xml_reader");
 
-        final String outputSchemaText = new String(Files.readAllBytes(Paths.get(filePath)));
-        runner.setProperty(reader, SchemaAccessUtils.SCHEMA_ACCESS_STRATEGY, SchemaAccessUtils.SCHEMA_TEXT_PROPERTY);
-        runner.setProperty(reader, SchemaAccessUtils.SCHEMA_TEXT, outputSchemaText);
+        for (Map.Entry<PropertyDescriptor, String> entry : xmlReaderProperties.entrySet()) {
+            runner.setProperty(reader, entry.getKey(), entry.getValue());
+        }
 
+        runner.enableControllerService(reader);
         return runner;
     }
 
     @Test
-    public void testRecordFormat() throws IOException, InitializationException {
-        TestRunner runner = setup("src/test/resources/xml/testschema");
-
-        runner.setProperty(reader, XMLReader.RECORD_FORMAT, XMLReader.RECORD_EVALUATE);
-
-        runner.enableControllerService(reader);
-
+    public void testRecordFormatDeterminedBasedOnAttribute() throws IOException, InitializationException {
+        // GIVEN

Review Comment:
   Personally I prefer this too, but in the last couple of reviews I did see (or pushed), this question came up with the conclusion I mentioned



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@nifi.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org