|
| 1 | +package com.fasterxml.jackson.dataformat.xml.misc; |
| 2 | + |
| 3 | +import java.lang.annotation.Retention; |
| 4 | +import java.lang.annotation.RetentionPolicy; |
| 5 | +import java.util.Arrays; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import com.fasterxml.jackson.databind.PropertyName; |
| 9 | +import com.fasterxml.jackson.databind.introspect.Annotated; |
| 10 | +import com.fasterxml.jackson.databind.introspect.NopAnnotationIntrospector; |
| 11 | +import com.fasterxml.jackson.databind.module.SimpleModule; |
| 12 | +import com.fasterxml.jackson.dataformat.xml.XmlMapper; |
| 13 | +import com.fasterxml.jackson.dataformat.xml.XmlTestBase; |
| 14 | + |
| 15 | +/** |
| 16 | + * A regression test for https://github.com/FasterXML/jackson-databind/issues/4595 |
| 17 | + */ |
| 18 | +public class CustomAnnotationIntrospectorNoWrapperTest extends XmlTestBase |
| 19 | +{ |
| 20 | + public static class Foo { |
| 21 | + private final List<String> bar; |
| 22 | + |
| 23 | + public Foo(List<String> bar) { |
| 24 | + this.bar = bar; |
| 25 | + } |
| 26 | + |
| 27 | + @NoWrapper |
| 28 | + public List<String> getBar() { |
| 29 | + return bar; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + public static class NoWrapperIntrospector extends NopAnnotationIntrospector { |
| 34 | + private static final long serialVersionUID = 1L; |
| 35 | + |
| 36 | + @Override |
| 37 | + public PropertyName findWrapperName(Annotated ann) { |
| 38 | + if (ann.hasAnnotation(NoWrapper.class)) { |
| 39 | + return PropertyName.NO_NAME; |
| 40 | + } |
| 41 | + return super.findWrapperName(ann); |
| 42 | + } |
| 43 | + } |
| 44 | + |
| 45 | + @Retention(RetentionPolicy.RUNTIME) |
| 46 | + public @interface NoWrapper { |
| 47 | + } |
| 48 | + |
| 49 | + private final XmlMapper VANILLA_MAPPER = newMapper(); |
| 50 | + |
| 51 | + public void testNoWrapper() throws Exception { |
| 52 | + Foo foo = new Foo(Arrays.asList("Value1", "Value2")); |
| 53 | + |
| 54 | + assertEquals("<Foo><bar><bar>Value1</bar><bar>Value2</bar></bar></Foo>", |
| 55 | + VANILLA_MAPPER.writeValueAsString(foo)); |
| 56 | + |
| 57 | + XmlMapper customMapper = mapperBuilder() |
| 58 | + .addModule(new SimpleModule("NoWrapperModule") { |
| 59 | + private static final long serialVersionUID = 1L; |
| 60 | + |
| 61 | + @Override |
| 62 | + public void setupModule(SetupContext context) { |
| 63 | + context.insertAnnotationIntrospector(new NoWrapperIntrospector()); |
| 64 | + super.setupModule(context); |
| 65 | + } |
| 66 | + }).build(); |
| 67 | + |
| 68 | + // After fixing https://github.com/FasterXML/jackson-databind/issues/4595 |
| 69 | + assertEquals("<Foo><bar>Value1</bar><bar>Value2</bar></Foo>", customMapper.writeValueAsString(foo)); |
| 70 | + |
| 71 | + // before fix had: |
| 72 | + //assertEquals("<Foo><bar><bar>Value1</bar><bar>Value2</bar></bar></Foo>", customMapper.writeValueAsString(foo)); |
| 73 | + } |
| 74 | +} |
0 commit comments