Skip to content

Commit

Permalink
Merge pull request #2823 from Particular/hotfix-5.0.5
Browse files Browse the repository at this point in the history
XmlSerializer now serializes properties of `object` type properly
  • Loading branch information
hmemcpy committed Aug 26, 2015
2 parents 797ebeb + 1a23564 commit 9d1cda4
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 20 deletions.
1 change: 1 addition & 0 deletions src/NServiceBus.Core.Tests/NServiceBus.Core.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@
<Compile Include="Config\When_users_override_the_configuration_source.cs" />
<Compile Include="Config\When_using_convention_based_messages.cs" />
<Compile Include="Conventions\MessageConventionSpecs.cs" />
<Compile Include="Serializers\XML\Issue_2796.cs" />
<Compile Include="StandardsTests.cs" />
<Compile Include="Encryption\ConfigureRijndaelEncryptionServiceTests.cs" />
<Compile Include="Encryption\RijndaelEncryptionServiceTest.cs" />
Expand Down
41 changes: 41 additions & 0 deletions src/NServiceBus.Core.Tests/Serializers/XML/Issue_2796.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
namespace NServiceBus.Core.Tests.Serializers.XML
{
using System;
using System.IO;
using NServiceBus.Serializers.XML.Test;
using NUnit.Framework;

[TestFixture]
public class Issue_2796
{
[Test]
public void Object_property_with_primitive_or_struct_value_should_serialize_correctly()
{
var serializer = SerializerFactory.Create<SerializedPair>();
var message = new SerializedPair
{
Key = "AddressId",
Value = new Guid("{ebdeeb33-baa7-4100-b1aa-eb4d6816fd3d}")
};

object[] messageDeserialized;
using (Stream stream = new MemoryStream())
{
serializer.Serialize(message, stream);

stream.Position = 0;

messageDeserialized = serializer.Deserialize(stream, new[] { message.GetType() });
}

Assert.AreEqual(message.Key, ((SerializedPair)messageDeserialized[0]).Key);
Assert.AreEqual(message.Value, ((SerializedPair)messageDeserialized[0]).Value);
}

public class SerializedPair
{
public string Key { get; set; }
public object Value { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void Run(Configure config)
statusText.Append("Deactivation reason: ");
if (diagnosticData.PrerequisiteStatus != null && !diagnosticData.PrerequisiteStatus.IsSatisfied)
{
statusText.AppendLine(string.Format("Did not fulfill its Prerequisites:"));
statusText.AppendLine("Did not fulfill its Prerequisites:");

foreach (var reason in diagnosticData.PrerequisiteStatus.Reasons)
{
Expand Down
2 changes: 1 addition & 1 deletion src/NServiceBus.Core/Licensing/LicenseExpiredForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ void browseButton_Click(object sender, EventArgs e)

if (LicenseExpirationChecker.HasLicenseExpired(license))
{
var message = string.Format("The license you provided has expired, please select another file.");
var message = "The license you provided has expired, please select another file.";
Logger.Warn(message);
MessageBox.Show(this, message, "License expired", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
return;
Expand Down
36 changes: 18 additions & 18 deletions src/NServiceBus.Core/Serializers/XML/XmlMessageSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -831,12 +831,8 @@ object GetPropertyValue(Type type, XmlNode n)
public void Serialize(object message, Stream stream)
{
InitializeNamespaces(message);
var messageBuilder = SerializeMessage(message);
var builder = SerializeMessage(message);

var builder = new StringBuilder();
builder.AppendLine("<?xml version=\"1.0\" ?>");

builder.Append(messageBuilder);
var buffer = Encoding.UTF8.GetBytes(builder.ToString());
stream.Write(buffer, 0, buffer.Length);
}
Expand All @@ -850,9 +846,23 @@ public void Serialize(object message, Stream stream)
StringBuilder SerializeMessage(object message)
{
var messageBuilder = new StringBuilder();

messageBuilder.AppendLine("<?xml version=\"1.0\" ?>");

var t = mapper.GetMappedTypeFor(message.GetType());
var baseTypes = GetBaseTypes(message);

var objectBuilder = new StringBuilder();
var elementName = t.SerializationFriendlyName();

WriteObject(elementName, t, message, objectBuilder);

var startElementBuilder = new StringBuilder();
var ns = GetNamespace(message);
CreateStartElementWithNamespaces(ns, baseTypes, startElementBuilder, elementName);

WriteObject(t.SerializationFriendlyName(), t, message, messageBuilder, true);
objectBuilder.Replace(string.Format("<{0}>", elementName), startElementBuilder.ToString());
messageBuilder.AppendLine(objectBuilder.ToString().TrimEnd());

return messageBuilder;
}
Expand Down Expand Up @@ -906,7 +916,7 @@ void WriteObject(string name, Type type, object value, StringBuilder builder, bo
{
var element = name;

if (type == typeof(object) && (value.GetType().IsSimpleType()))
if (type == typeof(object) && value.GetType().IsSimpleType())
{
if (!namespacesToAdd.Contains(value.GetType()))
{
Expand All @@ -920,17 +930,7 @@ void WriteObject(string name, Type type, object value, StringBuilder builder, bo
return;
}


if (useNS)
{
var @namespace = InitializeNamespaces(value);
var baseTypes = GetBaseTypes(value);
CreateStartElementWithNamespaces(@namespace, baseTypes, builder, element);
}
else
{
builder.AppendFormat("<{0}>\n", element);
}
builder.AppendFormat("<{0}>\n", element);

Write(builder, type, value);

Expand Down

0 comments on commit 9d1cda4

Please sign in to comment.