Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix UmbracoMapper InvalidOperationException due to concurrent modification of inner dictionary #15849

Merged
merged 1 commit into from
Mar 14, 2024
Merged
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
16 changes: 8 additions & 8 deletions src/Umbraco.Infrastructure/Mapping/UmbracoMapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ public class UmbracoMapper : IUmbracoMapper
// note
//
// the outer dictionary *can* be modified, see GetCtor and GetMap, hence have to be ConcurrentDictionary
// the inner dictionaries are never modified and therefore can be simple Dictionary
private readonly ConcurrentDictionary<Type, Dictionary<Type, Func<object, MapperContext, object>>> _ctors =
// the inner dictionaries can also be modified, see GetCtor and therefore also needs to be a ConcurrentDictionary
private readonly ConcurrentDictionary<Type, ConcurrentDictionary<Type, Func<object, MapperContext, object>>> _ctors =
new();

private readonly ConcurrentDictionary<Type, ConcurrentDictionary<Type, Action<object, object, MapperContext>>> _maps =
Expand Down Expand Up @@ -129,7 +129,7 @@ public void Define<TSource, TTarget>(
Type sourceType = typeof(TSource);
Type targetType = typeof(TTarget);

Dictionary<Type, Func<object, MapperContext, object>> sourceCtors = DefineCtors(sourceType);
ConcurrentDictionary<Type, Func<object, MapperContext, object>> sourceCtors = DefineCtors(sourceType);
if (ctor != null)
{
sourceCtors[targetType] = (source, context) => ctor((TSource)source, context)!;
Expand All @@ -139,8 +139,8 @@ public void Define<TSource, TTarget>(
sourceMaps[targetType] = (source, target, context) => map((TSource)source, (TTarget)target, context);
}

private Dictionary<Type, Func<object, MapperContext, object>> DefineCtors(Type sourceType) =>
_ctors.GetOrAdd(sourceType, _ => new Dictionary<Type, Func<object, MapperContext, object>>());
private ConcurrentDictionary<Type, Func<object, MapperContext, object>> DefineCtors(Type sourceType) =>
_ctors.GetOrAdd(sourceType, _ => new ConcurrentDictionary<Type, Func<object, MapperContext, object>>());

private ConcurrentDictionary<Type, Action<object, object, MapperContext>> DefineMaps(Type sourceType) =>
_maps.GetOrAdd(sourceType, _ => new ConcurrentDictionary<Type, Action<object, object, MapperContext>>());
Expand Down Expand Up @@ -391,15 +391,15 @@ public TTarget Map<TSource, TTarget>(TSource source, TTarget target, MapperConte
return null;
}

if (_ctors.TryGetValue(sourceType, out Dictionary<Type, Func<object, MapperContext, object>>? sourceCtor) &&
if (_ctors.TryGetValue(sourceType, out ConcurrentDictionary<Type, Func<object, MapperContext, object>>? sourceCtor) &&
sourceCtor.TryGetValue(targetType, out Func<object, MapperContext, object>? ctor))
{
return ctor;
}

// we *may* run this more than once but it does not matter
ctor = null;
foreach ((Type stype, Dictionary<Type, Func<object, MapperContext, object>> sctors) in _ctors)
foreach ((Type stype, ConcurrentDictionary<Type, Func<object, MapperContext, object>> sctors) in _ctors)
{
if (!stype.IsAssignableFrom(sourceType))
{
Expand Down Expand Up @@ -427,7 +427,7 @@ public TTarget Map<TSource, TTarget>(TSource source, TTarget target, MapperConte
{
if (!v.ContainsKey(c.Key))
{
v.Add(c.Key, c.Value);
v.TryAdd(c.Key, c.Value);
}
}

Expand Down
Loading