XmlMapper serializes jsonAppend property twice · FasterXML/jackson-databind · Discussion #3806 (original) (raw)
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("foobar",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>