diff --git a/plugins/transforms/xml/src/main/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMeta.java b/plugins/transforms/xml/src/main/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMeta.java index dd822b28668..2c493bb8f8c 100644 --- a/plugins/transforms/xml/src/main/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMeta.java +++ b/plugins/transforms/xml/src/main/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMeta.java @@ -120,6 +120,16 @@ public void getFields( IValueMeta v = new ValueMetaString(variables.resolve(this.getValueXmlField())); v.setOrigin(name); + IRowMeta knownTarget = info == null || info.length == 0 ? null : info[0]; + if (knownTarget != null) { + // The target's row with the joined XML, as processRow() builds it, whatever rows the + // previous transforms give here + IRowMeta target = knownTarget.clone(); + row.clear(); + row.addRowMeta(target); + row.addValueMeta(v); + return; + } try { /* Row should only include fields from the target and not the source. During the preview table diff --git a/plugins/transforms/xml/src/test/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMetaGetFieldsTest.java b/plugins/transforms/xml/src/test/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMetaGetFieldsTest.java index 5ac27f6d7f3..79f15a7637c 100644 --- a/plugins/transforms/xml/src/test/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMetaGetFieldsTest.java +++ b/plugins/transforms/xml/src/test/java/org/apache/hop/pipeline/transforms/xml/xmljoin/XmlJoinMetaGetFieldsTest.java @@ -20,9 +20,12 @@ import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.mock; +import java.util.List; import org.apache.hop.core.HopClientEnvironment; import org.apache.hop.core.row.IRowMeta; +import org.apache.hop.core.row.IValueMeta; import org.apache.hop.core.row.RowMeta; +import org.apache.hop.core.row.value.ValueMetaInteger; import org.apache.hop.core.row.value.ValueMetaString; import org.apache.hop.core.variables.Variables; import org.apache.hop.pipeline.PipelineMeta; @@ -150,4 +153,28 @@ void testGetFieldsReturnTargetTransformFieldsWithDuplicates() throws Exception { assertEquals("c", strings[1]); assertEquals("result xml field name", strings[2]); } + + /** + * The previous fields are merged in the order of the hops: with the source's hop first, a field + * both streams have keeps the source's type, and the fields come in the source's order. The + * transform writes the target's row, so that is what it declares. + */ + @Test + void testGetFieldsGiveTheTargetRowWhateverTheOrderOfThePreviousFields() throws Exception { + RowMeta source = new RowMeta(); + source.addValueMeta(new ValueMetaInteger("c")); + source.addValueMeta(new ValueMetaString("a")); + RowMeta target = new RowMeta(); + target.addValueMeta(new ValueMetaString("a")); + target.addValueMeta(new ValueMetaString("c")); + // As PipelineMeta merges the fields of the previous transforms, the source's first + RowMeta rowMeta = source.clone(); + xmlJoinMeta.setValueXmlField("xml"); + + xmlJoinMeta.getFields( + rowMeta, "join", new IRowMeta[] {target, source}, null, new Variables(), null); + + assertEquals(List.of("a", "c", "xml"), List.of(rowMeta.getFieldNames())); + assertEquals(IValueMeta.TYPE_STRING, rowMeta.getValueMeta(1).getType()); + } }