Skip to content

Commit

Permalink
Fix possibility to process references on enum types at deserialization.
Browse files Browse the repository at this point in the history
Closes x-stream#238.
  • Loading branch information
joehni committed Jan 6, 2021
1 parent fabe3ab commit d40e747
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 8 deletions.
1 change: 1 addition & 0 deletions xstream-distribution/src/content/changes.html
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ <h1 id="upcoming-1.4.x">Upcoming 1.4.x maintenance release</h1>
<h2>Minor changes</h2>

<ul>
<li>GHI:#238: Fix possibility to process references on enum types at deserialization.</li>
<li>GHI:#237: Fix optimization in XmlFriendlyNameCoder.</li>
</ul>

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2009, 2014, 2015 XStream Committers.
* Copyright (C) 2006, 2007, 2009, 2014, 2015, 2021 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand Down Expand Up @@ -70,8 +70,8 @@ public boolean isImmutableValueType(final Class<?> type) {

@Override
public boolean isReferenceable(final Class<?> type) {
if (unreferenceableTypes.contains(type)) {
return false;
if (immutableTypes.contains(type)) {
return !unreferenceableTypes.contains(type);
} else {
return super.isReferenceable(type);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2004, 2005 Joe Walnes.
* Copyright (C) 2006, 2007, 2009, 2010, 2011, 2014, 2015, 2018 XStream Committers.
* Copyright (C) 2006, 2007, 2009, 2010, 2011, 2014, 2015, 2018, 2021 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand All @@ -25,6 +25,8 @@
import com.thoughtworks.acceptance.objects.StandardObject;
import com.thoughtworks.acceptance.someobjects.WithNamedList;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.basic.AbstractSingleValueConverter;
import com.thoughtworks.xstream.converters.enums.SimpleEnum;
import com.thoughtworks.xstream.core.AbstractReferenceMarshaller;


Expand Down Expand Up @@ -546,9 +548,59 @@ public void testImmutableInstancesCanBeDereferenced() {

final Thing t0 = result.get(0);
final Thing t1 = result.get(1);
result.get(2);
final Thing t2 = result.get(2);

assertEquals(t0, t1);
assertSame(t0, t1);
assertNotSame(t0, t2);
assertEquals(t0, t2);
}

private static class ThingConverter extends AbstractSingleValueConverter {

@Override
public boolean canConvert(final Class<?> type) {
return type == Thing.class;
}

@Override
public Object fromString(final String str) {
throw new UnsupportedOperationException();
}

@Override
public String toString(final Object obj) {
return ((Thing)obj).field;
}
}

public void testImmutableEnumInstancesCanBeDereferenced() {

final Thing green = new Thing("GREEN");
final List<Thing> list = new ArrayList<>();
list.add(green);
list.add(green);

xstream.allowTypes(SimpleEnum.class);
xstream.alias("simple", Thing.class);
xstream.registerConverter(new ThingConverter());
final String xml = xstream.toXML(list);

xstream.alias("simple", SimpleEnum.class);

try {
xstream.fromXML(xml);
fail("Thrown " + ConversionException.class.getName() + " expected");
} catch (final ConversionException e) {
assertEquals(SimpleEnum.class.getName(), e.get("referenced-type"));
}

xstream.addImmutableType(SimpleEnum.class, true);

final List<SimpleEnum> result = xstream.fromXML(xml);

final SimpleEnum se0 = result.get(0);
final SimpleEnum se1 = result.get(1);

assertSame(se0, se1);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2005 Joe Walnes.
* Copyright (C) 2006, 2007 XStream Committers.
* Copyright (C) 2006, 2007, 2021 XStream Committers.
* All rights reserved.
*
* The software in this package is published under the terms of the BSD
Expand All @@ -11,6 +11,6 @@
*/
package com.thoughtworks.xstream.converters.enums;

enum SimpleEnum {
public enum SimpleEnum {
RED, GREEN, BLUE;
}

0 comments on commit d40e747

Please sign in to comment.