Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
146 changes: 117 additions & 29 deletions src/RestSharp.Serializers.Xml/XmlDeserializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,16 @@
var root = doc.Root;
var rootElement = response.RootElement ?? RootElement;

if (rootElement != null && doc.Root != null)
root = doc.Root.DescendantsAndSelf(rootElement.AsNamespaced(Namespace)).SingleOrDefault();
if (rootElement != null && doc.Root != null) {
var namespacedRoot = rootElement.AsNamespaced(Namespace);
// Prefer the shallowest match to avoid nested elements with the same name
root = namespacedRoot != null
? doc.Root.Element(namespacedRoot)
?? doc.Root.DescendantsAndSelf(namespacedRoot)
.OrderBy(e => e.Ancestors().Count())
.FirstOrDefault()
: null;
}

// autodetect xml namespace
if (Namespace.IsEmpty())
Expand Down Expand Up @@ -76,10 +84,15 @@
? new(XNamespace.None.GetName(a.Name.LocalName), a.Value)
: a
)
.Where(a => a != null)
);
}
}

static bool IsValidXmlElementName(string name) =>
// Generic type names contain backtick (e.g., "List`1") which is invalid in XML element names
!name.Contains('`');

protected virtual object Map(object x, XElement? root) {
var objType = x.GetType();
var props = objType.GetProperties();
Expand Down Expand Up @@ -177,12 +190,12 @@
}
}
else if (type.IsEnum) {
var converted = type.AsType().FindEnumValue(value.ToString()!, Culture);
var converted = type.AsType().FindEnumValue(value.ToString(), Culture);

Check warning on line 193 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net9.0)

Possible null reference argument for parameter 'value' in 'object? ReflectionExtensions.FindEnumValue(Type type, string value, CultureInfo culture)'.

Check warning on line 193 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net8.0)

Possible null reference argument for parameter 'value' in 'object? ReflectionExtensions.FindEnumValue(Type type, string value, CultureInfo culture)'.

Check warning on line 193 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net10.0)

Possible null reference argument for parameter 'value' in 'object? ReflectionExtensions.FindEnumValue(Type type, string value, CultureInfo culture)'.

Check warning on line 193 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net8.0)

Possible null reference argument for parameter 'value' in 'object? ReflectionExtensions.FindEnumValue(Type type, string value, CultureInfo culture)'.

Check warning on line 193 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net9.0)

Possible null reference argument for parameter 'value' in 'object? ReflectionExtensions.FindEnumValue(Type type, string value, CultureInfo culture)'.

Check warning on line 193 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net10.0)

Possible null reference argument for parameter 'value' in 'object? ReflectionExtensions.FindEnumValue(Type type, string value, CultureInfo culture)'.

prop.SetValue(x, converted, null);
}
else if (asType == typeof(Uri)) {
var uri = new Uri(value.ToString()!, UriKind.RelativeOrAbsolute);
var uri = new Uri(value.ToString(), UriKind.RelativeOrAbsolute);

Check warning on line 198 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net9.0)

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString, UriKind uriKind)'.

Check warning on line 198 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net8.0)

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString, UriKind uriKind)'.

Check warning on line 198 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net10.0)

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString, UriKind uriKind)'.

Check warning on line 198 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net8.0)

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString, UriKind uriKind)'.

Check warning on line 198 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net9.0)

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString, UriKind uriKind)'.

Check warning on line 198 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net10.0)

Possible null reference argument for parameter 'uriString' in 'Uri.Uri(string uriString, UriKind uriKind)'.

prop.SetValue(x, uri, null);
}
Expand All @@ -191,8 +204,8 @@
}
else if (asType == typeof(DateTime)) {
value = DateFormat.IsNotEmpty()
? DateTime.ParseExact(value.ToString()!, DateFormat!, Culture)
: DateTime.Parse(value.ToString()!, Culture);
? DateTime.ParseExact(value.ToString(), DateFormat!, Culture)

Check warning on line 207 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net9.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.ParseExact(string s, string format, IFormatProvider? provider)'.

Check warning on line 207 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net8.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.ParseExact(string s, string format, IFormatProvider? provider)'.

Check warning on line 207 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net10.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.ParseExact(string s, string format, IFormatProvider? provider)'.

Check warning on line 207 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net8.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.ParseExact(string s, string format, IFormatProvider? provider)'.

Check warning on line 207 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net9.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.ParseExact(string s, string format, IFormatProvider? provider)'.

Check warning on line 207 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net10.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.ParseExact(string s, string format, IFormatProvider? provider)'.
: DateTime.Parse(value.ToString(), Culture);

Check warning on line 208 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net9.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.Parse(string s, IFormatProvider? provider)'.

Check warning on line 208 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net8.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.Parse(string s, IFormatProvider? provider)'.

Check warning on line 208 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net10.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.Parse(string s, IFormatProvider? provider)'.

Check warning on line 208 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net8.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.Parse(string s, IFormatProvider? provider)'.

Check warning on line 208 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net9.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.Parse(string s, IFormatProvider? provider)'.

Check warning on line 208 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net10.0)

Possible null reference argument for parameter 's' in 'DateTime DateTime.Parse(string s, IFormatProvider? provider)'.

prop.SetValue(x, value, null);
}
Expand All @@ -219,24 +232,35 @@
}
}
else if (asType == typeof(decimal)) {
value = decimal.Parse(value.ToString()!, Culture);
value = decimal.Parse(value.ToString(), Culture);

Check warning on line 235 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net9.0)

Possible null reference argument for parameter 's' in 'decimal decimal.Parse(string s, IFormatProvider? provider)'.

Check warning on line 235 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net8.0)

Possible null reference argument for parameter 's' in 'decimal decimal.Parse(string s, IFormatProvider? provider)'.

Check warning on line 235 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net10.0)

Possible null reference argument for parameter 's' in 'decimal decimal.Parse(string s, IFormatProvider? provider)'.

Check warning on line 235 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net8.0)

Possible null reference argument for parameter 's' in 'decimal decimal.Parse(string s, IFormatProvider? provider)'.

Check warning on line 235 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net9.0)

Possible null reference argument for parameter 's' in 'decimal decimal.Parse(string s, IFormatProvider? provider)'.

Check warning on line 235 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net10.0)

Possible null reference argument for parameter 's' in 'decimal decimal.Parse(string s, IFormatProvider? provider)'.
prop.SetValue(x, value, null);
}
else if (asType == typeof(Guid)) {
var raw = value.ToString();

value = string.IsNullOrEmpty(raw) ? Guid.Empty : new(value.ToString()!);
value = string.IsNullOrEmpty(raw) ? Guid.Empty : new(value.ToString());

Check warning on line 241 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net9.0)

Possible null reference argument for parameter 'g' in 'Guid.Guid(string g)'.

Check warning on line 241 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net8.0)

Possible null reference argument for parameter 'g' in 'Guid.Guid(string g)'.

Check warning on line 241 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net10.0)

Possible null reference argument for parameter 'g' in 'Guid.Guid(string g)'.

Check warning on line 241 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net8.0)

Possible null reference argument for parameter 'g' in 'Guid.Guid(string g)'.

Check warning on line 241 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net9.0)

Possible null reference argument for parameter 'g' in 'Guid.Guid(string g)'.

Check warning on line 241 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net10.0)

Possible null reference argument for parameter 'g' in 'Guid.Guid(string g)'.

prop.SetValue(x, value, null);
}
else if (asType == typeof(TimeSpan)) {
var timeSpan = XmlConvert.ToTimeSpan(value.ToString()!);
var timeSpan = XmlConvert.ToTimeSpan(value.ToString());

Check warning on line 246 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net9.0)

Possible null reference argument for parameter 's' in 'TimeSpan XmlConvert.ToTimeSpan(string s)'.

Check warning on line 246 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net8.0)

Possible null reference argument for parameter 's' in 'TimeSpan XmlConvert.ToTimeSpan(string s)'.

Check warning on line 246 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net10.0)

Possible null reference argument for parameter 's' in 'TimeSpan XmlConvert.ToTimeSpan(string s)'.

Check warning on line 246 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net8.0)

Possible null reference argument for parameter 's' in 'TimeSpan XmlConvert.ToTimeSpan(string s)'.

Check warning on line 246 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net9.0)

Possible null reference argument for parameter 's' in 'TimeSpan XmlConvert.ToTimeSpan(string s)'.

Check warning on line 246 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net10.0)

Possible null reference argument for parameter 's' in 'TimeSpan XmlConvert.ToTimeSpan(string s)'.

