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

   ## CAMEL-27164: the configurer of a data format is the one of its own class
   
   https://issues.apache.org/jira/browse/CAMEL-27164
   
   ### Problem
   
   Since CAMEL-22857, when both camel-jackson (Jackson 2.x) and camel-jackson3 
are on the classpath,
   `DefaultDataFormatResolver` creates the Jackson 3.x data format for the 
names `jackson`, `avroJackson`,
   `protobufJackson` and `jacksonXml`. The configurer that sets the options of 
that data format is still found by name
   (`META-INF/services/org/apache/camel/configurer/jackson-dataformat`), which 
both JARs register, so the JAR first on
   the classpath wins. When that is camel-jackson, binding any option fails 
when the route is created:
   
   ```
   Error binding property 
(unmarshalType=com.fasterxml.jackson.databind.JsonNode) with name: 
unmarshalType on bean:
   org.apache.camel.component.jackson3.JacksonDataFormat@58fbcb71 ... class 
org.apache.camel.component.jackson3.JacksonDataFormat
   cannot be cast to class org.apache.camel.component.jackson.JacksonDataFormat
   ```
   
   This is what happens with Camel JBang (which has camel-jackson3) running a 
Kamelet that adds camel-jackson, for
   example with `unmarshal: json: library: Jackson, unmarshalType: ...`.
   
   The by-name lookup is in several places: `DataFormatReifier` (the data 
format of the route, and the options it takes
   from the data format configured on the context, such as 
`camel.dataformat.jackson.*`), `PropertyConfigurerHelper`
   (which maps the class `JacksonDataFormat` to `jackson-dataformat` by its 
simple name, the same for both lines), the
   Rest DSL binding (`RestBindingAdviceFactory`, the 
`JacksonXMLRestBindingJacksonXmlDataFormatFactory` of both XML
   lines), `RestProducer`, the `dataformat:` component, autowiring 
(`LifecycleStrategySupport`) and the
   `camel.dataformat.<name>.*` options of Camel Main 
(`MainHelper.configurerByName`).
   
   ### Fix
   
   A new `PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, 
name, target)` finds the configurer by name
   as before, and when it is a generated configurer for another type than the 
target (generated configurers are named
   after their type with the `Configurer` suffix, so this compares the class 
names along the target's class hierarchy),
   it uses the configurer generated for the target's own type instead. All the 
by-name lookups above use it, and so does
   the class to name mapping of 
`PropertyConfigurerHelper.resolvePropertyConfigurer(context, Class)`.
   
   With a single Jackson line on the classpath nothing changes: the configurer 
found by name is the one of the data
   format. A configurer that is not generated (such as one bound in the 
registry) is used as before, and so is a
   mismatching one when the target's type has no generated configurer, or when 
there is no target (autowiring can be
   called without a data format).
   
   ### Test
   
   `KameletJacksonBothLinesTest` in camel-kamelet, which already has both 
Jackson lines on its test classpath. Its class
   resolver finds the configurers of camel-jackson first, as when the 
camel-jackson JAR comes first on the classpath. It
   checks that the configurer is the one of the Jackson 3.x data format, that a 
Kamelet with
   `unmarshal().json(JsonLibrary.Jackson, Greeting.class)` unmarshals to 
`Greeting`, and that a Kamelet takes the options
   of the jackson data format configured on the context (`prettyPrint`). 
Without the fix, all three fail with the
   `ClassCastException` above.
   
   ### Verification
   
   - Tests of camel-support, camel-core-reifier, camel-kamelet, camel-jackson, 
camel-jackson3, camel-jacksonxml,
     camel-jackson3xml, camel-rest, camel-main and camel-core.
   - Camel JBang 4.24.0-SNAPSHOT with the changed modules installed locally, 
with
     `--dep=org.apache.camel:camel-jackson:4.24.0-SNAPSHOT` and with the 
`json-deserialize-action` Kamelet (which adds
     `camel:jackson`): the routes start and unmarshal with the Jackson 3.x data 
format. On this machine camel-jackson3 came
     first on the JBang classpath, so the startup failure did not show without 
the fix either; which JAR comes first
     depends on the classpath order, and the unit test controls that order.
   
   Note: the Jackson 3.x data format cannot unmarshal to the Jackson 2.x type 
`com.fasterxml.jackson.databind.JsonNode`
   (`InvalidDefinitionException: Cannot construct instance of 
com.fasterxml.jackson.databind.JsonNode`). Routes and
   Kamelets that name that type still fail, now when a message is unmarshalled; 
with `tools.jackson.databind.JsonNode`
   they work. This is the choice of CAMEL-22857 (Jackson 3.x when both lines 
are present), not something this change
   alters.
   
   _Claude Code on behalf of Claus Ibsen_
   
   🤖 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]

Reply via email to