Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deserialization fails/misses readonly collection properties #281

Open
perpetualKid opened this issue Apr 19, 2024 · 0 comments
Open

Deserialization fails/misses readonly collection properties #281

perpetualKid opened this issue Apr 19, 2024 · 0 comments

Comments

@perpetualKid
Copy link

below sample code fails since deserializer does not restore the collection items, even if collections are initialized.
Making the "Collection" property writable { get; set; } would solve that, but raises CA2227 warnings.

When restoring/deserializing collection properties, code could check if collections are alreay initialized, and add the restore items to the existing collection, instead of trying to set the collection property itself value.Collection = __Collection;

If it's not intended to handle readonly collection properties, code analysis warning or runtime error should be raised, and also the properties should be exempted from serizalization.

    [MemoryPackable]
    public partial class CollectionTest
    {
        public Collection<string> Collection { get; } = new Collection<string>();
    }

    internal class Program
    {
        static async Task Main(string[] args)
        {
            CollectionTest sourceCollection = new CollectionTest();
            sourceCollection.Collection.Add("1234");
            sourceCollection.Collection.Add("5678");

            Pipe bufferPipe = new Pipe();
            MemoryPackSerializer.Serialize(bufferPipe.Writer, sourceCollection);
            _ = await bufferPipe.Writer.FlushAsync().ConfigureAwait(false);
            ReadResult resultBuffer = await bufferPipe.Reader.ReadAsync().ConfigureAwait(false);

            CollectionTest resultCollection = MemoryPackSerializer.Deserialize<CollectionTest>(resultBuffer.Buffer);
            Debug.Assert(resultCollection.Collection.Count == 2);
        }
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant