Skip to content

Commit 6dd80ef

Browse files
committed
Merge pull request #169 from PaulCampbell/master
XML Deserialization of nullable types with attributes
2 parents 19d3854 + 88f9c6b commit 6dd80ef

File tree

2 files changed

+67
-1
lines changed

2 files changed

+67
-1
lines changed

RestSharp.Tests/XmlTests.cs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,32 @@ public void Can_Deserialize_Boolean_From_String()
508508
Assert.True(output.Value);
509509
}
510510

511+
[Fact]
512+
public void Can_Deserialize_Empty_Elements_With_Attributes_to_Nullable_Values()
513+
{
514+
var doc = CreateXmlWithAttributesAndNullValues();
515+
516+
var xml = new XmlDeserializer();
517+
var output = xml.Deserialize<NullableValues>(new RestResponse { Content = doc });
518+
519+
Assert.Null(output.Id);
520+
Assert.Null(output.StartDate);
521+
Assert.Null(output.UniqueId);
522+
}
523+
524+
[Fact]
525+
public void Can_Deserialize_Mixture_Of_Empty_Elements_With_Attributes_And_Populated_Elements()
526+
{
527+
var doc = CreateXmlWithAttributesAndNullValuesAndPopulatedValues();
528+
529+
var xml = new XmlDeserializer();
530+
var output = xml.Deserialize<NullableValues>(new RestResponse { Content = doc });
531+
532+
Assert.Null(output.Id);
533+
Assert.Null(output.StartDate);
534+
Assert.Equal(new Guid(GuidString), output.UniqueId);
535+
}
536+
511537
private static string CreateUnderscoresXml()
512538
{
513539
var doc = new XDocument();
@@ -753,5 +779,39 @@ private static string CreateXmlWithEmptyInlineList()
753779

754780
return doc.ToString();
755781
}
782+
783+
private static string CreateXmlWithAttributesAndNullValues()
784+
{
785+
var doc = new XDocument();
786+
var root = new XElement("NullableValues");
787+
788+
var idElement = new XElement("Id", null);
789+
idElement.SetAttributeValue("SomeAttribute", "SomeAttribute_Value");
790+
root.Add(idElement,
791+
new XElement("StartDate", null),
792+
new XElement("UniqueId", null)
793+
);
794+
795+
doc.Add(root);
796+
797+
return doc.ToString();
798+
}
799+
800+
private static string CreateXmlWithAttributesAndNullValuesAndPopulatedValues()
801+
{
802+
var doc = new XDocument();
803+
var root = new XElement("NullableValues");
804+
805+
var idElement = new XElement("Id", null);
806+
idElement.SetAttributeValue("SomeAttribute", "SomeAttribute_Value");
807+
root.Add(idElement,
808+
new XElement("StartDate", null),
809+
new XElement("UniqueId", new Guid(GuidString))
810+
);
811+
812+
doc.Add(root);
813+
814+
return doc.ToString();
815+
}
756816
}
757817
}

RestSharp/Deserializers/XmlDeserializer.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,13 @@ private void Map(object x, XElement root)
124124
// check for nullable and extract underlying type
125125
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Nullable<>))
126126
{
127-
type = type.GetGenericArguments()[0];
127+
// if the value is empty, set the property to null...
128+
if (value == null || String.IsNullOrEmpty(value.ToString()))
129+
{
130+
prop.SetValue(x, null, null);
131+
continue;
132+
}
133+
type = type.GetGenericArguments()[0];
128134
}
129135

130136
if (type == typeof(bool))

0 commit comments

Comments
 (0)