Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Polymorphic serializer subclassing issues #119

@Floris272

Description

@Floris272

If a PolymorphicSerializer is subclassed

PolymorphicSerializerMetaclass.__new__ raises an error because discriminator does not exist in attrs

    def __new__(cls, name, bases, attrs):
        attrs["discriminator"] = cls._sanitize_discriminator(name, attrs)
        return super().__new__(cls, name, bases, attrs)

currently fixed this by copying the discriminator:

class RolSubSerializer(SubSerializerMixin, RolSerializer):
    discriminator = RolSerializer.discriminator

Another issue is when the discriminatior has set the group_field, this field is added to all fields in the mapping in PolymorphicSerializerMetaclass.santize_discriminator, when the subclass (that copied the discriminator) is loaded this function is called again which results in <group_field><group_field>, this can currently be fixed by copying the mapping to the original serializer and the subserializer or by not setting group_field on the sub_serializer.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions