Skip to content

Conversation

@PaulCampbell
Copy link
Contributor

Hi there,

When trying to deserialize a nullable type, if the element has any attributes, the value is set to "", which is problematic.

here's a test:

[Fact]
public void Can_Deserialize_Empty_Elements_With_Attributes_to_Nullable_Values()
{
    var doc = CreateXmlWithAttributesAndNullValues();

    var xml = new XmlDeserializer();
    var output = xml.Deserialize<NullableValues>(new RestResponse { Content = doc });

    Assert.Null(output.Id);
    Assert.Null(output.StartDate);
    Assert.Null(output.UniqueId);
}

private static string CreateXmlWithAttributesAndNullValuesAndPopulatedValues()
{
    var doc = new XDocument();
    var root = new XElement("NullableValues");

    var idElement = new XElement("Id", null);
    idElement.SetAttributeValue("SomeAttribute", "SomeAttribute_Value");
    root.Add(idElement,
             new XElement("StartDate", null),
             new XElement("UniqueId", new Guid(GuidString))
        );

    doc.Add(root);

    return doc.ToString();
}

I have attached a pull request, with a further test and a fix.

Please give me a shout if you would like me to change anything, or if there's any problem

Thanks guys!

johnsheehan added a commit that referenced this pull request Oct 23, 2011
XML Deserialization of nullable types with attributes
@johnsheehan johnsheehan merged commit 6dd80ef into restsharp:master Oct 23, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants