镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

Unable to serialize standalone polymorphic type or collection #171

Closed

Description

@isaacabraham

I'm using the Redis MsgPack wrapper, but I've tried this separately using the raw MsgPack SDK as well.

I have a simple polymorphic type hierarchy e.g.

type Animal = Cat | Dog

etc. etc., and I want to serialize a list of animals. The problem is that it appears that you need to enclose the list in a type e.g.

type Data = { Animals = ResizeArray<Animal> }

and then place an set of attributes on that property with all of the possible subclasses (or try one of the Runtime attributes).

There are several we face with this approach: -

  1. Every property on every type that has a List of Animal has to have the same attribute / attributes (or perhaps we need to create a custom List that subclasses List etc.). This means attributes all over our types etc.
  2. The bigger issue that is we need to create an enclosing type with a single property just to place the serialization polymorphism information. We should be able to store this information on the types themselves, and / or have a mechanism to do this lookup (similar to how JSON .Net works).

Have we missed something out here?

Activity

  1. yfakariya commented on Jun 26, 2016

    @yfakariya
    Member

    It is a good question. Your understanding is absolutely right.

    I did not enable polymorphism related attribute for types themselves because of following reason:

    • Simply I could not imagine the use-case not like you. In this point, your argument is reasonable.
    • Just after I released the polymorphism, CVE-2015-4852 was found. I think we should be cautious to open subtype deserialization because we can misuse it easily. Although I think explicitly attribute based approach is enough secure, but I need more discussion for it.

    Do you have any opinion for above? It is hard to implement in 0.7.0 soon, but it can be implemented in 0.8 release.

  2. added
    enhancementRequires or request to feature enhancement
    need-more-infoNeed more information (error message, stack trace, repro code etc)
    on Jun 26, 2016
  3. isaacabraham commented on Aug 30, 2016

    @isaacabraham
    Author

    Hi :-) My opinion is that we would still need this in order to use MsgPack so if this could be added it would be great.

    Attributes at the type level would be acceptable for those types in the hierarchy, but not for every property that used one of those types.

  4. yfakariya commented on Sep 5, 2016

    @yfakariya
    Member

    Thank you for your opinion. Do you say that attribute qualification is messy?

    BTW, As plus one of type level attribute qualification, I've exposed polymorphic APIs to public. You can use it to control polymorphism on the fly:

    var myPolymorphicSerializer = serializationContext.GetSerializer<IFoo>( PolymorphicSchema.ForPolymorphicObject( typeof(IFoo), new Dictionary<string, Type> { { "Red", typeof( RedFoo ) }, { "Green", typeof( GreenFoo ) } };

    It is still complex and advanced feature, but I think it can solve your problem (2).

  5. yfakariya commented on May 4, 2017

    @yfakariya
    Member

    Close because it was answered over half year ago.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementRequires or request to feature enhancementneed-more-infoNeed more information (error message, stack trace, repro code etc)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions