Skip to content

Issue#233 XmlMapper.copy() doesn't properly copy internal configurations #234

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 18, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 17 additions & 10 deletions src/main/java/com/fasterxml/jackson/dataformat/xml/XmlMapper.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,23 @@
package com.fasterxml.jackson.dataformat.xml;

import java.io.IOException;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.PrettyPrinter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do not reformat code in places where you don't change it.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry about that, IntelliJ auto-formatted it, I'll revert those

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.AnnotationIntrospector;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.xml.deser.FromXmlParser;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;
import com.fasterxml.jackson.dataformat.xml.ser.XmlSerializerProvider;
import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter;
import com.fasterxml.jackson.dataformat.xml.util.XmlRootNameLookup;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;
import java.io.IOException;

/**
* Customized {@link ObjectMapper} that will read and write XML instead of JSON,
* using XML-backed {@link com.fasterxml.jackson.core.JsonFactory}
Expand Down Expand Up @@ -86,11 +88,16 @@ public XmlMapper(XmlFactory xmlFactory, JacksonXmlModule module)
_serializationConfig = _serializationConfig.withDefaultPrettyPrinter(DEFAULT_XML_PRETTY_PRINTER);
}

protected XmlMapper(XmlMapper mapper) {
super(mapper);
this._xmlModule = mapper._xmlModule;
}

@Override
public XmlMapper copy()
{
_checkInvalidCopy(XmlMapper.class);
return new XmlMapper((XmlFactory) _jsonFactory.copy(), _xmlModule);
return new XmlMapper(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,23 +1,22 @@
package com.fasterxml.jackson.dataformat.xml.ser;

import java.io.IOException;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;

import com.fasterxml.jackson.core.*;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.PropertyName;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.databind.ser.DefaultSerializerProvider;
import com.fasterxml.jackson.databind.ser.SerializerFactory;
import com.fasterxml.jackson.databind.util.TokenBuffer;
import com.fasterxml.jackson.dataformat.xml.util.StaxUtil;
import com.fasterxml.jackson.dataformat.xml.util.TypeUtil;
import com.fasterxml.jackson.dataformat.xml.util.XmlRootNameLookup;

import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamException;
import java.io.IOException;

/**
* We need to override some parts of
* {@link com.fasterxml.jackson.databind.SerializerProvider}
Expand Down Expand Up @@ -55,6 +54,11 @@ public XmlSerializerProvider(XmlSerializerProvider src,
/**********************************************************************
*/

@Override
public DefaultSerializerProvider copy() {
return this;
Copy link
Contributor Author

@gtrog gtrog May 16, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can add a comment if needed, but I'm doing this because XmlSerializerProvider is effectively immutable in that its only member variable is also immutable, and so I can avoid having to try to figure out whether I should construct it with just an instance of XmlRootNameLookup or with an instance of itself and SerializationConfig and SerializerFactory

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A brief comment would be appreciated... however, are you sure this is safe? Keep in mind that it extends DefaultSerializerProvider so I think it would be better to create a copy (super-class single-arg constructor to use).

}

@Override
public DefaultSerializerProvider createInstance(SerializationConfig config,
SerializerFactory jsf) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
package com.fasterxml.jackson.dataformat.xml;

import java.io.*;

import com.fasterxml.jackson.core.Version;
import com.fasterxml.jackson.core.Versioned;
import com.fasterxml.jackson.dataformat.xml.PackageVersion;
import com.fasterxml.jackson.dataformat.xml.XmlFactory;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.databind.SerializationConfig;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.ser.ToXmlGenerator;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class VersionInfoTest extends XmlTestBase
{
public void testMapperVersions()
Expand All @@ -24,7 +26,8 @@ public void testMapperCopy()
XmlMapper mapper1 = new XmlMapper();
mapper1.setXMLTextElementName("foo");
mapper1.configure(ToXmlGenerator.Feature.WRITE_XML_DECLARATION, true);

mapper1.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);

XmlMapper mapper2 = mapper1.copy();
assertNotSame(mapper1, mapper2);
XmlFactory xf1 = mapper1.getFactory();
Expand All @@ -36,6 +39,16 @@ public void testMapperCopy()
assertEquals(xf1.getXMLTextElementName(), xf2.getXMLTextElementName());
assertEquals(xf1._xmlGeneratorFeatures, xf2._xmlGeneratorFeatures);
assertEquals(xf1._xmlParserFeatures, xf2._xmlParserFeatures);

// and [Issue#233]
SerializationConfig sc1 = mapper1.getSerializationConfig();
SerializationConfig sc2 = mapper2.getSerializationConfig();
assertNotSame(sc1, sc2);
assertEquals(
"serialization features did not get copied",
sc1.getSerializationFeatures(),
sc2.getSerializationFeatures()
);
}

// Another test for [Issue#48]
Expand Down