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

Private fields on base class do not get serialized #1756

Open
QsROg8320 opened this issue Jan 31, 2024 · 4 comments
Open

Private fields on base class do not get serialized #1756

QsROg8320 opened this issue Jan 31, 2024 · 4 comments
Labels

Comments

@QsROg8320
Copy link

QsROg8320 commented Jan 31, 2024

Bug description

Private fields on base class do not get serialized

Repro steps

DerivedFromBase message = new DerivedFromBase();
message.Add(0);

var bytes = MessagePackSerializer.Serialize(message, TypelessContractlessStandardResolver.Options);
var deserialized = MessagePackSerializer.Deserialize<DerivedFromBase>(bytes, TypelessContractlessStandardResolver.Options);



class DerivedFromBase : Base { }
class Base
{
    private readonly List<int> _itmes;
    public Base()
    {
        _itmes = new();
    }
    public void Add(int value)
    {
        _itmes.Add(value);
    }
}

Expected behavior

field _items in deserialized has 1 item.

Actual behavior

field _items in deserialized has 0 items.

  • Version used: 2.5.140
  • Runtime: net6.0
@AArnott
Copy link
Collaborator

AArnott commented Jan 31, 2024

If you want private fields to be serialized, you need to use the AllowPrivate variant of the resolver.

@QsROg8320
Copy link
Author

If you want private fields to be serialized, you need to use the AllowPrivate variant of the resolver.

it doesn't work too.

DerivedFromBase message = new DerivedFromBase();
message.Add(0);

var bytes = MessagePackSerializer.Serialize(message, ContractlessStandardResolverAllowPrivate.Options);
var deserialized = MessagePackSerializer.Deserialize<DerivedFromBase>(bytes, ContractlessStandardResolverAllowPrivate.Options);

class DerivedFromBase : Base { }
class Base
{
    private readonly List<int> _itmes;
    public Base()
    {
        _itmes = new();
    }
    public void Add(int value)
    {
        _itmes.Add(value);
    }
}

But if we serialize the base class, everything works fine.

Base message = new Base();
message.Add(0);

var bytes = MessagePackSerializer.Serialize(message, TypelessContractlessStandardResolver.Options);
var deserialized = MessagePackSerializer.Deserialize<Base>(bytes, TypelessContractlessStandardResolver.Options);

In this case, _items field is not empty.

@AArnott AArnott added the bug label Jan 31, 2024
@AArnott
Copy link
Collaborator

AArnott commented Jan 31, 2024

Thank you for trying that out. Sounds like a bug. Albeit, IMO the contractless resolver does a poor job in general. It can't/shouldn't serialize all fields and all properties, because that would involve a lot of redundancy since properties are typically backed by fields. So I recommend (with or without this bug) that you apply the attributes if you own the type, or write a custom formatter if you don't.

@topsterde
Copy link

I also ran into the same Bug...

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants