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