Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
75 changes: 74 additions & 1 deletion RestSharp.Tests/SerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public void Can_serialize_Enum()
expected.Add(root);

Assert.Equal( expected.ToString(), doc.ToString() );
}
}

[Fact]
public void Can_serialize_simple_POCO_With_DateFormat_Specified() {
Expand Down Expand Up @@ -157,6 +157,35 @@ public void Can_serialize_simple_POCO_With_Attribute_Options_Defined() {
Assert.Equal(expected.ToString(), doc.ToString());
}

[Fact]
public void Can_serialize_simple_POCO_With_Attribute_Options_Defined_And_Property_Containing_IList_Elements()
{
var poco = new WackyPerson
{
Name = "Foo",
Age = 50,
Price = 19.95m,
StartDate = new DateTime(2009, 12, 18, 10, 2, 23),
ContactData = new ContactData
{
EmailAddresses = new List<EmailAddress>
{
new EmailAddress
{
Address = "test@test.com",
Location = "Work"
}
}
}
};

var xml = new XmlSerializer();
var doc = xml.Serialize(poco);
var expected = GetSimplePocoXDocWackyNamesWithIListProperty();

Assert.Equal(expected.ToString(), doc.ToString());
}

[Fact]
public void Can_serialize_a_list_which_is_the_root_element()
{
Expand Down Expand Up @@ -238,12 +267,36 @@ private class WackyPerson

[SerializeAs(Name = "start_date", Attribute = true)]
public DateTime StartDate { get; set; }

[SerializeAs(Name = "contact-data")]
public ContactData ContactData { get; set; }
}

[SerializeAs(Name = "People")]
private class PersonList : List<Person>
{

}

private class ContactData
{
public ContactData()
{
EmailAddresses = new List<EmailAddress>();
}

[SerializeAs(Name = "email-addresses")]
public List<EmailAddress> EmailAddresses { get; set; }
}

[SerializeAs(Name = "email-address")]
private class EmailAddress
{
[SerializeAs(Name = "address")]
public string Address { get; set; }

[SerializeAs(Name = "location")]
public string Location { get; set; }
}

private XDocument GetSimplePocoXDoc()
Expand Down Expand Up @@ -323,6 +376,26 @@ private XDocument GetSimplePocoXDocWackyNames() {
return doc;
}

private XDocument GetSimplePocoXDocWackyNamesWithIListProperty()
{
var doc = new XDocument();
var root = new XElement("Person");
root.Add(new XAttribute("WackyName", "Foo"),
new XElement("Age", 50),
new XAttribute("Price", 19.95m),
new XAttribute("start_date", new DateTime(2009, 12, 18, 10, 2, 23).ToString()),
new XElement("contact-data",
new XElement("email-addresses",
new XElement("email-address",
new XElement("address", "test@test.com"),
new XElement("location", "Work")
))));

doc.Add(root);

return doc;
}

private XDocument GetSortedPropsXDoc() {
var doc = new XDocument();
var root = new XElement("OrderedProperties");
Expand Down
9 changes: 7 additions & 2 deletions RestSharp/Serializers/XmlSerializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,13 @@ where p.CanRead && p.CanWrite
else if (rawValue is IList) {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I believe this is why it's specific to IList. There was an existing logic branch that my patch modified that operated specifically on IList collections. Changing this to IEnumerable would widen the scope of my patch. Following the logic branching in this section of code, IEnumerable or ICollection would fall down to the "else" statement beneath the one I modified.

var itemTypeName = "";
foreach (var item in (IList)rawValue) {
if (itemTypeName == "") {
itemTypeName = item.GetType().Name;
if (itemTypeName == "")
{
var type = item.GetType();
var setting = type.GetAttribute<SerializeAsAttribute>();
itemTypeName = setting != null && setting.Name.HasValue()
? setting.Name
: type.Name;
}
var instance = new XElement(itemTypeName);
Map(instance, item);
Expand Down