Skip to content

Commit

Permalink
Merge pull request #460 from slimberg91/fix/ValueProviderExecutor
Browse files Browse the repository at this point in the history
Avoid NullReferenceExceptions in ValueProviderExecutor
  • Loading branch information
Toxantron authored Sep 18, 2024
2 parents 207278b + 4e89aec commit af95c00
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions src/Moryx/Configuration/ValueProvider/ValueProviderExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ public void FillEmpty(object obj)
/// <exception cref="ArgumentNullException"></exception>
public static void Execute(object targetObject, ValueProviderExecutorSettings settings)
{
if (targetObject is null)
{
throw new ArgumentNullException(nameof(targetObject));
}

if (settings.Providers == null)
{
throw new ArgumentNullException(nameof(settings.Providers));
Expand All @@ -55,30 +60,34 @@ private static void Iterate(object target, ValueProviderExecutorSettings setting
{
foreach (var settingsProvider in settings.Providers)
{
if (settingsProvider.Handle(target, property) == ValueProviderResult.Handled)
try
{
break;
if (settingsProvider.Handle(target, property) == ValueProviderResult.Handled)
{
break;
}
}
catch (Exception)
{
// TODO: Restrict exception type
// TODO: Consider enabling logging
}
}

var value = property.GetValue(target);

if (property.PropertyType.IsValueType && !property.PropertyType.IsPrimitive ||
property.PropertyType.IsClass &&
property.PropertyType != typeof(string) &&
!(value is IEnumerable))
// Iterate each item of an enumerable
if (value is IEnumerable enumerable)
{
Iterate(value, settings);
}

if (value is IEnumerable)
{
var enumerable = value as IEnumerable;
foreach (var item in enumerable)
{
Iterate(item, settings);
if (item != null)
Iterate(item, settings);
}
}
else if (value != null && property.PropertyType.IsClass && property.PropertyType != typeof(string))
{
Iterate(value, settings);
}
}
}

Expand Down

0 comments on commit af95c00

Please sign in to comment.