XmlMapper serializes @JsonAppend property twice · Issue #578 · FasterXML/jackson-dataformat-xml (original) (raw)

Discussed in FasterXML/jackson-databind#3806

Originally posted by stepince March 5, 2023
XmlMapper is serializing jsonAppend virtual property twice. ObjectMapper for json works correctly.

jackson version: 2.14.1

public class VirtualBeanPropertyWriterTest {
    @Test
    public void testJsonAppend() throws Exception {
        ObjectMapper mapper = new XmlMapper();
        String xml = mapper.writeValueAsString(new Pojo("foo"));
        assertEquals("<Pojo><name>foo</name><virtual>bar</virtual></Pojo>",xml);
    }

    @JsonAppend(props = @JsonAppend.Prop(name = "virtual", value = MyVirtualPropertyWriter.class))
    public static class Pojo {
        private final String name;

        public Pojo(String name) {
            this.name = name;
        }
        public String getName() {
            return name;
        }
    }

    public static class MyVirtualPropertyWriter extends VirtualBeanPropertyWriter {
        public MyVirtualPropertyWriter() {}

        protected MyVirtualPropertyWriter(BeanPropertyDefinition propDef, Annotations contextAnnotations,
                                          JavaType declaredType) {
            super(propDef, contextAnnotations, declaredType);
        }

        @Override
        protected Object value(Object bean, JsonGenerator jgen, SerializerProvider prov) throws Exception {
            return "bar";
        }

        @Override
        public VirtualBeanPropertyWriter withConfig(MapperConfig<?> config, AnnotatedClass declaringClass,
                                                    BeanPropertyDefinition propDef, JavaType type) {

            return new MyVirtualPropertyWriter(propDef, declaringClass.getAnnotations(), type);
        }
    }
}

output

org.opentest4j.AssertionFailedError: 
Expected :`<Pojo><name>foo</name><virtual>bar</virtual></Pojo>`
Actual   :`<Pojo><name>foo</name><virtual>bar</virtual><virtual>bar</virtual></Pojo>`
</div>