davsclaus opened a new pull request, #29111: URL: https://github.com/apache/camel/pull/29111
Fixes [CAMEL-26509](https://issues.apache.org/jira/browse/CAMEL-26509). ### 1. TypeConversionException always said `from type: java.lang.String` **What was wrong:** when a type converter failed while converting with an exchange, `CoreTypeConverterRegistry.createTypeConversionException` built the exception from `MessageHelper.extractValueForLogging(value, exchange.getIn())`. That is a `String`, so the message always said `Error during type conversion from type: java.lang.String ...`, and `getValue()` / `getFromType()` gave that logging string and `String.class`. Without an exchange the real value was already used. **What changed:** the exception gets the value itself. Since CAMEL-24553 the message of `TypeConversionException` no longer includes the value (only its type), so the value prepared for logging was not used for the message text any more; only its wrong type showed. This also saves building the logging string (which may call `toString()` on the body) on every failed conversion. `getValue()` stays a transient field, as before. ### 2. ArrayTypeConverter threw for an element that does not fit the array **What was wrong:** `ArrayTypeConverter` fills the target array with `Array.set` (primitive arrays) or `Collection.toArray` (object arrays). An element that does not fit the component type, such as a `String` into `int[]` / `byte[]`, or a `null` into a primitive array, threw `IllegalArgumentException: argument type mismatch` or `ArrayStoreException`, so the conversion failed with a `TypeConversionException` instead of giving no conversion. **What changed:** such a conversion returns `null` (no conversion), so the other converters can try, `convertTo` returns `null` and `mandatoryConvertTo` throws `NoTypeConversionAvailableException`. Elements that fit convert as before, also after the fallback converter was promoted for the collection type. This is also the root cause of what was seen in CAMEL-26137: a `java.util.List` body converted to `byte[]` failed with `argument type mismatch`, and the exception named `java.lang.String` as the source type. The new test covers a list of strings to `byte[]`. ### Tests `CoreTypeConverterRegistryTest` (camel-core) has three new tests: the exception gives the real value and type (with `convertTo` and `mandatoryConvertTo` with an exchange), and a collection and an array with elements that do not fit `int[]`, `Integer[]` and `byte[]`. All three failed before the fix (wrong value `java.lang.String`, and `TypeConversionException ... argument type mismatch`) and pass now. The full camel-core test suite passes (8783 tests) against this camel-base. ### Docs Upgrade guide 4.24: a `camel-core` entry for both changes, as code that reads `getValue()` / `getFromType()` or catches the `TypeConversionException` from such an array conversion sees a different result. ### Not changed - The element conversion itself: `ArrayTypeConverter` does not convert each element with the type converter (for example `"1"` to `int`). That would be a new feature. - No logging was added in the converter. _Claude Code on behalf of Claus Ibsen (davsclaus)_ 🤖 Generated with [Claude Code](https://claude.com/claude-code) -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
