Non-default constructor is used to deserialize without a `[SerializationConstructor]` attribute
Author: charles-henryCreated Jun 15, 2022Updated Jul 31, 2026
Labelsawaiting-approval
Bug description
A deserialized object has null fields that shouldn't be null. This is related to the kinds of constructors present on the class. If there is no constructor defined, or only a parameterless consructor, it works as expected. This issue was not present until version 2.3.85.
Repro steps
Class to be serialized/deserialized:
using MessagePack;
namespace MessagePackTest
{
[MessagePackObject]
public class Class1
{
[Key(0)]
public string code { get; set; }
[Key(1)]
public string name { get; set; }
public Class1()
{ }
public Class1(string nm)
{
name = nm;
}
}
}
Test Class:
using MessagePack;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace MessagePackTest
{
[TestClass]
public class UnitTest1
{
[TestMethod]
public void TestMethod2()
{
Class1 obj = new Class1 { code = "A", name = "AAAA" };
byte[] original = MessagePackSerializer.Serialize(obj);
Class1 newObj = MessagePackSerializer.Deserialize<Class1>(original);
Assert.AreEqual(obj.code, newObj.code);
}
}
}
Expected behavior
All fields should be serialized and deserialized with the proper values.
Actual behavior
Some fields in the deserialized object are null.
- Version used: 2.3.85, 2.3.112
- Runtime: .Net Core 3.1
Additional context
Source: MessagePack-CSharp/MessagePack-CSharp