{"record":{"id":"035e93493db2e351","repo":"stride3d/stride","slug":"invalid-path-0-for-action-1-expecting-last-path-to-be-a","errorCode":null,"errorMessage":"Invalid path [{0}] for action [{1}]. Expecting last path to be a collection item","messagePattern":"Invalid path \\[(.+?)\\] for action \\[(.+?)\\]\\. Expecting last path to be a collection item","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Reflection/MemberPath.cs","lineNumber":240,"sourceCode":"        ArgumentNullException.ThrowIfNull(rootObject);\n        if (rootObject.GetType().IsValueType) throw new ArgumentException(\"Value type for root objects are not supported\", nameof(rootObject));\n        if (actionType != MemberPathAction.ValueSet && actionType != MemberPathAction.CollectionAdd && value != null)\n        {\n            throw new ArgumentException(\"Value must be null for action != (MemberActionType.SetValue || MemberPathAction.CollectionAdd)\");\n        }\n\n        if (items == null || items.Count == 0)\n        {\n            throw new InvalidOperationException(\"This instance doesn't contain any path. Use Push() methods to populate paths\");\n        }\n\n        var lastItem = items[^1];\n        switch (actionType)\n        {\n            case MemberPathAction.CollectionAdd:\n                if (lastItem is not CollectionPathItem)\n                {\n                    throw new ArgumentException(\"Invalid path [{0}] for action [{1}]. Expecting last path to be a collection item\".ToFormat(this, actionType));\n                }\n                break;\n            case MemberPathAction.CollectionRemove:\n                if (lastItem is not (CollectionPathItem or ArrayPathItem))\n                {\n                    throw new ArgumentException(\"Invalid path [{0}] for action [{1}]. Expecting last path to be a collection/array item\".ToFormat(this, actionType));\n                }\n                break;\n\n            case MemberPathAction.DictionaryRemove:\n                if (lastItem is not DictionaryPathItem)\n                {\n                    throw new ArgumentException(\"Invalid path [{0}] for action [{1}]. Expecting last path to be a dictionary item\".ToFormat(this, actionType));\n                }\n                break;\n        }\n\n        var stack = stackTLS;","sourceCodeStart":222,"sourceCodeEnd":258,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Reflection/MemberPath.cs#L222-L258","documentation":"MemberPathAction.CollectionAdd operates on the element referenced by the LAST path segment, which must be a CollectionPathItem. Apply() pattern-matches the final item and throws ArgumentException when the path ends on a plain member, dictionary item, or array item instead.","triggerScenarios":"Calling Apply(root, MemberPathAction.CollectionAdd, value) on a path whose last pushed segment is a MemberPathItem (e.g. path 'MyList' itself rather than an item within it) or a DictionaryPathItem.","commonSituations":"Pushing only the collection's member descriptor but not a collection-item segment before calling CollectionAdd; reusing a path built for ValueSet of the collection property and switching the action to CollectionAdd.","solutions":["Push a collection item segment so the path ends inside the collection before calling CollectionAdd.","If you intended to replace the whole collection, use MemberPathAction.ValueSet with the last segment being the member.","Use MemberPath.FromMemberPath / CreateIntermediateObjects helpers to construct the correctly shaped path."],"exampleFix":"// before\nvar path = new MemberPath();\npath.Push(type.GetProperty(\"Items\"));\npath.Apply(root, MemberPathAction.CollectionAdd, newItem);\n// after\nvar path = new MemberPath();\npath.Push(type.GetProperty(\"Items\"));\npath.PushCollectionItem(); // ensure last segment is a collection item\npath.Apply(root, MemberPathAction.CollectionAdd, newItem);","handlingStrategy":"validation","validationCode":"var last = path.Items[^1];\nif (action == MemberPathAction.CollectionAdd && last is not CollectionPathItem)\n    throw new InvalidOperationException(\"CollectionAdd requires a collection-item path segment.\");","typeGuard":"static bool CanCollectionAdd(MemberPath p) => p.Items.Count > 0 && p.Items[^1] is CollectionPathItem;","tryCatchPattern":"try { path.Apply(root, MemberPathAction.CollectionAdd, item); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Expecting last path to be a collection item\")) { /* push a collection-item segment */ }","preventionTips":["Always end CollectionAdd paths with a collection-item segment pushed via PushCollectionItem.","Centralize path construction so action/path-shape pairing is enforced in one place."],"tags":["path-shape","reflection","collections"],"backgroundTag":"invalid-argument-value","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}