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

   ## JIRA
   
   https://issues.apache.org/jira/browse/CAMEL-24515
   
   ## Related
   
   Follow-up to [PR #25760](https://github.com/apache/camel/pull/25760) 
(CAMEL-24510) which fixed the same NPE in `ExchangeHelper.convertToType()` and 
`convertToMandatoryType()`. Identified as a follow-up opportunity in [this 
review 
comment](https://github.com/apache/camel/pull/25760#pullrequestreview-5031635793).
   
   ## Problem
   
   `AbstractExchange.getIn(Class<T>)` and `getOut(Class<T>)` call 
`context.getTypeConverter().convertTo(...)` directly without a null guard. When 
`CamelContext` is shutting down, 
`DefaultCamelContextExtension.resetTypeConverter()` sets the type converter to 
`null` during `forceStopLazyInitialization()`. Any thread calling these methods 
during that window receives a bare `NullPointerException`:
   
   ```
   NullPointerException: Cannot invoke "TypeConverter.convertTo(...)"
       because "getTypeConverter()" is null
       at 
org.apache.camel.support.AbstractExchange.getIn(AbstractExchange.java:499)
   ```
   
   ## Fix
   
   Add null guard on the `TypeConverter` in both methods, throwing 
`IllegalStateException` with a descriptive message — matching the same fix 
applied to `ExchangeHelper` in CAMEL-24510.
   
   **Before:**
   ```
   NullPointerException: Cannot invoke "TypeConverter.convertTo(...)" because 
"getTypeConverter()" is null
   ```
   
   **After:**
   ```
   IllegalStateException: Cannot convert message body to java.lang.String 
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/AbstractExchange.java`
 — null guard in `getIn(Class<T>)` and `getOut(Class<T>)`
   - 
`core/camel-support/src/test/java/org/apache/camel/support/AbstractExchangeNullTypeConverterTest.java`
 — 2 tests covering null converter for both methods
   - `core/camel-support/pom.xml` — add assertj-core and mockito-core test 
dependencies
   
   ## Test plan
   
   - [x] `mvn test -pl core/camel-support 
-Dtest=AbstractExchangeNullTypeConverterTest` — Tests run: 2, 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