prop.SetValue(x, timeSpan, null);
}
else if (type.IsGenericType) {
var list = (IList)Activator.CreateInstance(asType)!;
var container = GetElementByName(root, name);
XElement? container = null;

// First check if root itself is the container (matches property name)
if (root != null && name?.LocalName != null) {
var rootName = root.Name.LocalName;
if (rootName.Equals(name.LocalName, StringComparison.OrdinalIgnoreCase)) {
container = root;
}
}

// If root is not the container, try to find it as a child element
container ??= GetElementByName(root, name);

if (container?.HasElements == true) {
var first = container.Elements().FirstOrDefault();
Expand All @@ -261,7 +285,7 @@
else {
//fallback to type converters if possible

if (TryGetFromString(value.ToString()!, out var result, asType)) {
if (TryGetFromString(value.ToString(), out var result, asType)) {

Check warning on line 288 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net9.0)

Possible null reference argument for parameter 'inputString' in 'bool XmlDeserializer.TryGetFromString(string inputString, out object? result, Type type)'.

Check warning on line 288 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net8.0)

Possible null reference argument for parameter 'inputString' in 'bool XmlDeserializer.TryGetFromString(string inputString, out object? result, Type type)'.

Check warning on line 288 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net10.0)

Possible null reference argument for parameter 'inputString' in 'bool XmlDeserializer.TryGetFromString(string inputString, out object? result, Type type)'.

Check warning on line 288 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net8.0)

Possible null reference argument for parameter 'inputString' in 'bool XmlDeserializer.TryGetFromString(string inputString, out object? result, Type type)'.

Check warning on line 288 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-linux (net9.0)

Possible null reference argument for parameter 'inputString' in 'bool XmlDeserializer.TryGetFromString(string inputString, out object? result, Type type)'.

Check warning on line 288 in src/RestSharp.Serializers.Xml/XmlDeserializer.cs

View workflow job for this annotation

GitHub Actions / test-windows (net10.0)

Possible null reference argument for parameter 'inputString' in 'bool XmlDeserializer.TryGetFromString(string inputString, out object? result, Type type)'.
prop.SetValue(x, result, null);
}
else {
Expand Down Expand Up @@ -306,37 +330,40 @@

var list = (IList)Activator.CreateInstance(type)!;

IList<XElement> elements = root.Descendants(t.Name.AsNamespaced(Namespace)).ToList();

var name = t.Name;
var attribute = t.GetAttribute<DeserializeAsAttribute>();

if (attribute != null)
name = attribute.Name;

if (!elements.Any()) {
var lowerName = name?.ToLower(Culture).AsNamespaced(Namespace);
// Try to find a container element first using the property name
XElement? container = null;

elements = root.Descendants(lowerName).ToList();
// Try the property name first (skip if it contains invalid XML name characters like ` in generic types)
if (IsValidXmlElementName(propName)) {
container = GetElementByName(root, propName.AsNamespaced(Namespace));
}

if (!elements.Any()) {
var camelName = name?.ToCamelCase(Culture).AsNamespaced(Namespace);
// Check if root itself matches the container naming
if (container == null && IsValidXmlElementName(propName)) {
var rootName = root.Name.LocalName;

elements = root.Descendants(camelName).ToList();
if (rootName.Equals(propName, StringComparison.OrdinalIgnoreCase)) {
container = root;
}
}

if (!elements.Any())
elements = root.Descendants()
.Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == name)
.ToList();
IList<XElement> elements;

if (!elements.Any()) {
var lowerName = name?.ToLower(Culture).AsNamespaced(Namespace);

elements = root.Descendants()
.Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == lowerName)
.ToList();
// If we found a specific container, use Elements() to get only direct children
// This prevents nested lists from being incorrectly flattened
if (container is { HasElements: true }) {
elements = TryFindElementsByNameVariations(container, t.Name, name, useDirectChildrenOnly: true);
}
else {
// No container found - use Descendants() for backward compatibility
// This handles cases where items are nested at varying depths without a clear container
elements = TryFindElementsByNameVariations(root, t.Name, name, useDirectChildrenOnly: false);
}

PopulateListFromElements(t, elements, list);
Expand All @@ -349,6 +376,67 @@
return list;
}

IList<XElement> TryFindElementsByNameVariations(XElement source, string typeName, string? itemName, bool useDirectChildrenOnly) {
IList<XElement> elements;

if (useDirectChildrenOnly) {
// Use Elements() for direct children only
elements = source.Elements(typeName.AsNamespaced(Namespace)).ToList();

if (!elements.Any()) {
var lowerName = itemName?.ToLower(Culture).AsNamespaced(Namespace);
elements = source.Elements(lowerName).ToList();
}

if (!elements.Any()) {
var camelName = itemName?.ToCamelCase(Culture).AsNamespaced(Namespace);
elements = source.Elements(camelName).ToList();
}

if (!elements.Any()) {
elements = source.Elements()
.Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == itemName)
.ToList();
}

if (!elements.Any()) {
var lowerName = itemName?.ToLower(Culture);
elements = source.Elements()
.Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == lowerName)
.ToList();
}
}
else {
// Use Descendants() for backward compatibility when no container is found
elements = source.Descendants(typeName.AsNamespaced(Namespace)).ToList();

if (!elements.Any()) {
var lowerName = itemName?.ToLower(Culture).AsNamespaced(Namespace);
elements = source.Descendants(lowerName).ToList();
}

if (!elements.Any()) {
var camelName = itemName?.ToCamelCase(Culture).AsNamespaced(Namespace);
elements = source.Descendants(camelName).ToList();
}

if (!elements.Any()) {
elements = source.Descendants()
.Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == itemName)
.ToList();
}

if (!elements.Any()) {
var lowerName = itemName?.ToLower(Culture);
elements = source.Descendants()
.Where(e => e.Name.LocalName.RemoveUnderscoresAndDashes() == lowerName)
.ToList();
}
}

return elements;
}

protected virtual object? CreateAndMap(Type t, XElement element) {
object? item;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
namespace RestSharp.Tests.Serializers.Xml.SampleClasses;

// Test classes for nested element bugs

public class Item {
public int Id { get; set; }
public List<Item> SubItems { get; set; }
}

public class ItemContainer {
public List<Item> Items { get; set; } = new();
}

public class ItemWithGroup {
public int Id { get; set; }
public ItemGroup Group { get; set; }
}

public class ItemGroup {
public List<Item> Items { get; set; }
}

public class ItemsResponse {
public List<ItemWithGroup> Items { get; set; } = new();
}
Loading
Loading