Skip to content

Unable to serialize interface types using contractless or typeless #970

@cocowalla

Description

@cocowalla

Bug description

I'm unable to serialize undecorated classes containing interface types using contractless or typeless serialization, getting:

MessagePack.MessagePackSerializationException: Failed to serialize Acme.TestClass value. ---> MessagePack.FormatterNotRegisteredException: Acme.IScheduler is not registered in resolver: MessagePack.Resolvers.CompositeResolver+CachingResolver
  at at MessagePack.FormatterResolverExtensions.Throw(Type t, IFormatterResolver resolver)
  at at Serialize(Byte[][] , Object[] , MessagePackWriter& , TestClass , MessagePackSerializerOptions )
  at at MessagePack.Internal.AnonymousSerializableFormatter`1.Serialize(MessagePackWriter& writer, T value, MessagePackSerializerOptions options)
  at at MessagePack.MessagePackSerializer.Serialize[T](MessagePackWriter& writer, T value, MessagePackSerializerOptions options)

Repro steps

Consider this class:

    public class TestClass
    {
        public IScheduler Scheduler { get; set; }
    }

    public interface IScheduler
    {
        Guid Id { get; set; }
    }
    
    public class SchedulerA : IScheduler
    {
        public Guid Id { get; set; } = Guid.NewGuid();
    }

And then attempting to serialize:

var test = new TestClass();
test.Scheduler = new SchedulerA();

// All these throw
var result1 = MessagePackSerializer.Typeless.Serialize(test);
var result2 = MessagePackSerializer.Serialize(test, MessagePack.Resolvers.ContractlessStandardResolver.Options);
var result3 = MessagePackSerializer.Serialize(test, MessagePack.Resolvers.TypelessContractlessStandardResolver.Options);

// Also throws
var resolver = CompositeResolver.Create(ContractlessStandardResolver.Instance, StandardResolver.Instance);
var options = MessagePackSerializerOptions.Standard.WithResolver(resolver);
var result4 = MessagePackSerializer.Serialize(test, options);

Expected behavior

I'd expect this to fail with the standard resolver since the types are not decorated with MessagePack attributes), but to suceed with contractless and/or typeless.

Actual behavior

MessagePackSerializationException when calling Serialize

  • Version used: tried both 2.1.115 and 2.1.152
  • Runtime: .NET Core 3.1

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions