Skip to content

Commit

Permalink
Add test for failing #325
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Dec 16, 2018
1 parent 8d52926 commit 742524a
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 7 deletions.
Expand Up @@ -10,7 +10,10 @@
import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTypeResolverBuilder;
import com.fasterxml.jackson.databind.ObjectMapper.DefaultTyping;
import com.fasterxml.jackson.databind.cfg.MapperBuilder;
import com.fasterxml.jackson.databind.jsontype.TypeResolverBuilder;
import com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider;
Expand Down Expand Up @@ -185,7 +188,6 @@ public static XmlMapper.Builder xmlBuilder() {
/**
* @since 2.10
*/
@SuppressWarnings("unchecked")
public static XmlMapper.Builder builder() {
return new XmlMapper.Builder(new XmlMapper());
}
Expand All @@ -202,6 +204,19 @@ public Version version() {
return PackageVersion.VERSION;
}

/*
/**********************************************************
/* Factory method overrides
/**********************************************************
*/

@Override
protected TypeResolverBuilder<?> _constructDefaultTypeResolverBuilder(DefaultTyping applicability) {
// return new DefaultTypeResolverBuilder(applicability);
// just a stub for now
return super._constructDefaultTypeResolverBuilder(applicability);
}

/*
/**********************************************************
/* Additional XML-specific configurations
Expand Down
@@ -0,0 +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;

public class DefaultTyping325Test extends XmlTestBase
{
static class Simple {
protected List<String> list;

public List<String> getList( ) { return list; }
public void setList(List<String> l) { list = l; }
}

public void testCanSerialize() throws IOException
{
ObjectMapper mapper = objectMapperBuilder()
.build();
mapper.enableDefaultTyping(ObjectMapper.DefaultTyping.NON_FINAL, JsonTypeInfo.As.WRAPPER_OBJECT);

// construct test object
Simple s = new Simple();
s.setList(Arrays.asList("foo", "bar"));

String doc = mapper.writeValueAsString(s);
System.err.println("DOC: "+doc);
Simple result = mapper.readValue(doc, Simple.class);
assertNotNull(result.list);
assertEquals(2, result.list.size());
}
}
@@ -0,0 +1,80 @@
package com.fasterxml.jackson.dataformat.xml.failing;

import javax.xml.stream.*;

import com.ctc.wstx.stax.WstxInputFactory;
import com.ctc.wstx.stax.WstxOutputFactory;
import com.fasterxml.jackson.databind.*;

import com.fasterxml.jackson.dataformat.xml.*;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlProperty;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;

// for [dataformat-xml#311]
public class FailingNamespace311Test extends XmlTestBase
{
@JacksonXmlRootElement(localName = "new")
static class Bean {
@JacksonXmlProperty(isAttribute = true)
public String source="ECOM";
public Member member;

public Bean(int id, String name) {
this.member=new Member(id, name);
}

public Member getMember() {
return member;
}
}

static class Member {
private final int id;
private final String name;

public Member(int id, String name) {
this.id = id;
this.name = name;
}

public int getId() {
return id;
}

public String getName() {
return name;
}
}

public void testIssue311() throws Exception {
XMLInputFactory xmlInputFactory = new WstxInputFactory();
XMLOutputFactory xmlOutputFactory = new WstxOutputFactory();

/* Setting this to true makes the application run but does not write namespace */
xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false);

XmlMapper mapper = new XmlMapper(xmlInputFactory);
mapper.enable(SerializationFeature.INDENT_OUTPUT);

XMLStreamWriter writer = xmlOutputFactory.createXMLStreamWriter(System.out);

startDocument(writer);
Bean bean=new Bean(1, "Dude");
mapper.writeValue(writer, bean);
endDocument(writer);
}

protected void startDocument(XMLStreamWriter writer) throws XMLStreamException {
writer.writeStartDocument("utf-8", "1.0");
writer.writeCharacters("\n");
writer.setDefaultNamespace("http://eClub.Schemas.ImportContact");
writer.writeStartElement("contacts");
writer.writeDefaultNamespace( "http://eClub.Schemas.ImportContact");
writer.writeCharacters("\n");
}

protected void endDocument(XMLStreamWriter writer) throws XMLStreamException {
writer.writeCharacters("\n");
writer.writeEndDocument();
}
}
Expand Up @@ -11,12 +11,6 @@

public class PolymorphicTypesTest extends XmlTestBase
{
/*
/**********************************************************
/* Helper types
/**********************************************************
*/

@JsonTypeInfo(use=JsonTypeInfo.Id.CLASS, include=JsonTypeInfo.As.PROPERTY)
static class BaseTypeWithClassProperty { }

Expand Down

0 comments on commit 742524a

Please sign in to comment.