{"record":{"id":"08c2166618e3351b","repo":"stride3d/stride","slug":"unable-to-deserialize-array-current-number-of-elements-index","errorCode":null,"errorMessage":"Unable to deserialize array. Current number of elements [{index}] exceeding array size [{arrayList.Count}]","messagePattern":"Unable to deserialize array\\. Current number of elements \\[(.+?)\\] exceeding array size \\[(.+?)\\]","errorType":"exception","errorClass":"YamlException","httpStatus":null,"severity":"error","filePath":"sources/core/Stride.Core.Yaml/Serialization/Serializers/ArraySerializer.cs","lineNumber":79,"sourceCode":"\n        public virtual object ReadYaml(ref ObjectContext objectContext)\n        {\n            var reader = objectContext.Reader;\n            var arrayDescriptor = (ArrayDescriptor) objectContext.Descriptor;\n\n            bool isArray = objectContext.Instance != null && objectContext.Instance.GetType().IsArray;\n            var arrayList = (IList) objectContext.Instance;\n\n            reader.Expect<SequenceStart>();\n            int index = 0;\n            if (isArray)\n            {\n                while (!reader.Accept<SequenceEnd>())\n                {\n                    var node = reader.Peek<ParsingEvent>();\n                    if (index >= arrayList.Count)\n                    {\n                        throw new YamlException(node.Start, node.End, $\"Unable to deserialize array. Current number of elements [{index}] exceeding array size [{arrayList.Count}]\");\n                    }\n\n                    // Handle aliasing\n                    arrayList[index++] = ReadYaml(objectContext.SerializerContext, arrayDescriptor.ElementType);\n                }\n            }\n            else\n            {\n                var results = new List<object>();\n                while (!reader.Accept<SequenceEnd>())\n                {\n                    results.Add(ReadYaml(objectContext.SerializerContext, arrayDescriptor.ElementType));\n                }\n\n                // Handle aliasing\n                arrayList = arrayDescriptor.CreateArray(results.Count);\n                foreach (var arrayItem in results)\n                {","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/core/Stride.Core.Yaml/Serialization/Serializers/ArraySerializer.cs#L61-L97","documentation":"When deserializing into a fixed-size .NET array, ArraySerializer reads elements until SequenceEnd. If the YAML sequence contains more entries than the target array's length, it cannot grow the array, so it throws a YamlException reporting the element index and the array size.","triggerScenarios":"Deserializing a YAML sequence with N elements into a Type that is an array of length M < N (arrays have fixed length once constructed).","commonSituations":"Schema drift: the array size is determined by a fixed attribute or an earlier field, but the data file was extended; hand-edited YAML adding entries to a fixed-size array like a matrix or vector.","solutions":["Change the target type to List<T> or another growable collection if the data is variable-length","Make the YAML sequence match the fixed array size","Catch YamlException during deserialization to surface which index/size mismatched"],"exampleFix":"// before\npublic float[] Coefficients = new float[3]; // yaml has 5 values\n// after\npublic List<float> Coefficients; // accepts any number of elements","handlingStrategy":"validation","validationCode":"var expected = targetArray.Length;\nvar actual = ((YamlSequenceNode)node).Children.Count;\nif (actual > expected) throw new InvalidDataException($\"YAML sequence has {actual} items but array holds {expected}\");","typeGuard":null,"tryCatchPattern":"try { return serializer.Deserialize(reader, arrayType); } catch (YamlException ex) { throw new InvalidDataException($\"Array overflow: {ex.Message} at {ex.Start}\", ex); }","preventionTips":["Use List<T> instead of fixed arrays for variable-length YAML sequences","Keep fixed-size array declarations in sync with the data schema","Validate sequence length before deserializing"],"tags":["yaml","deserialization","array","sequence-length"],"backgroundTag":"index-out-of-range","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}