mayurbm opened a new pull request, #25760:
URL: https://github.com/apache/camel/pull/25760

   ## JIRA
   
   https://issues.apache.org/jira/browse/CAMEL-24510
   
   ## Problem
   
   `ExchangeHelper.convertToType()` and `convertToMandatoryType()` call 
`exchange.getContext().getTypeConverter().convertTo(...)` without a null guard 
on `getTypeConverter()`. When `CamelContext` is stopping, restarting, or not 
fully initialized (e.g. during a Quartz SFTP poll shutdown), 
`getTypeConverter()` can return `null`, causing a bare `NullPointerException` 
with no actionable diagnostic message.
   
   ## Stack trace
   
   ```
   java.lang.NullPointerException: Cannot invoke 
"org.apache.camel.TypeConverter.convertTo(java.lang.Class, 
org.apache.camel.Exchange, Object)"
       because the return value of 
"org.apache.camel.CamelContext.getTypeConverter()" is null
       at 
org.apache.camel.support.ExchangeHelper.convertToType(ExchangeHelper.java:261)
       at 
org.apache.camel.support.AbstractExchange.getProperty(AbstractExchange.java:321)
       at 
org.apache.camel.support.DefaultExchange.getProperty(DefaultExchange.java:27)
   ```
   
   ## Fix
   
   Add null guards in both `convertToType` and `convertToMandatoryType`, 
throwing a descriptive `IllegalStateException` instead of a bare NPE. Also add 
fast-path short-circuits in `convertToType`: return `null` for null values, 
return the value directly if it is already the requested type.
   
   **Before:**
   ```
   NullPointerException: Cannot invoke "TypeConverter.convertTo(...)" because 
the return value of "CamelContext.getTypeConverter()" is null
   ```
   
   **After:**
   ```
   IllegalStateException: Cannot convert from java.lang.String to 
java.lang.Boolean because CamelContext type converter is not available (context 
not started, stopped, or not initialized)
   ```
   
   ## Changes
   
   - 
`core/camel-support/src/main/java/org/apache/camel/support/ExchangeHelper.java` 
— null guards in `convertToType` and `convertToMandatoryType`
   - 
`core/camel-support/src/test/java/org/apache/camel/support/ExchangeHelperNullTypeConverterTest.java`
 — 5 tests covering null converter, null value, already correct type, null 
exchange
   - `core/camel-support/pom.xml` — add assertj-core and mockito-core test 
dependencies
   
   ## Test plan
   
   - [x] `mvn test -pl core/camel-support 
-Dtest=ExchangeHelperNullTypeConverterTest` — Tests run: 5, Failures: 0
   - [x] `mvn formatter:format impsort:sort` — no changes needed
   
   > AI-assisted contribution. Co-authored-by: Claude <[email protected]>


-- 
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]

Reply via email to