| | | 1 | | using System.Collections; |
| | | 2 | | using System.Collections.ObjectModel; |
| | | 3 | | using System.Reflection; |
| | | 4 | | using System.Text.Json; |
| | | 5 | | using Elsa.Extensions; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Common.Serialization; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Resolves serialization type aliases without loading arbitrary CLR type names. |
| | | 11 | | /// </summary> |
| | | 12 | | public static class SerializationTypeResolver |
| | | 13 | | { |
| | 1 | 14 | | private static readonly IDictionary<string, Type> GenericCollectionTypes = new Dictionary<string, Type>(StringCompar |
| | 1 | 15 | | { |
| | 1 | 16 | | ["IEnumerable"] = typeof(IEnumerable<>), |
| | 1 | 17 | | ["ICollection"] = typeof(ICollection<>), |
| | 1 | 18 | | ["IList"] = typeof(IList<>), |
| | 1 | 19 | | ["IReadOnlyCollection"] = typeof(IReadOnlyCollection<>), |
| | 1 | 20 | | ["IReadOnlyList"] = typeof(IReadOnlyList<>), |
| | 1 | 21 | | ["ISet"] = typeof(ISet<>), |
| | 1 | 22 | | ["List"] = typeof(List<>), |
| | 1 | 23 | | ["HashSet"] = typeof(HashSet<>), |
| | 1 | 24 | | ["Collection"] = typeof(Collection<>) |
| | 1 | 25 | | }; |
| | | 26 | | |
| | 1 | 27 | | private static readonly IDictionary<Type, string> GenericCollectionAliases = new Dictionary<Type, string> |
| | 1 | 28 | | { |
| | 1 | 29 | | [typeof(List<>)] = "List", |
| | 1 | 30 | | [typeof(HashSet<>)] = "HashSet", |
| | 1 | 31 | | [typeof(Collection<>)] = "Collection" |
| | 1 | 32 | | }; |
| | | 33 | | |
| | 1 | 34 | | private static readonly IDictionary<Type, Type> GenericCollectionInterfaceMappings = new Dictionary<Type, Type> |
| | 1 | 35 | | { |
| | 1 | 36 | | [typeof(IEnumerable<>)] = typeof(List<>), |
| | 1 | 37 | | [typeof(ICollection<>)] = typeof(List<>), |
| | 1 | 38 | | [typeof(IList<>)] = typeof(List<>), |
| | 1 | 39 | | [typeof(IReadOnlyCollection<>)] = typeof(List<>), |
| | 1 | 40 | | [typeof(IReadOnlyList<>)] = typeof(List<>), |
| | 1 | 41 | | [typeof(ISet<>)] = typeof(HashSet<>), |
| | 1 | 42 | | [typeof(IDictionary<,>)] = typeof(Dictionary<,>), |
| | 1 | 43 | | [typeof(IReadOnlyDictionary<,>)] = typeof(Dictionary<,>) |
| | 1 | 44 | | }; |
| | | 45 | | |
| | 1 | 46 | | private static readonly IDictionary<Type, Type> CollectionInterfaceMappings = new Dictionary<Type, Type> |
| | 1 | 47 | | { |
| | 1 | 48 | | [typeof(IEnumerable)] = typeof(List<object>), |
| | 1 | 49 | | [typeof(ICollection)] = typeof(List<object>), |
| | 1 | 50 | | [typeof(IList)] = typeof(List<object>), |
| | 1 | 51 | | [typeof(IDictionary)] = typeof(Dictionary<string, object>) |
| | 1 | 52 | | }; |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Resolves the specified serialization type alias. |
| | | 56 | | /// </summary> |
| | | 57 | | public static Type ResolveType(ISerializationTypeRegistry serializationTypeRegistry, string? typeAlias) |
| | | 58 | | { |
| | 4938 | 59 | | if (string.IsNullOrWhiteSpace(typeAlias)) |
| | 0 | 60 | | throw new JsonException("The serialization type alias is missing."); |
| | | 61 | | |
| | 4938 | 62 | | if (TryResolveType(serializationTypeRegistry, typeAlias, out var type)) |
| | 4938 | 63 | | return type; |
| | | 64 | | |
| | 0 | 65 | | throw new JsonException( |
| | 0 | 66 | | $"Unknown serialization type alias '{typeAlias}'. Only registered aliases and supported compound aliases can |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | /// <summary> |
| | | 70 | | /// Attempts to resolve the specified serialization type alias. |
| | | 71 | | /// </summary> |
| | | 72 | | public static bool TryResolveType(ISerializationTypeRegistry serializationTypeRegistry, string typeAlias, out Type t |
| | | 73 | | { |
| | 10016 | 74 | | IReadOnlyList<Type>? registeredTypes = null; |
| | 10016 | 75 | | return TryResolveType(serializationTypeRegistry, typeAlias, ref registeredTypes, out type); |
| | | 76 | | } |
| | | 77 | | |
| | | 78 | | private static bool TryResolveType(ISerializationTypeRegistry serializationTypeRegistry, string typeAlias, ref IRead |
| | | 79 | | { |
| | 10151 | 80 | | if (serializationTypeRegistry.TryGetType(typeAlias, out var registeredType)) |
| | | 81 | | { |
| | 10016 | 82 | | type = registeredType; |
| | 10016 | 83 | | return true; |
| | | 84 | | } |
| | | 85 | | |
| | 135 | 86 | | if (TryResolveArrayType(serializationTypeRegistry, typeAlias, ref registeredTypes, out var arrayType)) |
| | | 87 | | { |
| | 122 | 88 | | type = arrayType; |
| | 122 | 89 | | return true; |
| | | 90 | | } |
| | | 91 | | |
| | 13 | 92 | | if (TryResolveGenericCollectionType(serializationTypeRegistry, typeAlias, ref registeredTypes, out var genericCo |
| | | 93 | | { |
| | 13 | 94 | | type = genericCollectionType; |
| | 13 | 95 | | return true; |
| | | 96 | | } |
| | | 97 | | |
| | 0 | 98 | | if (TryResolveRegisteredLegacyTypeName(serializationTypeRegistry, typeAlias, ref registeredTypes, out var legacy |
| | | 99 | | { |
| | 0 | 100 | | type = legacyType; |
| | 0 | 101 | | return true; |
| | | 102 | | } |
| | | 103 | | |
| | 0 | 104 | | type = null!; |
| | 0 | 105 | | return false; |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | /// <summary> |
| | | 109 | | /// Attempts to return a serialization type alias that this resolver can read back. |
| | | 110 | | /// </summary> |
| | | 111 | | public static bool TryGetAlias(ISerializationTypeRegistry serializationTypeRegistry, Type type, out string alias) |
| | | 112 | | { |
| | 28043 | 113 | | if (serializationTypeRegistry.TryGetAlias(type, out alias!)) |
| | 23888 | 114 | | return true; |
| | | 115 | | |
| | 4155 | 116 | | if (type.IsArray) |
| | | 117 | | { |
| | 63 | 118 | | var elementType = type.GetElementType()!; |
| | | 119 | | |
| | 63 | 120 | | if (TryGetAlias(serializationTypeRegistry, elementType, out var elementTypeAlias)) |
| | | 121 | | { |
| | 63 | 122 | | alias = $"{elementTypeAlias}[]"; |
| | 63 | 123 | | return true; |
| | | 124 | | } |
| | | 125 | | } |
| | | 126 | | |
| | 4092 | 127 | | if (type is { IsGenericType: true, GenericTypeArguments.Length: 1 }) |
| | | 128 | | { |
| | 1295 | 129 | | var genericTypeDefinition = type.GetGenericTypeDefinition(); |
| | | 130 | | |
| | 1295 | 131 | | if (TryGetWritableGenericCollectionAlias(genericTypeDefinition, out var genericTypeAlias) && |
| | 1295 | 132 | | TryGetAlias(serializationTypeRegistry, type.GenericTypeArguments[0], out var elementTypeAlias)) |
| | | 133 | | { |
| | 1291 | 134 | | alias = $"{genericTypeAlias}<{elementTypeAlias}>"; |
| | 1291 | 135 | | return true; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | 2801 | 139 | | alias = null!; |
| | 2801 | 140 | | return false; |
| | | 141 | | } |
| | | 142 | | |
| | | 143 | | /// <summary> |
| | | 144 | | /// Attempts to map a supported collection interface type to an instantiable concrete type. |
| | | 145 | | /// </summary> |
| | | 146 | | public static bool TryGetInstantiableCollectionType(Type type, out Type instantiableType) |
| | | 147 | | { |
| | 0 | 148 | | if (type.IsGenericType) |
| | | 149 | | { |
| | 0 | 150 | | var genericTypeDefinition = type.GetGenericTypeDefinition(); |
| | 0 | 151 | | if (GenericCollectionInterfaceMappings.TryGetValue(genericTypeDefinition, out var instantiableGenericTypeDef |
| | | 152 | | { |
| | 0 | 153 | | instantiableType = instantiableGenericTypeDefinition.MakeGenericType(type.GenericTypeArguments); |
| | 0 | 154 | | return true; |
| | | 155 | | } |
| | | 156 | | } |
| | | 157 | | |
| | 0 | 158 | | if (CollectionInterfaceMappings.TryGetValue(type, out instantiableType!)) |
| | 0 | 159 | | return true; |
| | | 160 | | |
| | 0 | 161 | | instantiableType = null!; |
| | 0 | 162 | | return false; |
| | | 163 | | } |
| | | 164 | | |
| | | 165 | | private static bool TryResolveArrayType(ISerializationTypeRegistry serializationTypeRegistry, string typeAlias, ref |
| | | 166 | | { |
| | 135 | 167 | | type = null!; |
| | | 168 | | |
| | 135 | 169 | | if (!typeAlias.EndsWith("[]", StringComparison.Ordinal)) |
| | 13 | 170 | | return false; |
| | | 171 | | |
| | 122 | 172 | | var elementTypeAlias = typeAlias[..^2]; |
| | 122 | 173 | | if (!TryResolveType(serializationTypeRegistry, elementTypeAlias, ref registeredTypes, out var elementType)) |
| | 0 | 174 | | return false; |
| | | 175 | | |
| | 122 | 176 | | type = elementType.MakeArrayType(); |
| | 122 | 177 | | return true; |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private static bool TryResolveGenericCollectionType(ISerializationTypeRegistry serializationTypeRegistry, string typ |
| | | 181 | | { |
| | 13 | 182 | | type = null!; |
| | 13 | 183 | | var genericStart = typeAlias.IndexOf('<', StringComparison.Ordinal); |
| | | 184 | | |
| | 13 | 185 | | if (genericStart <= 0 || !typeAlias.EndsWith(">", StringComparison.Ordinal)) |
| | 0 | 186 | | return false; |
| | | 187 | | |
| | 13 | 188 | | var genericTypeAlias = typeAlias[..genericStart]; |
| | 13 | 189 | | if (!GenericCollectionTypes.TryGetValue(genericTypeAlias, out var genericTypeDefinition)) |
| | 0 | 190 | | return false; |
| | | 191 | | |
| | 13 | 192 | | var elementTypeAlias = typeAlias[(genericStart + 1)..^1]; |
| | 13 | 193 | | if (!TryResolveType(serializationTypeRegistry, elementTypeAlias, ref registeredTypes, out var elementType)) |
| | 0 | 194 | | return false; |
| | | 195 | | |
| | 13 | 196 | | type = genericTypeDefinition.MakeGenericType(elementType); |
| | 13 | 197 | | return true; |
| | | 198 | | } |
| | | 199 | | |
| | | 200 | | private static bool TryGetWritableGenericCollectionAlias(Type genericTypeDefinition, out string alias) |
| | | 201 | | { |
| | 1295 | 202 | | if (GenericCollectionAliases.TryGetValue(genericTypeDefinition, out alias!)) |
| | 637 | 203 | | return true; |
| | | 204 | | |
| | 658 | 205 | | if (GenericCollectionInterfaceMappings.TryGetValue(genericTypeDefinition, out var instantiableGenericTypeDefinit |
| | 658 | 206 | | GenericCollectionAliases.TryGetValue(instantiableGenericTypeDefinition, out alias!)) |
| | | 207 | | { |
| | 654 | 208 | | return true; |
| | | 209 | | } |
| | | 210 | | |
| | 4 | 211 | | alias = null!; |
| | 4 | 212 | | return false; |
| | | 213 | | } |
| | | 214 | | |
| | | 215 | | private static bool TryResolveRegisteredLegacyTypeName(ISerializationTypeRegistry serializationTypeRegistry, string |
| | | 216 | | { |
| | 0 | 217 | | registeredTypes ??= GetRegisteredTypes(serializationTypeRegistry); |
| | 0 | 218 | | var registeredTypeSnapshot = registeredTypes; |
| | | 219 | | |
| | 0 | 220 | | if (TryResolveRegisteredSimpleAssemblyQualifiedName(registeredTypeSnapshot, typeAlias, out type)) |
| | 0 | 221 | | return true; |
| | | 222 | | |
| | 0 | 223 | | if (TryResolveLegacyGenericCollectionTypeName(serializationTypeRegistry, typeAlias, ref registeredTypes, out typ |
| | 0 | 224 | | return true; |
| | | 225 | | |
| | | 226 | | Type? resolvedType; |
| | | 227 | | |
| | | 228 | | try |
| | | 229 | | { |
| | 0 | 230 | | resolvedType = Type.GetType( |
| | 0 | 231 | | typeAlias, |
| | 0 | 232 | | assemblyName => ResolveAssembly(registeredTypeSnapshot, assemblyName), |
| | 0 | 233 | | (assembly, typeName, ignoreCase) => ResolveType(registeredTypeSnapshot, assembly, typeName, ignoreCase), |
| | 0 | 234 | | false); |
| | 0 | 235 | | } |
| | 0 | 236 | | catch (Exception e) when (e is ArgumentException or FileLoadException) |
| | | 237 | | { |
| | 0 | 238 | | resolvedType = null; |
| | 0 | 239 | | } |
| | | 240 | | |
| | 0 | 241 | | type = resolvedType!; |
| | 0 | 242 | | return resolvedType != null; |
| | | 243 | | } |
| | | 244 | | |
| | | 245 | | private static IReadOnlyList<Type> GetRegisteredTypes(ISerializationTypeRegistry serializationTypeRegistry) |
| | | 246 | | { |
| | 0 | 247 | | return serializationTypeRegistry.ListTypes().ToArray(); |
| | | 248 | | } |
| | | 249 | | |
| | | 250 | | private static bool TryResolveRegisteredSimpleAssemblyQualifiedName(IEnumerable<Type> registeredTypes, string typeAl |
| | | 251 | | { |
| | 0 | 252 | | type = registeredTypes.FirstOrDefault(x => |
| | 0 | 253 | | string.Equals(x.GetSimpleAssemblyQualifiedName(), typeAlias, StringComparison.Ordinal) || |
| | 0 | 254 | | string.Equals(x.AssemblyQualifiedName, typeAlias, StringComparison.Ordinal))!; |
| | | 255 | | |
| | 0 | 256 | | return type != null; |
| | | 257 | | } |
| | | 258 | | |
| | | 259 | | private static bool TryResolveLegacyGenericCollectionTypeName(ISerializationTypeRegistry serializationTypeRegistry, |
| | | 260 | | { |
| | 0 | 261 | | type = null!; |
| | | 262 | | |
| | 0 | 263 | | foreach (var genericTypeDefinition in GenericCollectionTypes.Values) |
| | | 264 | | { |
| | 0 | 265 | | var prefix = $"{genericTypeDefinition.FullName}[["; |
| | 0 | 266 | | var separatorIndex = typeAlias.LastIndexOf("]], ", StringComparison.Ordinal); |
| | | 267 | | |
| | 0 | 268 | | if (!typeAlias.StartsWith(prefix, StringComparison.Ordinal) || separatorIndex <= prefix.Length) |
| | | 269 | | continue; |
| | | 270 | | |
| | 0 | 271 | | var assemblyName = typeAlias[(separatorIndex + 4)..].Split(',')[0]; |
| | 0 | 272 | | if (!string.Equals(assemblyName, genericTypeDefinition.Assembly.GetName().Name, StringComparison.Ordinal)) |
| | | 273 | | continue; |
| | | 274 | | |
| | 0 | 275 | | var elementTypeAlias = typeAlias[prefix.Length..separatorIndex]; |
| | 0 | 276 | | if (!TryResolveType(serializationTypeRegistry, elementTypeAlias, ref registeredTypes, out var elementType)) |
| | 0 | 277 | | return false; |
| | | 278 | | |
| | | 279 | | // The resolver only closes known collection definitions over registered element types. |
| | | 280 | | #pragma warning disable IL2055 |
| | 0 | 281 | | type = genericTypeDefinition.MakeGenericType(elementType); |
| | | 282 | | #pragma warning restore IL2055 |
| | 0 | 283 | | return true; |
| | | 284 | | } |
| | | 285 | | |
| | 0 | 286 | | return false; |
| | 0 | 287 | | } |
| | | 288 | | |
| | | 289 | | private static Assembly? ResolveAssembly(IEnumerable<Type> registeredTypes, AssemblyName assemblyName) |
| | | 290 | | { |
| | 0 | 291 | | var coreLibAssembly = typeof(List<>).Assembly; |
| | 0 | 292 | | if (AssemblyName.ReferenceMatchesDefinition(coreLibAssembly.GetName(), assemblyName)) |
| | 0 | 293 | | return coreLibAssembly; |
| | | 294 | | |
| | 0 | 295 | | return registeredTypes |
| | 0 | 296 | | .Select(x => x.Assembly) |
| | 0 | 297 | | .Distinct() |
| | 0 | 298 | | .FirstOrDefault(x => AssemblyName.ReferenceMatchesDefinition(x.GetName(), assemblyName)); |
| | | 299 | | } |
| | | 300 | | |
| | | 301 | | private static Type? ResolveType(IEnumerable<Type> registeredTypes, Assembly? assembly, string typeName, bool ignore |
| | | 302 | | { |
| | 0 | 303 | | if (assembly == typeof(List<>).Assembly) |
| | | 304 | | { |
| | 0 | 305 | | var genericCollectionType = GenericCollectionTypes.Values.FirstOrDefault(x => |
| | 0 | 306 | | x.FullName != null && string.Equals(x.FullName, typeName, ignoreCase ? StringComparison.OrdinalIgnoreCas |
| | | 307 | | |
| | 0 | 308 | | if (genericCollectionType != null) |
| | 0 | 309 | | return genericCollectionType; |
| | | 310 | | } |
| | | 311 | | |
| | 0 | 312 | | return registeredTypes.FirstOrDefault(x => |
| | 0 | 313 | | x.Assembly == assembly && |
| | 0 | 314 | | x.FullName != null && |
| | 0 | 315 | | (string.Equals(x.FullName, typeName, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordi |
| | 0 | 316 | | string.Equals(x.FullName.Replace('+', '.'), typeName, ignoreCase ? StringComparison.OrdinalIgnoreCase : Str |
| | | 317 | | } |
| | | 318 | | } |