|
| 1 | +package tools.jackson.dataformat.xml.failing; |
| 2 | + |
| 3 | +import tools.jackson.core.JsonGenerator; |
| 4 | + |
| 5 | +import tools.jackson.databind.*; |
| 6 | +import tools.jackson.databind.annotation.JsonAppend; |
| 7 | +import tools.jackson.databind.cfg.MapperConfig; |
| 8 | +import tools.jackson.databind.introspect.AnnotatedClass; |
| 9 | +import tools.jackson.databind.introspect.BeanPropertyDefinition; |
| 10 | +import tools.jackson.databind.ser.VirtualBeanPropertyWriter; |
| 11 | +import tools.jackson.databind.util.Annotations; |
| 12 | +import tools.jackson.dataformat.xml.XmlMapper; |
| 13 | +import tools.jackson.dataformat.xml.XmlTestBase; |
| 14 | + |
| 15 | +public class JsonAppend508Test extends XmlTestBase |
| 16 | +{ |
| 17 | + // [dataformat-xml#508]: Duplication of virtual properties |
| 18 | + @JsonAppend(props = @JsonAppend.Prop(name = "virtual", value = MyVirtualPropertyWriter.class)) |
| 19 | + public static class Pojo508 { |
| 20 | + private final String name; |
| 21 | + |
| 22 | + public Pojo508(String name) { |
| 23 | + this.name = name; |
| 24 | + } |
| 25 | + public String getName() { |
| 26 | + return name; |
| 27 | + } |
| 28 | + } |
| 29 | + |
| 30 | + static class MyVirtualPropertyWriter extends VirtualBeanPropertyWriter { |
| 31 | + private static final long serialVersionUID = 1L; |
| 32 | + |
| 33 | + public MyVirtualPropertyWriter() {} |
| 34 | + |
| 35 | + protected MyVirtualPropertyWriter(BeanPropertyDefinition propDef, Annotations contextAnnotations, |
| 36 | + JavaType declaredType) { |
| 37 | + super(propDef, contextAnnotations, declaredType); |
| 38 | + } |
| 39 | + |
| 40 | + @Override |
| 41 | + protected Object value(Object bean, JsonGenerator g, SerializerProvider prov) { |
| 42 | + return "bar"; |
| 43 | + } |
| 44 | + |
| 45 | + @Override |
| 46 | + public VirtualBeanPropertyWriter withConfig(MapperConfig<?> config, AnnotatedClass declaringClass, |
| 47 | + BeanPropertyDefinition propDef, JavaType type) { |
| 48 | + return new MyVirtualPropertyWriter(propDef, declaringClass.getAnnotations(), type); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + private final XmlMapper MAPPER = newMapper(); |
| 53 | + |
| 54 | + // [dataformat-xml#508]: Duplication of virtual properties |
| 55 | + public void testJsonAppend() throws Exception { |
| 56 | + String xml = MAPPER.writeValueAsString(new Pojo508("foo")); |
| 57 | + assertEquals("<Pojo><name>foo</name><virtual>bar</virtual></Pojo>",xml); |
| 58 | + } |
| 59 | +} |
0 commit comments