{"record":{"id":"ff01a81bf6a8c4f1","repo":"elsa-workflows/elsa-core","slug":"cannot-determine-the-element-type-of-array-targettype","errorCode":null,"errorMessage":"Cannot determine the element type of array '{targetType}'.","messagePattern":"Cannot determine the element type of array '(.+?)'\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/modules/Elsa.Workflows.Core/Serialization/Converters/PolymorphicObjectConverter.cs","lineNumber":139,"sourceCode":"        {\n            // Remove the _type property name from the JSON, if any.\n            var parsedNode = JsonNode.Parse(ref reader)!;\n            if (parsedNode is JsonObject parsedModel) parsedModel.Remove(TypePropertyName);\n            return parsedNode.Deserialize(targetType, newOptions)!;\n        }\n\n        var isCollection = typeof(ICollection).IsAssignableFrom(targetType);\n\n        // Otherwise, deserialize the object as an array.\n        var elementType = targetType.IsArray\n            ? targetType.GetElementType()\n            : targetType.GenericTypeArguments.FirstOrDefault() ??\n              (isCollection // Could be a class derived from Collection<T> or List<T>.\n                  ? targetType.BaseType?.GenericTypeArguments[0]\n                  : targetType.GenericTypeArguments.FirstOrDefault()\n                    ?? typeof(object));\n        if (elementType == null)\n            throw new InvalidOperationException($\"Cannot determine the element type of array '{targetType}'.\");\n\n        var model = JsonElement.ParseValue(ref reader);\n        var referenceResolver = (newOptions.ReferenceHandler as CrossScopedReferenceHandler)?.GetResolver();\n\n        if (model.TryGetProperty(RefPropertyName, out var refProperty))\n        {\n            var refId = refProperty.GetString()!;\n            return referenceResolver?.ResolveReference(refId)!;\n        }\n\n        var values = model.TryGetProperty(ItemsPropertyName, out var itemsProp) ? itemsProp.EnumerateArray().ToList() : model.GetProperty(ValuesPropertyName).EnumerateArray().ToList();\n        var id = model.TryGetProperty(IdPropertyName, out var idProp) ? idProp.GetString() : default;\n        var collection = targetType.IsArray ? Array.CreateInstance(elementType, values.Count) : Activator.CreateInstance(targetType)!;\n        var index = 0;\n\n        if (id != null)\n            referenceResolver?.AddReference(id, collection);\n","sourceCodeStart":121,"sourceCodeEnd":157,"githubUrl":"https://github.com/elsa-workflows/elsa-core/blob/fe9217bdfa0e27f0e09e45006eb6898f616e513d/src/modules/Elsa.Workflows.Core/Serialization/Converters/PolymorphicObjectConverter.cs#L121-L157","documentation":"PolymorphicObjectConverter.Read, when deserializing a JSON array into a concrete collection type, must determine the element type via generic type arguments. When it cannot (no GenericTypeArguments and no derivable base-type argument), it throws InvalidOperationException. This is an internal invariant failure for exotic collection targets.","triggerScenarios":"Deserializing a JSON array into a target type whose element type cannot be inferred — e.g. a non-generic collection type deriving from Collection<T>/List<T> chains the converter cannot introspect, or a nested non-generic collection (src/modules/Elsa.Workflows.Core/Serialization/Converters/PolymorphicObjectConverter.cs:139).","commonSituations":"Custom collection classes without generic parameters; deserializing into MyCollection (non-generic) where Elsa expects T to be discoverable; refactored property types losing their generic argument.","solutions":["Use a generic collection type (List<T>, Collection<T>, T[]) for the target property.","If a custom collection is required, derive from Collection<T> or List<T> so the element type is discoverable.","Add a custom JsonConverter for the exotic collection type instead of relying on polymorphic inference.","Fallback behavior is typeof(object); confirm whether deserializing into List<object> is acceptable."],"exampleFix":"// before\npublic class MyItems : CollectionBase { ... } // non-generic, element type unknown\n\n// after\npublic class MyItems : Collection<MyItem> { ... }","handlingStrategy":"type-guard","validationCode":"var t = typeof(MyCollection);\nbool inferable = t.IsGenericType || (t.BaseType?.IsGenericType == true);\nif (!inferable) throw new InvalidOperationException($\"{t.Name} must be generic so its element type can be inferred\");","typeGuard":"static bool HasInferableElementType(Type t) => t.IsArray || t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IEnumerable<>));","tryCatchPattern":"try { var list = JsonSerializer.Deserialize<MyCollection>(json, options); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"Cannot determine the element type\")) { log.LogError(ex, \"Make the collection generic\"); }","preventionTips":["Use List<T>, T[], or Collection<T> for collection-typed workflow properties","Avoid non-generic legacy collection types in models","Add a serialization round-trip unit test for custom collection classes"],"tags":["csharp","json","serialization","generics","collections"],"backgroundTag":"internal-invariant-violation","analyzedSha":"fe9217bdfa0e27f0e09e45006eb6898f616e513d","analyzedAt":"2026-09-13T20:32:34.702Z","contentChangedAt":"2026-09-13T20:32:34.702Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}