codeconsole commented on code in PR #2949:
URL: https://github.com/apache/groovy/pull/2949#discussion_r4308554932
##########
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:
Added in a19601a: a `jackson2Test` task in groovy-xml, part of `check`,
reruns `XmlParserTypedTest` on the test runtime classpath with Jackson 3
swapped for Jackson 2.22.3 (the two classpaths differ only in the Jackson
artifacts). That covers successful conversion and unknown-element rejection
through the reflective fallback. `testRunsAgainstTheExpectedJackson` asserts
which Jackson each run sees, so the task fails instead of silently retesting
Jackson 3 if the swap breaks; removing the exclusion makes it fail.
--
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]