Copilot commented on code in PR #2949:
URL: https://github.com/apache/groovy/pull/2949#discussion_r4209783466


##########
subprojects/groovy-xml/src/main/java/org/apache/groovy/xml/util/JacksonHelper.java:
##########
@@ -55,22 +59,39 @@ public static <T> T convertMapToType(Map<String, Object> 
map, Class<T> type) {
             return type.cast(cachedConvertValue.invoke(cachedMapper, map, 
type));
         } catch (XmlRuntimeException e) {
             throw e;
+        } catch (InvocationTargetException e) {
+            // report Jackson's own exception rather than the reflective 
wrapper
+            throw new XmlRuntimeException("Failed to convert XML map to " + 
type.getName(), e.getCause());
         } catch (Exception e) {
             throw new XmlRuntimeException("Failed to convert XML map to " + 
type.getName(), e);
         }
     }
 
     private static synchronized void initMapper() {
         if (cachedMapper != null) return;
-        Class<?> omClass = loadOptionalClass(OBJECT_MAPPER_CLASS);
-        if (omClass == null) {
-            throw new XmlRuntimeException(
-                    "Typed XML parsing requires jackson-databind on the 
classpath. "
-                            + "Add com.fasterxml.jackson.core:jackson-databind 
to your dependencies.");
-        }
+        Object mapper;
         try {
-            cachedMapper = omClass.getDeclaredConstructor().newInstance();
-            cachedConvertValue = omClass.getMethod("convertValue", 
Object.class, Class.class);
+            Class<?> jsonMapperClass = 
loadOptionalClass(JACKSON3_JSON_MAPPER_CLASS);
+            if (jsonMapperClass != null) {
+                // Jackson 2's defaults (for example failing on an unknown 
property), so a typed
+                // parse behaves the same whichever Jackson version the 
application carries
+                Object builder = 
jsonMapperClass.getMethod("builderWithJackson2Defaults").invoke(null);
+                mapper = builder.getClass().getMethod("build").invoke(builder);
+            } else {
+                Class<?> objectMapperClass = 
loadOptionalClass(JACKSON2_OBJECT_MAPPER_CLASS);

Review Comment:
   The newly documented Jackson 2 fallback is no longer exercised: `groovy-xml` 
now runs this suite only with Jackson 3, so every test takes this branch and 
the reflective Jackson 2 path can regress unnoticed. Please add a separate 
Jackson-2-only test configuration/classloader case that verifies successful 
conversion and unknown-property rejection without Jackson 3 present.



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