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
5 changes: 5 additions & 0 deletions RestSharp.Tests/SampleClasses/misc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public class PersonForXml
public Disposition Disposition { get; set; }

}

public class IncomingInvoice
{
public int ConceptId { get; set; }
}

public class PersonForJson
{
Expand Down
36 changes: 31 additions & 5 deletions RestSharp.Tests/XmlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -464,12 +464,12 @@ public void Can_Deserialize_Names_With_Dashes_On_Default_Root()
Assert.NotNull(p.Foes);
Assert.Equal(5, p.Foes.Count);
Assert.Equal("Yankees", p.Foes.Team);
}

[Fact]
public void Can_Deserialize_Names_With_Underscores_Without_Matching_Case_On_Default_Root()
}
[Fact]
public void Can_Deserialize_Lower_Cased_Root_Elements_With_Dashes()
Copy link
Contributor

Choose a reason for hiding this comment

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

Instead of removing this existing test for LowercaseUnderscores, can we supplement the lowercase underscores test with an additional test for dashes? Doing so will help us ensure both deserialization approaches stay working after future changes.

{
var doc = CreateLowercaseUnderscoresXml();
var doc = CreateDashesXml();
var response = new RestResponse { Content = doc };

var d = new XmlDeserializer();
Expand Down Expand Up @@ -497,6 +497,20 @@ public void Can_Deserialize_Names_With_Underscores_Without_Matching_Case_On_Defa
Assert.Equal("Yankees", p.Foes.Team);
}

[Fact]
public void Can_Deserialize_Root_Elements_Without_Matching_Case_And_Dashes()
{
var doc = CreateLowerCasedRootElementWithDashesXml();
var response = new RestResponse { Content = doc };

var d = new XmlDeserializer();
var p = d.Deserialize<List<IncomingInvoice>>(response);

Assert.NotNull(p);
Assert.Equal(1, p.Count);
Assert.Equal(45, p[0].ConceptId);
}


[Fact]
public void Can_Deserialize_Eventful_Xml()
Expand Down Expand Up @@ -726,6 +740,18 @@ private static string CreateDashesXml()
doc.Add(root);
return doc.ToString();
}

private static string CreateLowerCasedRootElementWithDashesXml()
{
var doc = new XDocument();
var root = new XElement("incoming-invoices",
new XElement("incoming-invoice",
new XElement("concept-id", 45)
)
);
doc.Add(root);
return doc.ToString();
}

private static string CreateElementsXml()
{
Expand Down
9 changes: 8 additions & 1 deletion RestSharp/Deserializers/XmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ private object HandleListDerivative(object x, XElement root, string propName, Ty
var elements = root.Descendants(t.Name.AsNamespaced(Namespace));

var name = t.Name;

if (!elements.Any())
{
var lowerName = name.ToLower().AsNamespaced(Namespace);
Expand All @@ -267,7 +268,13 @@ private object HandleListDerivative(object x, XElement root, string propName, Ty
if (!elements.Any())
{
elements = root.Descendants().Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == name);
}
}

if (!elements.Any())
{
var lowerName = name.ToLower().AsNamespaced(Namespace);
elements = root.Descendants().Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == lowerName);
}

PopulateListFromElements(t, elements, list);

Expand Down