This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 24ae572d9034 CAMEL-27058: camel-bindy - fixed-length trim reads a
number padded with '0' that is only made of '0' as 0 (#30228)
24ae572d9034 is described below
commit 24ae572d9034d4f15d380e55f5b0f5d6153cf91b
Author: Federico Mariani <[email protected]>
AuthorDate: Tue Nov 17 12:59:06 2026 +0100
CAMEL-27058: camel-bindy - fixed-length trim reads a number padded with '0'
that is only made of '0' as 0 (#30228)
* CAMEL-27058: camel-bindy - fixed-length trim reads a number padded with
'0' that is only made of '0' as 0
Co-Authored-By: Claude <[email protected]>
---
.../camel/catalog/docs/bindy-dataformat.adoc | 6 +-
.../src/main/docs/bindy-dataformat.adoc | 6 +-
.../dataformat/bindy/BindyFixedLengthFactory.java | 23 ++-
.../dataformat/bindy/annotation/DataField.java | 4 +-
.../fixed/BindyFixedLengthTrimZeroPaddingTest.java | 206 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 8 +
6 files changed, 244 insertions(+), 9 deletions(-)
diff --git
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/bindy-dataformat.adoc
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/bindy-dataformat.adoc
index 76bfc268bdcf..5d988ba8f097 100644
---
a/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/bindy-dataformat.adoc
+++
b/catalog/camel-catalog/src/generated/resources/org/apache/camel/catalog/docs/bindy-dataformat.adoc
@@ -493,7 +493,9 @@ HALF_DOWN,HALF_EVEN, UNNECESSARY e.g : Number = 123456.789,
Precision = 2, Round
| timezone | String | | | Timezone to be used.
-| trim | boolean | | false | Indicates if the value should be trimmed
+| trim | boolean | | false a| Indicates if the value should be trimmed. In a
fixed-length record, unmarshal removes the padding char (on the
+left with align R, on the right with L, on both sides with B); a number padded
with '0' that is only made of '0'
+is read as 0.
|===
// annotation options: END
@@ -944,6 +946,8 @@ Sometimes, the default padding defined for record cannot be
applied to
the field as we have a number format where we would like to pad with
'0' instead of `' '`. In this case, you can use in the model the attribute
`paddingChar` on `@DataField` to set this value.
+With `trim = true`, unmarshal removes this padding, and a number made only of
+'0' (for example `000`) is read as 0.
----
10A9 PaulineM ISINXD12345678BUYShare000002500.45USD01-08-2009
diff --git a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
index 76bfc268bdcf..5d988ba8f097 100644
--- a/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
+++ b/components/camel-bindy/src/main/docs/bindy-dataformat.adoc
@@ -493,7 +493,9 @@ HALF_DOWN,HALF_EVEN, UNNECESSARY e.g : Number = 123456.789,
Precision = 2, Round
| timezone | String | | | Timezone to be used.
-| trim | boolean | | false | Indicates if the value should be trimmed
+| trim | boolean | | false a| Indicates if the value should be trimmed. In a
fixed-length record, unmarshal removes the padding char (on the
+left with align R, on the right with L, on both sides with B); a number padded
with '0' that is only made of '0'
+is read as 0.
|===
// annotation options: END
@@ -944,6 +946,8 @@ Sometimes, the default padding defined for record cannot be
applied to
the field as we have a number format where we would like to pad with
'0' instead of `' '`. In this case, you can use in the model the attribute
`paddingChar` on `@DataField` to set this value.
+With `trim = true`, unmarshal removes this padding, and a number made only of
+'0' (for example `000`) is read as 0.
----
10A9 PaulineM ISINXD12345678BUYShare000002500.45USD01-08-2009
diff --git
a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java
index 0ef25f69876e..fbcf7420e086 100644
---
a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java
+++
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/BindyFixedLengthFactory.java
@@ -234,7 +234,7 @@ public class BindyFixedLengthFactory extends
BindyAbstractFactory implements Bin
}
if (dataField.trim()) {
- token = trim(token, dataField, paddingChar);
+ token = trim(token, dataField, paddingChar,
annotatedFields.get(dataField.pos()).getType());
//token = token.trim();
}
@@ -344,19 +344,30 @@ public class BindyFixedLengthFactory extends
BindyAbstractFactory implements Bin
}
- private String trim(String token, DataField dataField, char paddingChar) {
+ private String trim(String token, DataField dataField, char paddingChar,
Class<?> type) {
char myPaddingChar = dataField.paddingChar();
if (dataField.paddingChar() == 0) {
myPaddingChar = paddingChar;
}
+ String trimmed;
if ("R".equals(dataField.align())) {
- return leftTrim(token, myPaddingChar);
+ trimmed = leftTrim(token, myPaddingChar);
} else if ("L".equals(dataField.align())) {
- return rightTrim(token, myPaddingChar);
+ trimmed = rightTrim(token, myPaddingChar);
} else {
- token = leftTrim(token, myPaddingChar);
- return rightTrim(token, myPaddingChar);
+ trimmed = rightTrim(leftTrim(token, myPaddingChar), myPaddingChar);
}
+ // a number padded with '0' that is only made of '0' is the number 0,
not an empty field
+ if (trimmed.isEmpty() && !token.isEmpty() && myPaddingChar == '0' &&
isNumber(type)) {
+ return "0";
+ }
+ return trimmed;
+ }
+
+ private static boolean isNumber(Class<?> type) {
+ return Number.class.isAssignableFrom(type)
+ || type == int.class || type == long.class || type ==
short.class || type == byte.class
+ || type == double.class || type == float.class;
}
private String rightTrim(String token, char myPaddingChar) {
diff --git
a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
index 846cc6f0427c..964aa5fda230 100644
---
a/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
+++
b/components/camel-bindy/src/main/java/org/apache/camel/dataformat/bindy/annotation/DataField.java
@@ -106,7 +106,9 @@ public @interface DataField {
boolean required() default false;
/**
- * Indicates if the value should be trimmed
+ * Indicates if the value should be trimmed. In a fixed-length record,
unmarshal removes the padding char (on the
+ * left with align R, on the right with L, on both sides with B); a number
padded with '0' that is only made of '0'
+ * is read as 0.
*/
boolean trim() default false;
diff --git
a/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthTrimZeroPaddingTest.java
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthTrimZeroPaddingTest.java
new file mode 100644
index 000000000000..40d9bb7acb30
--- /dev/null
+++
b/components/camel-bindy/src/test/java/org/apache/camel/dataformat/bindy/fixed/BindyFixedLengthTrimZeroPaddingTest.java
@@ -0,0 +1,206 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.dataformat.bindy.fixed;
+
+import java.math.BigDecimal;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.dataformat.bindy.annotation.DataField;
+import org.apache.camel.dataformat.bindy.annotation.FixedLengthRecord;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNull;
+
+/**
+ * A number padded with '0' and unmarshalled with trim = true: trimming the
padding must not remove the value 0.
+ */
+public class BindyFixedLengthTrimZeroPaddingTest extends CamelTestSupport {
+
+ @Override
+ protected RouteBuilder createRouteBuilder() {
+ return new RouteBuilder() {
+ @Override
+ public void configure() {
+ from("direct:numbers").unmarshal(new
BindyFixedLengthDataFormat(Numbers.class)).to("mock:result");
+ from("direct:marshalNumbers").marshal(new
BindyFixedLengthDataFormat(Numbers.class)).to("mock:result");
+ from("direct:required").unmarshal(new
BindyFixedLengthDataFormat(RequiredNumber.class)).to("mock:result");
+ from("direct:recordPadding").unmarshal(new
BindyFixedLengthDataFormat(RecordPadding.class))
+ .to("mock:result");
+ from("direct:noTrim").unmarshal(new
BindyFixedLengthDataFormat(NumbersNoTrim.class)).to("mock:result");
+ from("direct:text").unmarshal(new
BindyFixedLengthDataFormat(Text.class)).to("mock:result");
+ from("direct:spaces").unmarshal(new
BindyFixedLengthDataFormat(SpacePadded.class)).to("mock:result");
+ from("direct:short").unmarshal(new
BindyFixedLengthDataFormat(ShortLine.class)).to("mock:result");
+ }
+ };
+ }
+
+ private <T> T send(String uri, Object body, Class<T> type) throws
Exception {
+ MockEndpoint mock = getMockEndpoint("mock:result");
+ mock.reset();
+ mock.expectedMessageCount(1);
+ template.sendBody(uri, body);
+ mock.assertIsSatisfied();
+ return mock.getReceivedExchanges().get(0).getIn().getBody(type);
+ }
+
+ @Test
+ public void allZeroNumbersUnmarshalToZero() throws Exception {
+ Numbers numbers = send("direct:numbers", "000000000000000",
Numbers.class);
+
+ assertEquals(Integer.valueOf(0), numbers.boxed);
+ assertEquals(0, numbers.prim);
+ assertEquals(0L, numbers.big);
+ assertEquals(new BigDecimal("0.00"), numbers.amount);
+ }
+
+ @Test
+ public void zeroRoundTrips() throws Exception {
+ Numbers zero = new Numbers();
+ zero.boxed = 0;
+ zero.amount = BigDecimal.ZERO;
+
+ String line = send("direct:marshalNumbers", zero, String.class);
+ assertEquals("000000000000000", line.trim());
+
+ Numbers back = send("direct:numbers", line, Numbers.class);
+ assertEquals(Integer.valueOf(0), back.boxed);
+ assertEquals(0, back.prim);
+ assertEquals(0L, back.big);
+ assertEquals(new BigDecimal("0.00"), back.amount);
+ }
+
+ @Test
+ public void requiredFieldAcceptsZero() throws Exception {
+ RequiredNumber number = send("direct:required", "000",
RequiredNumber.class);
+
+ assertEquals(0, number.value);
+ }
+
+ @Test
+ public void recordPaddingCharZeroUnmarshalsToZero() throws Exception {
+ RecordPadding number = send("direct:recordPadding", "000",
RecordPadding.class);
+
+ assertEquals(0, number.value);
+ }
+
+ @Test
+ public void paddedNumbersKeepTheirDigits() throws Exception {
+ Numbers numbers = send("direct:numbers", "007012005000125",
Numbers.class);
+
+ assertEquals(Integer.valueOf(7), numbers.boxed);
+ assertEquals(12, numbers.prim);
+ assertEquals(50L, numbers.big);
+ assertEquals(new BigDecimal("1.25"), numbers.amount);
+ }
+
+ @Test
+ public void withoutTrimZeroIsZero() throws Exception {
+ NumbersNoTrim numbers = send("direct:noTrim", "000000",
NumbersNoTrim.class);
+
+ assertEquals(Integer.valueOf(0), numbers.boxed);
+ assertEquals(0, numbers.prim);
+ }
+
+ @Test
+ public void textPaddedWithZeroStaysEmpty() throws Exception {
+ Text text = send("direct:text", "000", Text.class);
+
+ assertEquals("", text.value);
+ }
+
+ @Test
+ public void numbersPaddedWithSpacesStayEmpty() throws Exception {
+ SpacePadded numbers = send("direct:spaces", "x ",
SpacePadded.class);
+
+ assertNull(numbers.boxed);
+ assertEquals(Integer.MIN_VALUE, numbers.prim);
+ }
+
+ @Test
+ public void missingNumbersStayEmpty() throws Exception {
+ ShortLine numbers = send("direct:short", "007", ShortLine.class);
+
+ assertEquals(Integer.valueOf(7), numbers.first);
+ assertNull(numbers.second);
+ }
+
+ @FixedLengthRecord(length = 15)
+ public static class Numbers {
+ @DataField(pos = 1, length = 3, paddingChar = '0', trim = true)
+ Integer boxed;
+
+ @DataField(pos = 4, length = 3, paddingChar = '0', trim = true)
+ int prim;
+
+ @DataField(pos = 7, length = 4, paddingChar = '0', trim = true)
+ long big;
+
+ @DataField(pos = 11, length = 5, precision = 2,
impliedDecimalSeparator = true, paddingChar = '0', trim = true)
+ BigDecimal amount;
+ }
+
+ @FixedLengthRecord(length = 3)
+ public static class RequiredNumber {
+ @DataField(pos = 1, length = 3, paddingChar = '0', trim = true,
required = true)
+ int value;
+ }
+
+ @FixedLengthRecord(length = 3, paddingChar = '0')
+ public static class RecordPadding {
+ @DataField(pos = 1, length = 3, paddingChar = 0, trim = true)
+ int value;
+ }
+
+ @FixedLengthRecord(length = 6)
+ public static class NumbersNoTrim {
+ @DataField(pos = 1, length = 3, paddingChar = '0')
+ Integer boxed;
+
+ @DataField(pos = 4, length = 3, paddingChar = '0')
+ int prim;
+ }
+
+ @FixedLengthRecord(length = 3)
+ public static class Text {
+ @DataField(pos = 1, length = 3, paddingChar = '0', trim = true)
+ String value;
+ }
+
+ @FixedLengthRecord(length = 7)
+ public static class SpacePadded {
+ @DataField(pos = 1, length = 1)
+ String id;
+
+ @DataField(pos = 2, length = 3, trim = true)
+ Integer boxed;
+
+ @DataField(pos = 5, length = 3, trim = true)
+ int prim;
+ }
+
+ @FixedLengthRecord(length = 6, ignoreMissingChars = true)
+ public static class ShortLine {
+ @DataField(pos = 1, length = 3, paddingChar = '0', trim = true)
+ Integer first;
+
+ @DataField(pos = 4, length = 3, paddingChar = '0', trim = true)
+ Integer second;
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index c1c5b8ad02af..e5bd5be7ceda 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -703,6 +703,14 @@ The `camel.routeId` MDC value is now the id of the route
that runs the step, as
`camel.main.useMdcLogging`. Before, it was the id of the route that created
the exchange, so the log lines of a route
called with `direct:` (or another route-to-route endpoint) showed the id of
the calling route.
+=== camel-bindy - fixed-length trim keeps the value 0 of a number padded with
'0'
+
+In a fixed-length record, a numeric `@DataField` with `paddingChar = '0'` and
`trim = true` that only holds `0`
+characters (for example `000`) is now read as 0. Before, trimming removed
every `0` and the field was read as empty:
+`null` for an object type such as `Integer` or `BigDecimal`, `MIN_VALUE` for a
primitive such as `int`, the
+`defaultValue` of the field when it has one, and an error for a `required`
field. Text fields, and numeric fields
+padded with another char, are not changed.
+
=== Java
*Breaking change:* Camel 4.24 requires *Java 21* as the minimum runtime
version.