From c7e259ecdee4ec4cf723bfd1eb073a9473d7ef88 Mon Sep 17 00:00:00 2001 From: Vincent Latombe Date: Sun, 10 Oct 2021 11:08:16 +0200 Subject: [PATCH] Fix support of implicit collections when using Xstream2 (#5757) --- .../util/RobustReflectionConverter.java | 3 ++- .../util/RobustReflectionConverterTest.java | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/hudson/util/RobustReflectionConverter.java b/core/src/main/java/hudson/util/RobustReflectionConverter.java index 4192ea7b53..96b10f6ef9 100644 --- a/core/src/main/java/hudson/util/RobustReflectionConverter.java +++ b/core/src/main/java/hudson/util/RobustReflectionConverter.java @@ -506,9 +506,9 @@ public class RobustReflectionConverter implements Converter { private Class determineType(HierarchicalStreamReader reader, boolean validField, Object result, String fieldName, Class definedInCls) { String classAttribute = reader.getAttribute(mapper.aliasForAttribute("class")); - Class fieldType = reflectionProvider.getFieldType(result, fieldName, definedInCls); if (classAttribute != null) { Class specifiedType = mapper.realClass(classAttribute); + Class fieldType = reflectionProvider.getFieldType(result, fieldName, definedInCls); if(fieldType.isAssignableFrom(specifiedType)) // make sure that the specified type in XML is compatible with the field type. // this allows the code to evolve in more flexible way. @@ -522,6 +522,7 @@ public class RobustReflectionConverter implements Converter { return mapper.realClass(reader.getNodeName()); } } else { + Class fieldType = reflectionProvider.getFieldType(result, fieldName, definedInCls); return mapper.defaultImplementationOf(fieldType); } } diff --git a/core/src/test/java/hudson/util/RobustReflectionConverterTest.java b/core/src/test/java/hudson/util/RobustReflectionConverterTest.java index 08c34df29d..2ef437512a 100644 --- a/core/src/test/java/hudson/util/RobustReflectionConverterTest.java +++ b/core/src/test/java/hudson/util/RobustReflectionConverterTest.java @@ -32,8 +32,10 @@ import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.converters.ConversionException; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; +import org.hamcrest.Matchers; import org.junit.Test; /** @@ -100,6 +102,23 @@ public class RobustReflectionConverterTest { assertEquals(Billy.class, s.lover.getClass()); } + @Test + public void implicitCollection() { + XStream2 xs = new XStream2(); + xs.alias("hold", Hold.class); + xs.addImplicitCollection(Hold.class,"items", "item", String.class); + Hold h = (Hold) xs.fromXML("ab"); + assertThat(h.items, Matchers.containsInAnyOrder("a", "b")); + assertEquals("\n" + + " a\n" + + " b\n" + + "", xs.toXML(h)); + } + + public static class Hold { + List items; + } + @Retention(RetentionPolicy.RUNTIME) @interface Owner {String value();} public static class Projekt { Bild[] bildz;