Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

V2.5 Incorrect deserialization of whitespaces #82

Merged
merged 1 commit into from

2 participants

@almazik

This fix solves the problem of exception appearing during deserialization of object when all its properties are null. See NestedObjectWithNullPropertiesShouldBeSerialized test that fails with "System.InvalidCastException: Unable to cast object of type 'System.String' to type" without this fix.

@andreasohlund andreasohlund merged commit 0a15a44 into Particular:v2.5
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
27 src/impl/Serializers/NServiceBus.Serializers.XML.Test/SerializerTests.cs
@@ -242,6 +242,28 @@ public void TestDataContractSerializer()
Debug.WriteLine("deserializing: " + sw.Elapsed);
}
+ [Test]
+ public void NestedObjectWithNullPropertiesShouldBeSerialized()
+ {
+ var mapper = new MessageMapper();
+ var serializer = new MessageSerializer();
+ serializer.MessageMapper = mapper;
+
+ serializer.MessageTypes = new List<Type> { typeof(MessageWithNestedObject) };
+
+ var msg = new MessageWithNestedObject { NestedObject = new MessageWithListItem() };
+
+ using (var stream = new MemoryStream())
+ {
+ serializer.Serialize(new[] { msg }, stream);
+ stream.Position = 0;
+
+ var msgArray = serializer.Deserialize(stream);
+ var actualMessage = (MessageWithNestedObject)msgArray[0];
+ Assert.IsNotNull(actualMessage.NestedObject);
+ }
+ }
+
private void DataContractSerialize(XmlWriterSettings xws, DataContractSerializer dcs, IMessage[] messages, Stream str)
{
ArrayList o = new ArrayList(messages);
@@ -381,4 +403,9 @@ public class MessageWithList : IMessage
{
public List<MessageWithListItem> Items { get; set; }
}
+
+ public class MessageWithNestedObject : IMessage
+ {
+ public MessageWithListItem NestedObject { get; set; }
+ }
}
View
20 src/impl/Serializers/NServiceBus.Serializers.XML/MessageSerializer.cs
@@ -442,25 +442,12 @@ private FieldInfo GetField(Type t, string name)
private object GetPropertyValue(Type type, XmlNode n)
{
- if (n.ChildNodes.Count == 1 && n.ChildNodes[0] is XmlWhitespace)
- {
- if (typeof(IEnumerable).IsAssignableFrom(type) && type != typeof(string))
- {
- var list = CreateList(type);
- if (type.IsArray)
- return list.GetType().GetMethod("ToArray").Invoke(list, null);
- return list;
- }
-
- return n.ChildNodes[0].InnerText;
- }
-
- if (n.ChildNodes.Count == 1 && n.ChildNodes[0] is XmlText)
+ if (n.ChildNodes.Count == 1 && n.ChildNodes[0] is XmlCharacterData)
{
var text = n.ChildNodes[0].InnerText;
var args = type.GetGenericArguments();
- if (args.Length == 1)
+ if (args.Length == 1 && args[0].IsValueType)
{
var nullableType = typeof (Nullable<>).MakeGenericType(args);
if (type == nullableType)
@@ -535,7 +522,8 @@ private object GetPropertyValue(Type type, XmlNode n)
if (type == typeof(Uri))
return new Uri(text);
- throw new Exception("Type not supported by the serializer: " + type.AssemblyQualifiedName);
+ if (!(n.ChildNodes[0] is XmlWhitespace))
+ throw new Exception("Type not supported by the serializer: " + type.AssemblyQualifiedName);
}
//Handle dictionaries
Something went wrong with that request. Please try again.