|
| 1 | +package com.fasterxml.jackson.dataformat.xml.failing; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.util.List; |
| 5 | + |
| 6 | +import com.fasterxml.jackson.annotation.*; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.*; |
| 9 | +import com.fasterxml.jackson.databind.annotation.JsonTypeIdResolver; |
| 10 | +import com.fasterxml.jackson.databind.jsontype.impl.TypeIdResolverBase; |
| 11 | + |
| 12 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 13 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlElementWrapper; |
| 14 | +import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty; |
| 15 | + |
| 16 | +public class PolymorphicList426Test extends XmlTestBase |
| 17 | +{ |
| 18 | + static class Auto { |
| 19 | + @JacksonXmlProperty(localName = "Object") |
| 20 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 21 | +// @JsonInclude(JsonInclude.Include.NON_EMPTY) |
| 22 | + public List<CarParts> carParts; |
| 23 | + } |
| 24 | + |
| 25 | + @JsonTypeIdResolver(CarPartsResolver.class) |
| 26 | + @JsonTypeInfo(use = JsonTypeInfo.Id.CUSTOM, include = JsonTypeInfo.As.PROPERTY, property = "type") |
| 27 | + abstract static class CarParts { |
| 28 | + @JacksonXmlProperty(isAttribute = true) // The is triggering the issue. |
| 29 | + public String uid; |
| 30 | + |
| 31 | + @JacksonXmlProperty(localName = "Object") |
| 32 | + @JacksonXmlElementWrapper(useWrapping = false) |
| 33 | +// @JsonInclude(JsonInclude.Include.NON_EMPTY) |
| 34 | + public List<CarParts> carParts; |
| 35 | + } |
| 36 | + |
| 37 | + static class Engine extends CarParts{} |
| 38 | + static class Chassis extends CarParts{} |
| 39 | + static class Motor extends CarParts{} |
| 40 | + static class Body extends CarParts{} |
| 41 | + |
| 42 | + static class CarPartsResolver extends TypeIdResolverBase { |
| 43 | + private JavaType superType; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void init(JavaType javaType) { |
| 47 | + this.superType = javaType; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String idFromValue(Object o) { |
| 52 | + return idFromValueAndType(o, o.getClass()); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public String idFromValueAndType(Object o, Class<?> aClass) { |
| 57 | + return aClass.getSimpleName(); |
| 58 | + } |
| 59 | + |
| 60 | + @Override |
| 61 | + public JavaType typeFromId(DatabindContext context, String id) throws IOException { |
| 62 | + Class<?> subType = null; |
| 63 | + switch (id) { |
| 64 | + case "Engine": subType = Engine.class; break; |
| 65 | + case "Chassis": subType = Chassis.class; break; |
| 66 | + case "Motor": subType = Motor.class; break; |
| 67 | + case "Body": subType = Body.class; break; |
| 68 | + } |
| 69 | + return context.constructSpecializedType(superType, subType); |
| 70 | + } |
| 71 | + |
| 72 | + @Override |
| 73 | + public JsonTypeInfo.Id getMechanism() { |
| 74 | + return JsonTypeInfo.Id.CUSTOM; |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + /* |
| 79 | + /******************************************************** |
| 80 | + /* Test methods |
| 81 | + /******************************************************** |
| 82 | + */ |
| 83 | + |
| 84 | + private final ObjectMapper MAPPER = newMapper(); |
| 85 | + |
| 86 | + // [dataformat-xml#426] |
| 87 | + public void testPolymorphicList426() throws Exception |
| 88 | + { |
| 89 | + String xml = "" + |
| 90 | + "<Auto>\n" + |
| 91 | + " <Object uid=\"1\" type=\"Engine\">\n" + |
| 92 | + " <Object uid=\"2\" type=\"Chassis\"></Object>\n" + |
| 93 | + " <Object uid=\"3\" type=\"Motor\"></Object>\n" + |
| 94 | + " </Object>\n" + |
| 95 | + " <Object uid=\"4\" type=\"Body\"></Object>\n" + |
| 96 | + "</Auto>"; |
| 97 | + Auto result = MAPPER.readValue(xml, Auto.class); |
| 98 | + assertNotNull(result); |
| 99 | + assertNotNull(result.carParts); |
| 100 | + assertEquals(2, result.carParts.size()); |
| 101 | + CarParts cp = result.carParts.get(0); |
| 102 | + |
| 103 | + // for debugging: |
| 104 | +//System.err.println("XML:\n"+MAPPER.writeValueAsString(result)); |
| 105 | + |
| 106 | + assertNotNull(cp); |
| 107 | + assertNotNull(cp.carParts); |
| 108 | + |
| 109 | + // So far so good, but fails here: |
| 110 | + assertEquals(2, cp.carParts.size()); |
| 111 | + } |
| 112 | +} |
0 commit comments