{"record":{"id":"ebe4bf151e804510","repo":"stride3d/stride","slug":"non-scalar-key-not-yet-supported","errorCode":null,"errorMessage":"Non-scalar key not yet supported!","messagePattern":"Non-scalar key not yet supported!","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/assets/Stride.Core.Assets/Yaml/KeyWithIdSerializer.cs","lineNumber":39,"sourceCode":"\n    /// <inheritdoc/>\n    public override object? ConvertFrom(ref ObjectContext objectContext, Scalar fromScalar)\n    {\n        var idIndex = fromScalar.Value.IndexOf('~');\n        var id = ItemId.Empty;\n        var keyString = fromScalar.Value;\n        if (idIndex >= 0)\n        {\n            var idString = fromScalar.Value[..idIndex];\n            keyString = fromScalar.Value[(idIndex + 1)..];\n            id = ItemId.Parse(idString);\n        }\n        var keyType = objectContext.Descriptor.Type.GetGenericArguments()[0];\n        var keyDescriptor = objectContext.SerializerContext.FindTypeDescriptor(keyType);\n        var keySerializer = objectContext.SerializerContext.Serializer.GetSerializer(objectContext.SerializerContext, keyDescriptor);\n        // TODO: deserialize non-scalar keys!\n        if (keySerializer is not ScalarSerializerBase scalarKeySerializer)\n            throw new InvalidOperationException(\"Non-scalar key not yet supported!\");\n\n        if (keyString.Length == 0)\n            return Activator.CreateInstance(typeof(DeletedKeyWithId<>).MakeGenericType(keyType), id);\n\n        var context = new ObjectContext(objectContext.SerializerContext, null, keyDescriptor);\n        var key = scalarKeySerializer.ConvertFrom(ref context, new Scalar(keyString));\n        var result = Activator.CreateInstance(typeof(KeyWithId<>).MakeGenericType(keyType), id, key);\n        return result;\n    }\n\n    /// <inheritdoc/>\n    public override string ConvertTo(ref ObjectContext objectContext)\n    {\n        var key = (IKeyWithId)objectContext.Instance;\n        var keyDescriptor = objectContext.SerializerContext.FindTypeDescriptor(key.KeyType);\n        var keySerializer = objectContext.SerializerContext.Serializer.GetSerializer(objectContext.SerializerContext, keyDescriptor);\n\n        // TODO: serialize non-scalar keys!","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/assets/Stride.Core.Assets/Yaml/KeyWithIdSerializer.cs#L21-L57","documentation":"KeyWithIdSerializer.ConvertFrom deserializes dictionary keys of KeyWithId<K,V> but only supports scalar (string-like) keys. When the key serializer for the dictionary's key type is not a ScalarSerializerBase, it throws InvalidOperationException('Non-scalar key not yet supported!'). It is a documented limitation (TODO in source).","triggerScenarios":"Deserializing an asset YAML containing a DictionaryWithItemIds (or KeyWithId keys) whose key type is a complex/non-scalar object (e.g. a struct or class key) rather than a scalar like string or int.","commonSituations":"Asset types using dictionaries keyed by composite objects; schema evolution where a scalar key type was changed to a complex key type; custom asset types with non-primitive dictionary keys.","solutions":["Change the dictionary key type to a scalar (string, int, Guid, PropertyKey, etc.)","Keep the complex key as a dictionary value and use a scalar identifier as the key","Implement a ScalarSerializerBase for the key type so it round-trips as a scalar string","Replace KeyWithId-keyed collections with lists or another supported collection shape"],"exampleFix":"// before\npublic DictionaryWithItemIds<MyComplexKey, int> Items { get; set; }\n// after\npublic DictionaryWithItemIds<string, int> Items { get; set; } // use MyComplexKey.Id","handlingStrategy":"validation","validationCode":"var keyDesc = context.FindTypeDescriptor(typeof(TKey));\nvar ser = context.Serializer.GetSerializer(context, keyDesc);\nif (ser is not ScalarSerializerBase) throw new InvalidOperationException(\"Dictionary key must be scalar for KeyWithId serialization\");","typeGuard":"bool IsScalarKey<TKey>(SerializerContext ctx) =>\n    ctx.Serializer.GetSerializer(ctx, ctx.FindTypeDescriptor(typeof(TKey))) is ScalarSerializerBase;","tryCatchPattern":"try { var value = serializer.ConvertFrom(ref objectContext); }\ncatch (InvalidOperationException ex) { logger.Error(ex, \"Non-scalar dictionary key in asset\"); throw; }","preventionTips":["Use scalar types (string, int, Guid) as dictionary keys in asset types","Provide a ScalarSerializerBase for custom key types","Avoid changing key types to complex objects in asset schemas"],"tags":["yaml","deserialization","dictionary","unsupported-key-type"],"backgroundTag":"unsupported-operation","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}