This is an automated email from the ASF dual-hosted git repository.
mattcasters pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/hop.git
The following commit(s) were added to refs/heads/main by this push:
new 7e2ec9a4b1 HOP-9037 Calculator: restore -1 default for
value_length/value_precision on legacy loads (#9038)
7e2ec9a4b1 is described below
commit 7e2ec9a4b17b909a1876ee19d60867a5da6630b3
Author: Romain Manni-Bucau <[email protected]>
AuthorDate: Wed Oct 21 09:12:51 2026 +0200
HOP-9037 Calculator: restore -1 default for value_length/value_precision on
legacy loads (#9038)
CalculatorMetaFunction lost its legacy -1 default for
valueLength/valuePrecision
when PR #795 (HOP-2849) replaced the hand-written XML loader with
HopMetadataProperty-based automatic (de)serialization. A plain `int
valueLength;`
now deserializes to Java's 0 when the tag is absent, whereas the old loader
defaulted to -1 (the sentinel for "unspecified length/precision" used by
CalculatorMeta.getValueMeta() and by CalculatorDialog, which reads blank
cells
back as -1 and only shows a length >= 0). Initialize both fields to -1 so a
legacy restore keeps the sentinel instead of collapsing it to 0. Add two
tests
to CalculatorMetaFunctionTest: one restoring a legacy XML without
value_length/value_precision (asserts -1; fails before the fix with
expected:<-1> but was:<0>), and one restoring XML with explicit values
(asserts
they are preserved and not clobbered by the default).
Fixes #9037
---
.../calculator/CalculatorMetaFunction.java | 4 +-
.../calculator/CalculatorMetaFunctionTest.java | 55 ++++++++++++++++++++++
2 files changed, 57 insertions(+), 2 deletions(-)
diff --git
a/plugins/transforms/calculator/src/main/java/org/apache/hop/pipeline/transforms/calculator/CalculatorMetaFunction.java
b/plugins/transforms/calculator/src/main/java/org/apache/hop/pipeline/transforms/calculator/CalculatorMetaFunction.java
index 74ed8a7b30..b5701a15a2 100644
---
a/plugins/transforms/calculator/src/main/java/org/apache/hop/pipeline/transforms/calculator/CalculatorMetaFunction.java
+++
b/plugins/transforms/calculator/src/main/java/org/apache/hop/pipeline/transforms/calculator/CalculatorMetaFunction.java
@@ -67,13 +67,13 @@ public class CalculatorMetaFunction implements Cloneable {
key = "value_length",
injectionKey = "value_length",
injectionKeyDescription =
"CalculatorMeta.Injection.Calculation.ValueLength")
- private int valueLength;
+ private int valueLength = -1;
@HopMetadataProperty(
key = "value_precision",
injectionKey = "value_precision",
injectionKeyDescription =
"CalculatorMeta.Injection.Calculation.ValuePrecision")
- private int valuePrecision;
+ private int valuePrecision = -1;
@HopMetadataProperty(
key = "conversion_mask",
diff --git
a/plugins/transforms/calculator/src/test/java/org/apache/hop/pipeline/transforms/calculator/CalculatorMetaFunctionTest.java
b/plugins/transforms/calculator/src/test/java/org/apache/hop/pipeline/transforms/calculator/CalculatorMetaFunctionTest.java
index 8cb8f7c813..00c7bae444 100644
---
a/plugins/transforms/calculator/src/test/java/org/apache/hop/pipeline/transforms/calculator/CalculatorMetaFunctionTest.java
+++
b/plugins/transforms/calculator/src/test/java/org/apache/hop/pipeline/transforms/calculator/CalculatorMetaFunctionTest.java
@@ -69,4 +69,59 @@ class CalculatorMetaFunctionTest {
assertEquals("-1", XmlHandler.getTagValue(node, "value_precision"));
assertEquals("String", XmlHandler.getTagValue(node, "value_type"));
}
+
+ @Test
+ void testRestoreLegacyXmlWithoutLengthPrecisionDefaultsToMinusOne() throws
Exception {
+ // A legacy pipeline (Hop <= 2.x) that did NOT write
value_length/value_precision.
+ // Before HOP-2849 the -1 default survived this restore; after the switch
to
+ // HopMetadataProperty-based serialization the fields were left at their
Java default 0
+ // until a fix re-initialised them to -1.
+ String legacy =
+ ""
+ + "<hop>"
+ + " <field_name>sum</field_name>"
+ + " <calc_type>ADD</calc_type>"
+ + " <field_a>A</field_a>"
+ + " <field_b>B</field_b>"
+ + " <value_type>Number</value_type>"
+ + " <remove>N</remove>"
+ + "</hop>";
+
+ Node node = XmlHandler.loadXmlString(legacy, "hop");
+
+ CalculatorMetaFunction function =
+ XmlMetadataUtil.deSerializeFromXml(node, CalculatorMetaFunction.class,
null);
+
+ assertEquals("sum", function.getFieldName());
+ // Regression HOP-2849: the -1 sentinel for an un-specified
length/precision must survive a
+ // legacy restore instead of collapsing to 0.
+ assertEquals(-1, function.getValueLength());
+ assertEquals(-1, function.getValuePrecision());
+ }
+
+ @Test
+ void testRestoreXmlWithExplicitLengthPrecisionPreservesValues() throws
Exception {
+ // A regular file where the tags ARE present: the values must round-trip
untouched and must
+ // not be clobbered by the -1 default used for absent tags.
+ String xml =
+ ""
+ + "<hop>"
+ + " <field_name>rounded</field_name>"
+ + " <calc_type>ROUND_2</calc_type>"
+ + " <field_a>A</field_a>"
+ + " <value_type>Number</value_type>"
+ + " <value_length>15</value_length>"
+ + " <value_precision>4</value_precision>"
+ + " <remove>N</remove>"
+ + "</hop>";
+
+ Node node = XmlHandler.loadXmlString(xml, "hop");
+
+ CalculatorMetaFunction function =
+ XmlMetadataUtil.deSerializeFromXml(node, CalculatorMetaFunction.class,
null);
+
+ assertEquals("rounded", function.getFieldName());
+ assertEquals(15, function.getValueLength());
+ assertEquals(4, function.getValuePrecision());
+ }
}