Skip to content

Commit 69b6970

Browse files
author
Michael Hallett
committed
Merge pull request #646 from lycilph/master
Make the attributes search work the same as for elements
2 parents 3642ea4 + 416146f commit 69b6970

File tree

5 files changed

+74
-39
lines changed

5 files changed

+74
-39
lines changed

RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@
8585
<Compile Include="OAuthTests.cs" />
8686
<Compile Include="SampleClasses\EmployeeTracker.cs" />
8787
<Compile Include="SampleClasses\EnumTest.cs" />
88+
<Compile Include="SampleClasses\Goodreads.cs" />
8889
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
8990
<Compile Include="SampleClasses\Struct.cs" />
9091
<Compile Include="XmlAttributeDeserializerTests.cs" />
@@ -123,6 +124,9 @@
123124
<Content Include="SampleData\boolean_from_number.xml">
124125
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
125126
</Content>
127+
<Content Include="SampleData\Goodreads.xml">
128+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
129+
</Content>
126130
<Content Include="SampleData\iso8601datetimes.txt">
127131
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
128132
</Content>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
using System.Collections.Generic;
2+
3+
namespace RestSharp.Tests.SampleClasses
4+
{
5+
public class GoodReadsReviewCollection
6+
{
7+
public int Start { get; set; }
8+
public int End { get; set; }
9+
public int Total { get; set; }
10+
public List<GoodReadsReview> Reviews { get; set; }
11+
}
12+
13+
public class GoodReadsReview
14+
{
15+
public string Id { get; set; }
16+
public GoodReadsBook Book { get; set; }
17+
}
18+
19+
public class GoodReadsBook
20+
{
21+
public string Isbn { get; set; }
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<GoodreadsResponse>
3+
<reviews start="1" end="2" total="288">
4+
<review>
5+
<book id="1208943892">
6+
<isbn>0345475836</isbn>
7+
</book>
8+
</review>
9+
10+
<review>
11+
<Id>1198344567</Id>
12+
<book>
13+
<isbn>0802775802</isbn>
14+
</book>
15+
</review>
16+
</reviews>
17+
</GoodreadsResponse>

RestSharp.Tests/XmlDeserializerTests.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,20 @@ public void Can_Deserialize_Google_Weather_Xml()
546546
Assert.Equal("Sunny", output.weather[0].condition.data);
547547
}
548548

549+
[Fact]
550+
public void Can_Deserialize_Goodreads_Xml()
551+
{
552+
var xmlpath = PathFor("Goodreads.xml");
553+
var doc = XDocument.Load(xmlpath);
554+
var response = new RestResponse { Content = doc.ToString() };
555+
var d = new XmlDeserializer();
556+
var output = d.Deserialize<GoodReadsReviewCollection>(response);
557+
558+
Assert.Equal(2, output.Reviews.Count);
559+
Assert.Equal("1208943892", output.Reviews[0].Id); // This fails without fixing the XmlDeserializer
560+
Assert.Equal("1198344567", output.Reviews[1].Id);
561+
}
562+
549563
[Fact]
550564
public void Can_Deserialize_Boolean_From_Number()
551565
{

RestSharp/Deserializers/XmlDeserializer.cs

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -453,50 +453,27 @@ protected virtual XElement GetElementByName(XElement root, XName name)
453453
}
454454

455455
// try looking for element that matches sanitized property name (Order by depth)
456-
var element =
457-
root.Descendants()
458-
.OrderBy(d => d.Ancestors().Count())
459-
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName) ?? root.Descendants()
460-
.OrderBy(d => d.Ancestors().Count())
461-
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName.ToLower());
462-
463-
if (element != null)
464-
{
465-
return element;
466-
}
467-
468-
return null;
456+
return root.Descendants()
457+
.OrderBy(d => d.Ancestors().Count())
458+
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName) ??
459+
root.Descendants()
460+
.OrderBy(d => d.Ancestors().Count())
461+
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName.ToLower());
469462
}
470463

471464
protected virtual XAttribute GetAttributeByName(XElement root, XName name)
472465
{
473-
var lowerName = name.LocalName.ToLower().AsNamespaced(name.NamespaceName);
474-
var camelName = name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName);
475-
476-
if (root.Attribute(name) != null)
477-
{
478-
return root.Attribute(name);
479-
}
480-
481-
if (root.Attribute(lowerName) != null)
466+
var names = new List<XName>
482467
{
483-
return root.Attribute(lowerName);
484-
}
485-
486-
if (root.Attribute(camelName) != null)
487-
{
488-
return root.Attribute(camelName);
489-
}
490-
491-
// try looking for element that matches sanitized property name
492-
var element = root.Attributes().FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName);
493-
494-
if (element != null)
495-
{
496-
return element;
497-
}
498-
499-
return null;
468+
name.LocalName,
469+
name.LocalName.ToLower().AsNamespaced(name.NamespaceName),
470+
name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName)
471+
};
472+
473+
return root.DescendantsAndSelf()
474+
.OrderBy(d => d.Ancestors().Count())
475+
.Attributes()
476+
.FirstOrDefault(d => names.Contains(d.Name.LocalName.RemoveUnderscoresAndDashes()));
500477
}
501478
}
502479
}

0 commit comments

Comments
 (0)