Skip to content

Unexpected result Deserializing by constructor #233

@chrischandler25

Description

@chrischandler25

I have been running tests with MessagePack CLI, and have experienced unexpected results when deserializing immutable classes via a single constructor.

It works as expected with writable properties, but via the constructor, two of the parameters are left null, and rather than setting the dictionary back to the expected property, it is assigned to a string property. This can be viewed while debugging, but crashes as soon as you try to programmatically access the property (AccessVioloationException).

Please see the code below for a better explanation. It looked from a few earlier issues on here that constructor deserialization is possible. Am I correct in this assumption?

Thanks
Chris

using System.Collections.Generic;
using MsgPack.Serialization;

namespace MessagePackCSharp
{
class Program
{
static void Main(string[] args)
{
var endpoints = new EndpointList(
"Test String One",
new Dictionary<string, string[]>
{
{ "ConfigService", new [] { "ur1", "ur2" } },
{ "TestService", new [] { "ur1", "ur2" } }
},
"Test String Two");

        var ser = SerializationContext.Default.GetSerializer(typeof(EndpointList));
        var bytes = ser.PackSingleObject(endpoints);
        var endpointsDeser = (EndpointList) ser.UnpackSingleObject(bytes);

         // endpointsDeser has very strange values, with one of the string properties being assigned 
         // the dictionary
    }
}


public class EndpointList
{
    public EndpointList(string stringOne, Dictionary<string, string[]> endpoints, string stringTwo)
    {
        StringOne = stringOne;
        Endpoints = endpoints;
        StringTwo = stringTwo;
    }

    public string StringOne { get; }

    public Dictionary<string, string[]> Endpoints { get; }

    public string StringTwo { get; }
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugDetected as bug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions