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
4 changes: 4 additions & 0 deletions RestSharp.Tests/RestSharp.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@
<Compile Include="OAuthTests.cs" />
<Compile Include="SampleClasses\EmployeeTracker.cs" />
<Compile Include="SampleClasses\EnumTest.cs" />
<Compile Include="SampleClasses\Goodreads.cs" />
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
<Compile Include="SampleClasses\Struct.cs" />
<Compile Include="XmlAttributeDeserializerTests.cs" />
Expand Down Expand Up @@ -122,6 +123,9 @@
<Content Include="SampleData\boolean_from_number.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SampleData\Goodreads.xml">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
<Content Include="SampleData\iso8601datetimes.txt">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</Content>
Expand Down
23 changes: 23 additions & 0 deletions RestSharp.Tests/SampleClasses/Goodreads.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System.Collections.Generic;

namespace RestSharp.Tests.SampleClasses
{
public class GoodReadsReviewCollection
{
public int Start { get; set; }
public int End { get; set; }
public int Total { get; set; }
public List<GoodReadsReview> Reviews { get; set; }
}

public class GoodReadsReview
{
public string Id { get; set; }
public GoodReadsBook Book { get; set; }
}

public class GoodReadsBook
{
public string Isbn { get; set; }
}
}
17 changes: 17 additions & 0 deletions RestSharp.Tests/SampleData/Goodreads.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<GoodreadsResponse>
<reviews start="1" end="2" total="288">
<review>
<book id="1208943892">
<isbn>0345475836</isbn>
</book>
</review>

<review>
<Id>1198344567</Id>
<book>
<isbn>0802775802</isbn>
</book>
</review>
</reviews>
</GoodreadsResponse>
14 changes: 14 additions & 0 deletions RestSharp.Tests/XmlDeserializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -546,6 +546,20 @@ public void Can_Deserialize_Google_Weather_Xml()
Assert.Equal("Sunny", output.weather[0].condition.data);
}

[Fact]
public void Can_Deserialize_Goodreads_Xml()
{
var xmlpath = PathFor("Goodreads.xml");
var doc = XDocument.Load(xmlpath);
var response = new RestResponse { Content = doc.ToString() };
var d = new XmlDeserializer();
var output = d.Deserialize<GoodReadsReviewCollection>(response);

Assert.Equal(2, output.Reviews.Count);
Assert.Equal("1208943892", output.Reviews[0].Id); // This fails without fixing the XmlDeserializer
Assert.Equal("1198344567", output.Reviews[1].Id);
}

[Fact]
public void Can_Deserialize_Boolean_From_Number()
{
Expand Down
55 changes: 16 additions & 39 deletions RestSharp/Deserializers/XmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -453,50 +453,27 @@ protected virtual XElement GetElementByName(XElement root, XName name)
}

// try looking for element that matches sanitized property name (Order by depth)
var element =
root.Descendants()
.OrderBy(d => d.Ancestors().Count())
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName) ?? root.Descendants()
.OrderBy(d => d.Ancestors().Count())
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName.ToLower());

if (element != null)
{
return element;
}

return null;
return root.Descendants()
.OrderBy(d => d.Ancestors().Count())
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName) ??
root.Descendants()
.OrderBy(d => d.Ancestors().Count())
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName.ToLower());
}

protected virtual XAttribute GetAttributeByName(XElement root, XName name)
{
var lowerName = name.LocalName.ToLower().AsNamespaced(name.NamespaceName);
var camelName = name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName);

if (root.Attribute(name) != null)
{
return root.Attribute(name);
}

if (root.Attribute(lowerName) != null)
var names = new List<XName>
{
return root.Attribute(lowerName);
}

if (root.Attribute(camelName) != null)
{
return root.Attribute(camelName);
}

// try looking for element that matches sanitized property name
var element = root.Attributes().FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName);

if (element != null)
{
return element;
}

return null;
name.LocalName,
name.LocalName.ToLower().AsNamespaced(name.NamespaceName),
name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName)
};

return root.DescendantsAndSelf()
.OrderBy(d => d.Ancestors().Count())
.Attributes()
.FirstOrDefault(d => names.Contains(d.Name.LocalName.RemoveUnderscoresAndDashes()));
}
}
}