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

Unexpected result Deserializing by constructor #233

Closed
chrischandler25 opened this issue Apr 24, 2017 · 1 comment
Closed

Unexpected result Deserializing by constructor #233

chrischandler25 opened this issue Apr 24, 2017 · 1 comment
Labels
bug Detected as bug
Milestone

Comments

@chrischandler25
Copy link

chrischandler25 commented Apr 24, 2017

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; }
}

}

@yfakariya yfakariya added the bug Detected as bug label Apr 24, 2017
@yfakariya
Copy link
Member

Sorry, it is bug of constructor deserialization.
You can avoid this to align orders of constructor parameters and properties as one of following.

  • Mark your properties with [MessagePackMember] with constructor parameter order.
  • Or, change constructor parameters order alphabetically.

@yfakariya yfakariya added this to the 0.9 milestone Apr 24, 2017
yfakariya added a commit that referenced this issue May 2, 2017
This commit fixes wrong code generation due to strange name calculation bug.
yfakariya added a commit that referenced this issue May 3, 2017
This commit fixes wrong code generation due to strange name calculation bug.
yfakariya added a commit that referenced this issue May 3, 2017
This commit fixes wrong code generation due to strange name calculation bug.
yfakariya added a commit that referenced this issue May 4, 2017
Fix constructor based deserialization code gen #233
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Detected as bug
Projects
None yet
Development

No branches or pull requests

2 participants