Skip to content

Commit c6eb4e9

Browse files
committed
Make the attributes search work the same as for elements
1 parent 0b6ffe8 commit c6eb4e9

File tree

5 files changed

+72
-14
lines changed

5 files changed

+72
-14
lines changed

RestSharp.Tests/RestSharp.Tests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@
8484
<Compile Include="OAuthTests.cs" />
8585
<Compile Include="SampleClasses\EmployeeTracker.cs" />
8686
<Compile Include="SampleClasses\EnumTest.cs" />
87+
<Compile Include="SampleClasses\Goodreads.cs" />
8788
<Compile Include="SampleClasses\GoogleWeatherWithAttributes.cs" />
8889
<Compile Include="SampleClasses\Struct.cs" />
8990
<Compile Include="XmlAttributeDeserializerTests.cs" />
@@ -122,6 +123,9 @@
122123
<Content Include="SampleData\boolean_from_number.xml">
123124
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
124125
</Content>
126+
<Content Include="SampleData\Goodreads.xml">
127+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
128+
</Content>
125129
<Content Include="SampleData\iso8601datetimes.txt">
126130
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
127131
</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: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -470,33 +470,33 @@ protected virtual XElement GetElementByName(XElement root, XName name)
470470

471471
protected virtual XAttribute GetAttributeByName(XElement root, XName name)
472472
{
473-
var lowerName = name.LocalName.ToLower().AsNamespaced(name.NamespaceName);
474-
var camelName = name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName);
473+
var lower_name = name.LocalName.ToLower().AsNamespaced(name.NamespaceName);
474+
var camel_name = name.LocalName.ToCamelCase(Culture).AsNamespaced(name.NamespaceName);
475475

476476
if (root.Attribute(name) != null)
477477
{
478478
return root.Attribute(name);
479479
}
480480

481-
if (root.Attribute(lowerName) != null)
481+
if (root.Attribute(lower_name) != null)
482482
{
483-
return root.Attribute(lowerName);
483+
return root.Attribute(lower_name);
484484
}
485485

486-
if (root.Attribute(camelName) != null)
486+
if (root.Attribute(camel_name) != null)
487487
{
488-
return root.Attribute(camelName);
488+
return root.Attribute(camel_name);
489489
}
490490

491491
// 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;
492+
return root.Descendants()
493+
.OrderBy(d => d.Ancestors().Count())
494+
.Attributes()
495+
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName) ??
496+
root.Descendants()
497+
.OrderBy(d => d.Ancestors().Count())
498+
.Attributes()
499+
.FirstOrDefault(d => d.Name.LocalName.RemoveUnderscoresAndDashes() == name.LocalName.ToLower());
500500
}
501501
}
502502
}

0 commit comments

Comments
 (0)