Deserializing nested element list in XML - only first element returned.

37 views
Skip to first unread message

kd5ziy

unread,
Mar 30, 2018, 6:58:14 PM3/30/18
to RestSharp
Hello,

I just read the Contributing.md on the github for RestSharp.  I ran into this problem with nested generic lists with xml deserialization. (see: https://github.com/restsharp/RestSharp/issues/1101 )

Explanation:

Expected Behavior

Create list of nested object

Actual Behavior

Created list of nested object with 1 actual object.

Steps to Reproduce the Problem

  1. Create an xml to deserialize that contains a custom xml element within the value of the root element. Make sure that the custom element is a list (has multiples) and has attributes.
  2. Deserialize XML.

Example XML:
<orders order_id="1000052" publisher_id="8888" store_id="99999" billing_address_first_name="test" billing_address_last_name="tester" billing_address_street1="123 do not ship" billing_address_street2="" billing_address_city="Test City" billing_address_state_id="14" billing_address_postcode="80204" billing_address_country_id="1" billing_address_phone="5555550779" shipping_address_first_name="Test" shipping_address_last_name="Tester" shipping_address_street1="123 do not ship" shipping_address_street2="" shipping_address_city="test city" shipping_address_state_id="14" shipping_address_postcode="80204" shipping_address_country_id="1" shipping_address_phone="5555550779" shipping_address_business="0" shipping_address_validated="0" shipping_address_pobox="0" shipping_method_id="1000027" shipping_method="will distribute" print_cost="0.0000" retail_cost="0.0000" retail_amount="190.0000" shipping_amount="0.0000" process_markup_amount="0.0000" tax_amount="0.0000" total_amount="190.0000" giftcert_amount="0.0000" amount_due="190.0000" ip_address="" date_created="2018-03-09T08:32:54.473" last_modified="2018-03-26T10:39:41.033" email="" process_amount="190.0000" payment_method="CREDITCARD" authorized="1" confirmed="1" rendered="0" ordered="0" received="0" prepared="0" paid="1" shipped="0" cancelled="0" estimated_ship_date="2018-04-16T20:00:00" estimated_delivery_min_date="2018-04-16T20:00:00" estimated_delivery_max_date="2018-04-16T20:00:00" currency_code="USD" hide_pricing="0" currency_symbol="$" tax_name="Tax" print_method_list="None" printed="0" in_production_manager="0" production_status="Confirmed"> <order_retail_items order_retail_item_id="1000075" price="16.5000" price_overridden="0" base_product_price="16.5000" print_method="None" printed="0"> <retail_product retail_product_id="1000046" product_id="1000086" sku="test1" product_style_id="1001050" style="Vintage Red"> <order_retail_item_sizes order_retail_item_size_id="1000077" quantity="1" product_style_size_id="1007196" size="2XL" render_attempt="0" price="16.5000" product_cost="0.0000" product_price="16.5000" print_cost="0.0000" quantity_discount_amount="0.0000" print_name_numbers="0.0000" price_each="16.5000" product_price_each="16.5000" print_cost_each="0.0000" print_setup_each="0.0000"> <order_retail_item_size_namenumbers> <order_retail_item_size_add_ons /> </order_retail_item_size_namenumbers> </order_retail_item_sizes> </retail_product> </order_retail_items> <order_retail_items order_retail_item_id="1000076" price="15.0000" price_overridden="0" base_product_price="15.0000" print_method="None" printed="0"> <retail_product retail_product_id="1000047" product_id="1000087" sku="test2" product_style_id="1001052" style="Vintage Navy"> <order_retail_item_sizes order_retail_item_size_id="1000078" quantity="1" product_style_size_id="1007200" size="LG" render_attempt="0" price="15.0000" product_cost="0.0000" product_price="15.0000" print_cost="0.0000" quantity_discount_amount="0.0000" print_name_numbers="0.0000" price_each="15.0000" product_price_each="15.0000" print_cost_each="0.0000" print_setup_each="0.0000"> <order_retail_item_size_namenumbers> <order_retail_item_size_add_ons /> </order_retail_item_size_namenumbers> </order_retail_item_sizes> </retail_product> </order_retail_items> </orders> </OrderDetail>

Possible Solution:

I fixed this issue by switching two If/Else statements in XMLDeserializer.cs

I moved the block of code associated with line 264: "else if (type.IsGenericType)" below the next else if statement "else if (asType.IsSubclassOfRawGeneric(typeof(List<>)))".
This second block of code seems to handle this situation and the others that the type.IsGenericType does.

I can check in this change to a branch if this change is desired.



I would be happy to share what I changed to make this work.  If this is a desired change then I can post a pull request for this github repo.


Let me know or if I'm total off base with this.


Thanks,
James

Reply all
Reply to author
Forward
0 new messages