From 19d8070d6ce046f8dd3aecd34b30077fbfe6c29d Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 19 Apr 2026 18:41:14 -0500 Subject: handle nulls in DiffWith --- src/slskd/Common/CommonExtensions.cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'src') diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index 88e6d3c8..1cdf9854 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -116,6 +116,16 @@ namespace slskd throw new InvalidCastException($"Unable to diff types {left?.GetType()} and {right?.GetType()}"); } + if (left is null || right is null) + { + if (left is null == right is null) + { + return []; + } + + return [(null, parentFqn ?? string.Empty, left, right)]; + } + var differences = new List<(PropertyInfo Property, string FQN, object Left, object Right)>(); foreach (var prop in left?.GetType().GetProperties()) @@ -125,6 +135,16 @@ namespace slskd var propType = prop.PropertyType; var fqn = string.IsNullOrEmpty(parentFqn) ? prop.Name : string.Join(".", parentFqn, prop.Name); + if (leftVal is null || rightVal is null) + { + if (leftVal is null == rightVal is null) + { + continue; + } + + differences.Add((prop, fqn, leftVal, rightVal)); + } + if (propType.IsArray || (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Dictionary<,>))) { if (leftVal.ToJson() != rightVal.ToJson()) -- cgit v1.2.3 From 8ef80b442688288a40fb8c8fa8392249dc6ebb10 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 19 Apr 2026 18:50:04 -0500 Subject: support null propertyinfo --- src/slskd/Application.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/slskd/Application.cs b/src/slskd/Application.cs index a8a7883a..86dd9dd1 100644 --- a/src/slskd/Application.cs +++ b/src/slskd/Application.cs @@ -1452,7 +1452,7 @@ namespace slskd foreach (var (property, fqn, left, right) in diff) { - static bool HasAttribute(PropertyInfo property) => property.CustomAttributes.Any(a => a.AttributeType == typeof(T)); + static bool HasAttribute(PropertyInfo property) => property?.CustomAttributes.Any(a => a.AttributeType == typeof(T)) ?? false; var requiresRestart = HasAttribute(property); var requiresReconnect = HasAttribute(property); -- cgit v1.2.3 From d415e5ca568f55cf9c45e84b05892219f9e1465a Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 19 Apr 2026 18:50:28 -0500 Subject: check for nulls first --- src/slskd/Common/CommonExtensions.cs | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/slskd/Common/CommonExtensions.cs b/src/slskd/Common/CommonExtensions.cs index 1cdf9854..77db91ef 100644 --- a/src/slskd/Common/CommonExtensions.cs +++ b/src/slskd/Common/CommonExtensions.cs @@ -111,11 +111,6 @@ namespace slskd /// A list of differences between the two objects. public static IEnumerable<(PropertyInfo Property, string FQN, object Left, object Right)> DiffWith(this object left, object right, string parentFqn = null) { - if (left?.GetType() != right?.GetType()) - { - throw new InvalidCastException($"Unable to diff types {left?.GetType()} and {right?.GetType()}"); - } - if (left is null || right is null) { if (left is null == right is null) @@ -126,9 +121,14 @@ namespace slskd return [(null, parentFqn ?? string.Empty, left, right)]; } + if (left.GetType() != right.GetType()) + { + throw new InvalidCastException($"Unable to diff types {left.GetType()} and {right.GetType()}"); + } + var differences = new List<(PropertyInfo Property, string FQN, object Left, object Right)>(); - foreach (var prop in left?.GetType().GetProperties()) + foreach (var prop in left.GetType().GetProperties()) { var leftVal = prop.GetValue(left); var rightVal = prop.GetValue(right); @@ -143,6 +143,7 @@ namespace slskd } differences.Add((prop, fqn, leftVal, rightVal)); + continue; } if (propType.IsArray || (propType.IsGenericType && propType.GetGenericTypeDefinition() == typeof(Dictionary<,>))) -- cgit v1.2.3