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

Typeless serializer fails to serialize simple type, unless fields are prefixed with "_" #1776

Open
epitka opened this issue Feb 21, 2024 · 2 comments

Comments

@epitka
Copy link

epitka commented Feb 21, 2024

Bug description

Typeless serializer fails to serialize simple type, unless fields are prefixed with "_"

Repro steps

public class TempTests
{
    private static Fixture Fixture = new Fixture();

    [Fact]
    public void Should_Serialize()
    {
        var expected = Fixture.Create<TestInfo>();

        var binary = MessagePackSerializer.Typeless.Serialize(expected);

        // var current = MessagePackSerializer.Typeless.Deserialize(binary);

        // current.ShouldDeepEqual(expected);
    }
    public struct TestInfo
    {
        // if fields are prefixed with "_" then it works fine
        private Guid typeId;
        private Guid levelId;

        public TestInfo(
            Guid typeId,
            Guid levelId)
        {
            this.typeId = typeId;
            this.levelId = levelId;
        }

        public Guid TypeId
        {
            get => this.typeId;
            set
            {
                this.typeId = value;
            }
        }

        public Guid LevelId
        {
            get => this.levelId;
            set
            {
                this.levelId = value;
            }
        }
    }

Expected behavior

Should be able to serialize simple types without requiring that fields are prefixed with "_"

Actual behavior

Serialization fails with message

MessagePack.MessagePackSerializationException
Failed to serialize System.Object value.
   at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
   at MessagePack.MessagePackSerializer.Serialize[T](T value, MessagePackSerializerOptions options, CancellationToken cancellationToken)
   at MessagePack.MessagePackSerializer.Typeless.Serialize(Object obj, MessagePackSerializerOptions options, CancellationToken cancellationToken)
   at SeatGeek.Open.SmartCaching.Tests.Serializers.MsgPackSerializerTests.TempTests.Should_Serialize() in /Users/edvardpitka/code/src/gitlab.service.seatgeek.mgmt/consumer/peakpass/SeatGeek.Open.SmartCaching.Tests/Serializers/MsgPackSerializerTests/TempTests.cs:line 21

System.TypeInitializationException
The type initializer for 'FormatterCache`1' threw an exception.
   at MessagePack.Resolvers.DynamicContractlessObjectResolverAllowPrivate.GetFormatter[T]()
   at MessagePack.Resolvers.TypelessContractlessStandardResolver.ResolverCache.GetFormatterCore[T]()
   at MessagePack.Resolvers.CachingFormatterResolver.GetFormatter[T]()
   at MessagePack.Resolvers.TypelessContractlessStandardResolver.GetFormatter[T]()
   at MessagePack.FormatterResolverExtensions.GetFormatterDynamic(IFormatterResolver resolver, Type type)
   at MessagePack.FormatterResolverExtensions.GetFormatterDynamicWithVerify(IFormatterResolver resolver, Type type)
   at MessagePack.Formatters.TypelessFormatter.Serialize(MessagePackWriter& writer, Object value, MessagePackSerializerOptions options)
   at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
  • Version used: MessagePack 2.5.108
  • Runtime: .NET Core 6
@epitka-sg
Copy link
Contributor

How do I push branch with a fix for review?

@AArnott
Copy link
Collaborator

AArnott commented Mar 4, 2024

Thanks for offering. Very strange bug.

Standard github flow: fork the repo, push to your fork, then create a pull request to this one.

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

3 participants