From 50649307c884c49f23e8611454ad3022cf6bd00e Mon Sep 17 00:00:00 2001 From: Sergey Vychegzhanin Date: Mon, 20 Jan 2020 10:59:14 +0300 Subject: [PATCH 1/2] Add ability to serialize list of items with type of interface. --- .../Serializers/Xml/XmlSerializer.cs | 15 ++-- test/RestSharp.Tests/XmlSerializerTests.cs | 71 ++++++++++++++++++- 2 files changed, 74 insertions(+), 12 deletions(-) diff --git a/src/RestSharp/Serializers/Xml/XmlSerializer.cs b/src/RestSharp/Serializers/Xml/XmlSerializer.cs index 076ed5b72..59b008c29 100644 --- a/src/RestSharp/Serializers/Xml/XmlSerializer.cs +++ b/src/RestSharp/Serializers/Xml/XmlSerializer.cs @@ -193,19 +193,14 @@ void Map(XContainer root, object obj) } else if (rawValue is IList items) { - var itemTypeName = ""; - foreach (var item in items) { - if (itemTypeName == "") - { - var type = item.GetType(); - var setting = type.GetAttribute(); + var type = item.GetType(); + var setting = type.GetAttribute(); - itemTypeName = setting != null && setting.Name.HasValue() - ? setting.Name - : type.Name; - } + var itemTypeName = setting != null && setting.Name.HasValue() + ? setting.Name + : type.Name; var instance = new XElement(itemTypeName.AsNamespaced(Namespace)); diff --git a/test/RestSharp.Tests/XmlSerializerTests.cs b/test/RestSharp.Tests/XmlSerializerTests.cs index 133dcd912..c6d49967e 100644 --- a/test/RestSharp.Tests/XmlSerializerTests.cs +++ b/test/RestSharp.Tests/XmlSerializerTests.cs @@ -18,6 +18,36 @@ public XmlSerializerTests() Thread.CurrentThread.CurrentUICulture = CultureInfo.InstalledUICulture; } + [Test] + public void Can_serialize_a_list_of_items_with_interface_type() + { + var items = new NamedItems + { + Items = new List + { + new Person + { + Name = "Foo", + Age = 50, + Price = 19.95m, + StartDate = new DateTime(2009, 12, 18, 10, 2, 23), + Items = new List + { + new Item {Name = "One", Value = 1}, + } + }, + new Item {Name = "Two", Value = 2}, + new Item {Name = "Three", Value = 3} + } + }; + + var xml = new XmlSerializer(); + var doc = xml.Serialize(items); + var expected = GetNamedItemsXDoc(CultureInfo.InvariantCulture); + + Assert.AreEqual(expected.ToString(), doc); + } + [Test] public void Can_serialize_a_list_which_is_the_content_of_root_element() { @@ -289,7 +319,12 @@ public void Serializes_Properties_In_Specified_Order() Assert.AreEqual(expected.ToString(), doc); } - class Person + interface INamed + { + string Name { get; set; } + } + + class Person : INamed { public string Name { get; set; } @@ -304,7 +339,7 @@ class Person public bool? IsCool { get; set; } } - class Item + class Item : INamed { public string Name { get; set; } @@ -336,6 +371,12 @@ class WackyPerson public ContactData ContactData { get; set; } } + class NamedItems + { + [SerializeAs(Content = true)] + public List Items { get; set; } + } + [SerializeAs(Name = "People")] class Contacts { @@ -527,6 +568,32 @@ static XDocument GetSortedPropsXDoc() return doc; } + static XDocument GetNamedItemsXDoc(IFormatProvider culture) + { + var doc = new XDocument(); + var root = new XElement("NamedItems"); + var element = new XElement("Person"); + var items = new XElement("Items"); + + items.Add(new XElement("Item", new XElement("Name", "One"), new XElement("Value", 1))); + + element.Add( + new XElement("Name", "Foo"), + new XElement("Age", 50), + new XElement("Price", 19.95m.ToString(culture)), + new XElement("StartDate", new DateTime(2009, 12, 18, 10, 2, 23).ToString(culture)) + ); + + element.Add(items); + root.Add(element); + root.Add(new XElement("Item", new XElement("Name", "Two"), new XElement("Value", 2))); + root.Add(new XElement("Item", new XElement("Name", "Three"), new XElement("Value", 3))); + + doc.Add(root); + + return doc; + } + static XDocument GetPeopleXDoc(IFormatProvider culture) { var doc = new XDocument(); From 94b5dc26ec82d065e0feb559071bbdacc6b390ba Mon Sep 17 00:00:00 2001 From: Sergey Vychegzhanin Date: Mon, 20 Jan 2020 14:48:38 +0300 Subject: [PATCH 2/2] Changed actions for Github --- .github/workflows/build-master.yml | 5 ----- .github/workflows/publish-docs.yml | 30 ------------------------------ 2 files changed, 35 deletions(-) delete mode 100644 .github/workflows/publish-docs.yml diff --git a/.github/workflows/build-master.yml b/.github/workflows/build-master.yml index 78d022b44..5688604b7 100644 --- a/.github/workflows/build-master.yml +++ b/.github/workflows/build-master.yml @@ -52,8 +52,3 @@ jobs: - name: Create NuGet package run: dotnet pack -c Release -o nuget -p:IncludeSymbols=true -p:SymbolPackageFormat=snupkg - - - name: Push NuGet package - run: dotnet nuget push **/*.nupkg --api-key ${{ secrets.NUGET_API_KEY }} --source https://api.nuget.org/v3/index.json --skip-duplicate - - \ No newline at end of file diff --git a/.github/workflows/publish-docs.yml b/.github/workflows/publish-docs.yml deleted file mode 100644 index 4a5f0da7d..000000000 --- a/.github/workflows/publish-docs.yml +++ /dev/null @@ -1,30 +0,0 @@ -name: Publish the docs - -on: - push: - paths: - - 'docs/**' - - 'yarn.lock' - - 'package.json' - - '.github/workflows/publish-docs.yml' - -jobs: - build: - runs-on: ubuntu-latest - - steps: - - name: Checkout - uses: actions/checkout@v1 - - - name: Vuepress - run: | - yarn install - yarn docs:build - - - name: GitHub Pages - uses: JamesIves/github-pages-deploy-action@releases/v3 - with: - ACCESS_TOKEN: ${{ secrets.ACCESS_TOKEN }} - BRANCH: gh-pages - FOLDER: 'docs/.vuepress/dist' - CLEAN: true