-
-
Notifications
You must be signed in to change notification settings - Fork 221
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
76 changes: 76 additions & 0 deletions
76
...est/java/tools/jackson/dataformat/xml/misc/CustomAnnotationIntrospectorNoWrapperTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
package tools.jackson.dataformat.xml.misc; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import tools.jackson.databind.PropertyName; | ||
import tools.jackson.databind.cfg.MapperConfig; | ||
import tools.jackson.databind.introspect.Annotated; | ||
import tools.jackson.databind.introspect.NopAnnotationIntrospector; | ||
import tools.jackson.databind.module.SimpleModule; | ||
|
||
import tools.jackson.dataformat.xml.XmlMapper; | ||
import tools.jackson.dataformat.xml.XmlTestBase; | ||
|
||
/** | ||
* A regression test for https://github.com/FasterXML/jackson-databind/issues/4595 | ||
*/ | ||
public class CustomAnnotationIntrospectorNoWrapperTest extends XmlTestBase | ||
{ | ||
public static class Foo { | ||
private final List<String> bar; | ||
|
||
public Foo(List<String> bar) { | ||
this.bar = bar; | ||
} | ||
|
||
@NoWrapper | ||
public List<String> getBar() { | ||
return bar; | ||
} | ||
} | ||
|
||
public static class NoWrapperIntrospector extends NopAnnotationIntrospector { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public PropertyName findWrapperName(MapperConfig<?> config, Annotated ann) { | ||
if (ann.hasAnnotation(NoWrapper.class)) { | ||
return PropertyName.NO_NAME; | ||
} | ||
return super.findWrapperName(config, ann); | ||
} | ||
} | ||
|
||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface NoWrapper { | ||
} | ||
|
||
private final XmlMapper VANILLA_MAPPER = newMapper(); | ||
|
||
public void testNoWrapper() throws Exception { | ||
Foo foo = new Foo(Arrays.asList("Value1", "Value2")); | ||
|
||
assertEquals("<Foo><bar><bar>Value1</bar><bar>Value2</bar></bar></Foo>", | ||
VANILLA_MAPPER.writeValueAsString(foo)); | ||
|
||
XmlMapper customMapper = mapperBuilder() | ||
.addModule(new SimpleModule("NoWrapperModule") { | ||
private static final long serialVersionUID = 1L; | ||
|
||
@Override | ||
public void setupModule(SetupContext context) { | ||
context.insertAnnotationIntrospector(new NoWrapperIntrospector()); | ||
super.setupModule(context); | ||
} | ||
}).build(); | ||
|
||
// After fixing https://github.com/FasterXML/jackson-databind/issues/4595 | ||
assertEquals("<Foo><bar>Value1</bar><bar>Value2</bar></Foo>", customMapper.writeValueAsString(foo)); | ||
|
||
// before fix had: | ||
//assertEquals("<Foo><bar><bar>Value1</bar><bar>Value2</bar></bar></Foo>", customMapper.writeValueAsString(foo)); | ||
} | ||
} |