Skip to content

Commit 84cc01d

Browse files
committed
Fix for issue restsharp#144 with test.
1 parent 7d70af4 commit 84cc01d

File tree

5 files changed

+53
-4
lines changed

5 files changed

+53
-4
lines changed

RestSharp.Tests/RestSharp.Tests.csproj

+3
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,9 @@
146146
<Content Include="SampleData\Lastfm.xml">
147147
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
148148
</Content>
149+
<Content Include="SampleData\xmllists.xml">
150+
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
151+
</Content>
149152
</ItemGroup>
150153
<ItemGroup>
151154
<BootstrapperPackage Include="Microsoft.Net.Client.3.5">

RestSharp.Tests/SampleClasses/ListSamples.cs

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
namespace RestSharp.Tests.SampleClasses
77
{
8+
public class SimpleTypesListSample
9+
{
10+
public List<string> Names { get; set; }
11+
public List<int> Numbers { get; set; }
12+
}
13+
814
public class InlineListSample
915
{
1016
public int Count { get; set; }
+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<XmlLists>
3+
<Names xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
4+
<a:string>John</a:string>
5+
<a:string>Bob</a:string>
6+
<a:string>Albert</a:string>
7+
<a:string>Jeff</a:string>
8+
<a:string>Charlie</a:string>
9+
</Names>
10+
<Numbers xmlns:a="http://schemas.microsoft.com/2003/10/Serialization/Arrays">
11+
<a:number>1</a:number>
12+
<a:number>2</a:number>
13+
<a:number>3</a:number>
14+
<a:number>5</a:number>
15+
<a:number>7</a:number>
16+
<a:number>11</a:number>
17+
</Numbers>
18+
</XmlLists>

RestSharp.Tests/XmlTests.cs

+13
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,19 @@ private string PathFor(string sampleFile)
3636
return Path.Combine(SampleDataPath, sampleFile);
3737
}
3838

39+
[Fact]
40+
public void Can_Deserialize_Lists_of_Simple_Types()
41+
{
42+
var xmlpath = PathFor("xmllists.xml");
43+
var doc = XDocument.Load(xmlpath);
44+
45+
var xml = new XmlDeserializer();
46+
var output = xml.Deserialize<SimpleTypesListSample>(new RestResponse() { Content = doc.ToString() });
47+
48+
Assert.NotEmpty(output.Names);
49+
Assert.NotEmpty(output.Numbers);
50+
}
51+
3952
[Fact]
4053
public void Can_Deserialize_To_List_Inheritor_From_Custom_Root_With_Attributes()
4154
{

RestSharp/Deserializers/XmlDeserializer.cs

+13-4
Original file line numberDiff line numberDiff line change
@@ -278,10 +278,19 @@ private object HandleListDerivative(object x, XElement root, string propName, Ty
278278

279279
private object CreateAndMap(Type t, XElement element)
280280
{
281-
var item = Activator.CreateInstance(t);
282-
Map(item, element);
283-
return item;
284-
}
281+
object item;
282+
if (t == typeof(String))
283+
{
284+
item = element.Value;
285+
}
286+
else
287+
{
288+
item = Activator.CreateInstance(t);
289+
Map(item, element);
290+
}
291+
292+
return item;
293+
}
285294

286295
private object GetValueFromXml(XElement root, XName name)
287296
{

0 commit comments

Comments
 (0)