davsclaus commented on code in PR #30338:
URL: https://github.com/apache/camel/pull/30338#discussion_r4620070112


##########
core/camel-main/src/main/java/org/apache/camel/main/MainHelper.java:
##########
@@ -501,7 +502,7 @@ public static PropertyConfigurer 
configurerByName(CamelContext context, String o
             return null;
         }
         PropertyConfigurer configurer
-                = 
PluginHelper.getConfigurerResolver(context).resolvePropertyConfigurer(name + 
"-" + kind, context);
+                = 
PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, name + "-" + 
kind, target);
         // the configurer that PackageDataFormatMojo or PackageLanguageMojo 
generates for a type is named after it
         if (configurer != null && 
configurer.getClass().getName().equals(target.getClass().getName() + 
"Configurer")) {
             return configurer;

Review Comment:
   Kept as is. The exact class name check comes from CAMEL-26297: 
`configurerByName` returns a configurer only when it is the one generated for 
the class of the target, and otherwise returns `null`. The caller then binds 
the `camel.dataformat.<name>.*` / `camel.language.<name>.*` options through the 
default lookup, which is 
`PropertyConfigurerHelper.resolvePropertyConfigurer(context, target)` and also 
uses the new hierarchy-aware helper. So a configurer generated for a supertype 
is not lost: it is found on that path. jamesnetherton checked the same in his 
review.
   
   _Claude Code on behalf of davsclaus_



##########
components/camel-kamelet/src/test/java/org/apache/camel/component/kamelet/KameletJacksonBothLinesTest.java:
##########
@@ -0,0 +1,163 @@
+/*
+ * 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.component.kamelet;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UncheckedIOException;
+import java.net.URL;
+import java.util.Enumeration;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.RoutesBuilder;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.jackson3.JacksonDataFormat;
+import org.apache.camel.component.jackson3.JacksonDataFormatConfigurer;
+import org.apache.camel.impl.engine.DefaultClassResolver;
+import org.apache.camel.model.dataformat.JsonLibrary;
+import org.apache.camel.spi.ConfigurerResolver;
+import org.apache.camel.support.PluginHelper;
+import org.apache.camel.support.PropertyConfigurerHelper;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+/**
+ * camel-jackson (Jackson 2.x) and camel-jackson3 (Jackson 3.x) register the 
same configurer name, jackson-dataformat.
+ * With both on the classpath, as when a Kamelet adds camel-jackson to an 
application with camel-jackson3, Camel creates
+ * the Jackson 3.x data format for the name jackson, while the configurer 
found by that name is the one of the JAR first
+ * on the classpath (CAMEL-27164). This test puts camel-jackson first: the 
options of the data format must still be set
+ * with the configurer of the Jackson 3.x data format.
+ */
+public class KameletJacksonBothLinesTest extends CamelTestSupport {
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+        context.setClassResolver(new 
Jackson2ConfigurersFirstClassResolver(context));
+        // a data format configured on the context (as with 
camel.dataformat.jackson.* in Camel Main), whose options
+        // the data formats in the routes take
+        JacksonDataFormat configured = (JacksonDataFormat) 
context.resolveDataFormat("jackson");
+        configured.setPrettyPrint(true);
+        return context;
+    }
+
+    @Test
+    public void theConfigurerIsTheOneOfTheDataFormat() {
+        
assertThat(PluginHelper.getConfigurerResolver(context).resolvePropertyConfigurer("jackson-dataformat",
 context))
+                .as("the configurer of camel-jackson is found first by name")
+                
.isInstanceOf(org.apache.camel.component.jackson.JacksonDataFormatConfigurer.class);
+
+        JacksonDataFormat dataFormat = new JacksonDataFormat();
+        
assertThat(PropertyConfigurerHelper.resolvePropertyConfigurerByName(context, 
"jackson-dataformat", dataFormat))
+                .isInstanceOf(JacksonDataFormatConfigurer.class);
+        assertThat(PropertyConfigurerHelper.resolvePropertyConfigurer(context, 
dataFormat))
+                .isInstanceOf(JacksonDataFormatConfigurer.class);
+    }
+
+    @Test
+    public void unmarshalType() {
+        Object body = template.requestBody("direct:unmarshal", 
"{\"name\":\"Camel\"}");
+
+        assertThat(body).isInstanceOfSatisfying(Greeting.class, g -> 
assertThat(g.getName()).isEqualTo("Camel"));
+    }
+
+    @Test
+    public void optionsOfTheDataFormatConfiguredOnTheContext() {
+        Greeting greeting = new Greeting();
+        greeting.setName("Camel");
+
+        String json = template.requestBody("direct:marshal", greeting, 
String.class);
+
+        assertThat(json).isEqualTo("{%s  \"name\" : \"Camel\"%s}", 
System.lineSeparator(), System.lineSeparator());
+    }
+
+    @Override
+    protected RoutesBuilder createRouteBuilder() {
+        return new RouteBuilder() {
+            @Override
+            public void configure() {
+                routeTemplate("json-to-greeting")
+                        .from("kamelet:source")
+                        .unmarshal().json(JsonLibrary.Jackson, Greeting.class);
+
+                routeTemplate("greeting-to-json")
+                        .from("kamelet:source")
+                        .marshal().json(JsonLibrary.Jackson);
+
+                from("direct:unmarshal").kamelet("json-to-greeting");
+                from("direct:marshal").kamelet("greeting-to-json");
+            }
+        };
+    }
+
+    public static class Greeting {
+        private String name;
+
+        public String getName() {
+            return name;
+        }
+
+        public void setName(String name) {
+            this.name = name;
+        }
+    }
+
+    /**
+     * Finds the configurers that camel-jackson registers before those of 
camel-jackson3, as when the JAR of
+     * camel-jackson comes first on the classpath.
+     */
+    private static final class Jackson2ConfigurersFirstClassResolver extends 
DefaultClassResolver {
+
+        private Jackson2ConfigurersFirstClassResolver(CamelContext 
camelContext) {
+            super(camelContext);
+        }
+
+        @Override
+        public InputStream loadResourceAsStream(String uri) {
+            if (uri.startsWith(ConfigurerResolver.RESOURCE_PATH)) {
+                URL url = findInJackson2(uri);
+                if (url != null) {
+                    try {
+                        return url.openStream();
+                    } catch (IOException e) {
+                        throw new UncheckedIOException(e);
+                    }
+                }
+            }
+            return super.loadResourceAsStream(uri);
+        }
+
+        private static URL findInJackson2(String uri) {
+            String jar = 
org.apache.camel.component.jackson.JacksonDataFormat.class.getProtectionDomain().getCodeSource()
+                    .getLocation().toString();

Review Comment:
   Fixed in b4c665c494b0: `findInJackson2` returns `null` when the code source 
is `null`, and the class resolver then falls back to the normal resource lookup.
   
   _Claude Code on behalf of davsclaus_



##########
core/camel-support/src/main/java/org/apache/camel/support/PropertyConfigurerHelper.java:
##########
@@ -103,6 +108,75 @@ public static PropertyConfigurer 
resolvePropertyConfigurer(CamelContext context,
         return null;
     }
 
+    /**
+     * Resolves the configurer of a component, data format or language by its 
name (such as jackson-dataformat), and
+     * makes sure it is the configurer of the given target.
+     * <p>
+     * Two JARs can register a configurer with the same name, and then the one 
first on the classpath is found. For
+     * example both camel-jackson (Jackson 2.x) and camel-jackson3 (Jackson 
3.x) register jackson-dataformat, while the
+     * jackson data format is created from camel-jackson3 when both are on the 
classpath. When the configurer found by
+     * name is generated for another type than the target, the configurer 
generated for the type of the target is used
+     * instead.
+     *
+     * @param  context the camel context
+     * @param  name    the name of the configurer, such as jackson-dataformat
+     * @param  target  the component, data format or language to configure, or 
<code>null</code> to only resolve the
+     *                 configurer by its name
+     * @return         the resolved configurer, or <code>null</code> if no 
configurer could be found
+     */
+    public static PropertyConfigurer 
resolvePropertyConfigurerByName(CamelContext context, String name, Object 
target) {
+        return resolvePropertyConfigurerByName(context, name, target != null ? 
target.getClass() : null);
+    }
+
+    private static PropertyConfigurer resolvePropertyConfigurerByName(

Review Comment:
   Done in b4c665c494b0: the private overload is now 
`resolvePropertyConfigurerByNameForType`.
   
   _Claude Code on behalf of davsclaus_



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