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
1 parent
2bc25e4
commit b5ae1e8
Showing
6 changed files
with
118 additions
and
16 deletions.
There are no files selected for viewing
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
83 changes: 83 additions & 0 deletions
83
src/main/java/com/fasterxml/jackson/dataformat/xml/DefaultingXmlTypeResolverBuilder.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,83 @@ | ||
package com.fasterxml.jackson.dataformat.xml; | ||
|
||
import java.util.Collection; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.databind.*; | ||
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping; | ||
import com.fasterxml.jackson.databind.cfg.MapperConfig; | ||
import com.fasterxml.jackson.databind.jsontype.NamedType; | ||
import com.fasterxml.jackson.databind.jsontype.PolymorphicTypeValidator; | ||
import com.fasterxml.jackson.databind.jsontype.TypeIdResolver; | ||
import com.fasterxml.jackson.databind.jsontype.impl.StdTypeResolverBuilder; | ||
|
||
import com.fasterxml.jackson.dataformat.xml.util.StaxUtil; | ||
|
||
/** | ||
* Sub-class of {@code StdTypeResolverBuilder} specifically used with | ||
* Default Typing. | ||
*<p> | ||
* Composition/sub-classing gets quite tricky here: ideally we would just | ||
* extend {@link XmlTypeResolverBuilder} but unfortunately inheritance hierarchy | ||
* does not allow this. | ||
* | ||
* @since 2.10 | ||
*/ | ||
public class DefaultingXmlTypeResolverBuilder | ||
extends ObjectMapper.DefaultTypeResolverBuilder | ||
implements java.io.Serializable | ||
{ | ||
private static final long serialVersionUID = 1L; | ||
|
||
public DefaultingXmlTypeResolverBuilder(DefaultTyping t, PolymorphicTypeValidator ptv) { | ||
super(t, ptv); | ||
} | ||
|
||
/* | ||
/********************************************************************** | ||
/* Methods copied from `XmlTypeResolverBuilder` | ||
/********************************************************************** | ||
*/ | ||
|
||
@Override | ||
public StdTypeResolverBuilder init(JsonTypeInfo.Id idType, TypeIdResolver idRes) | ||
{ | ||
super.init(idType, idRes); | ||
if (_typeProperty != null) { | ||
_typeProperty = StaxUtil.sanitizeXmlTypeName(_typeProperty); | ||
} | ||
return this; | ||
} | ||
|
||
@Override | ||
public StdTypeResolverBuilder typeProperty(String typeIdPropName) | ||
{ | ||
// ok to have null/empty; will restore to use defaults | ||
if (typeIdPropName == null || typeIdPropName.length() == 0) { | ||
typeIdPropName = _idType.getDefaultPropertyName(); | ||
} | ||
_typeProperty = StaxUtil.sanitizeXmlTypeName(typeIdPropName); | ||
return this; | ||
} | ||
|
||
@Override | ||
protected TypeIdResolver idResolver(MapperConfig<?> config, | ||
JavaType baseType, PolymorphicTypeValidator subtypeValidator, | ||
Collection<NamedType> subtypes, boolean forSer, boolean forDeser) | ||
{ | ||
if (_customIdResolver != null) { | ||
return _customIdResolver; | ||
} | ||
// Only override handlers of class, minimal class; name is good as is | ||
switch (_idType) { | ||
case CLASS: | ||
return new XmlTypeResolverBuilder.XmlClassNameIdResolver(baseType, config.getTypeFactory(), | ||
subTypeValidator(config)); | ||
case MINIMAL_CLASS: | ||
return new XmlTypeResolverBuilder.XmlMinimalClassNameIdResolver(baseType, config.getTypeFactory(), | ||
subTypeValidator(config)); | ||
default: | ||
} | ||
return super.idResolver(config, baseType, subtypeValidator, subtypes, forSer, forDeser); | ||
} | ||
} |
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
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
23 changes: 11 additions & 12 deletions
23
src/test/java/com/fasterxml/jackson/dataformat/xml/failing/DefaultTyping325Test.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 |
---|---|---|
@@ -1,37 +1,36 @@ | ||
package com.fasterxml.jackson.dataformat.xml.failing; | ||
|
||
import java.io.IOException; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import com.fasterxml.jackson.annotation.JsonTypeInfo; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
|
||
import com.fasterxml.jackson.dataformat.xml.XmlTestBase; | ||
import com.fasterxml.jackson.dataformat.xml.testutil.NoCheckSubTypeValidator; | ||
|
||
public class DefaultTyping325Test extends XmlTestBase | ||
{ | ||
static class Simple { | ||
protected List<String> list; | ||
static class Simple325 { | ||
protected String[] list; | ||
|
||
public List<String> getList( ) { return list; } | ||
public void setList(List<String> l) { list = l; } | ||
public String[] getList( ) { return list; } | ||
public void setList(String[] l) { list = l; } | ||
} | ||
|
||
public void testCanSerialize() throws IOException | ||
// [dataformat-xml#325] | ||
public void testDefaultTypingWithInnerClass() throws IOException | ||
{ | ||
ObjectMapper mapper = mapperBuilder() | ||
.activateDefaultTyping(NoCheckSubTypeValidator.instance, | ||
ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT) | ||
.build(); | ||
|
||
// construct test object | ||
Simple s = new Simple(); | ||
s.setList(Arrays.asList("foo", "bar")); | ||
Simple325 s = new Simple325(); | ||
s.setList(new String[] { "foo", "bar" }); | ||
|
||
String doc = mapper.writeValueAsString(s); | ||
Simple result = mapper.readValue(doc, Simple.class); | ||
Simple325 result = mapper.readValue(doc, Simple325.class); | ||
assertNotNull(result.list); | ||
assertEquals(2, result.list.size()); | ||
assertEquals(2, result.list.length); | ||
} | ||
} |
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