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

Polymorphic Serializer Examples per Serializer #649

Open
kalosisz opened this issue Feb 8, 2022 · 3 comments
Open

Polymorphic Serializer Examples per Serializer #649

kalosisz opened this issue Feb 8, 2022 · 3 comments
Labels
enhancement New feature or request

Comments

@kalosisz
Copy link

kalosisz commented Feb 8, 2022

I was wondering if it is possible to have examples per seriliazer in a polymorphic serializer schema, either by using PolymorphicProxySerializer or django-rest-polymorphic.

I have an example defined in the @extend_schema_serializer decorator, but it is not picked up.

Thanks

@tfranzel
Copy link
Owner

tfranzel commented Feb 8, 2022

Hi @kalosisz

i don't think the issues lies directly with @extend_schema_serializer. I would guess this happens because examples are only retrieved from directly used serializers. By wrapping the actual serializers in PolymorphicProxySerializer or PolymorphicSerializer it would mask that annotation of the sub-serializers.

applying @extend_schema_serializer directly to a PolymorphicSerializer should work for the moment. That would mean some duplication unfortunately, since you have to likely list the examples more than once.
The end result would be the same no matter what because examples are on a endpoint level and not on a component level.

In theory this should be an easy, but I have to think about whether this would break the flow of behavior (additive or replacement).

@axieum
Copy link

axieum commented Mar 13, 2023

I've found that applying the following decorator to any parent PolymorphicSerializer class, allows you to lift the examples up.

S = TypeVar("S", bound=PolymorphicSerializer)

def inherit_poly_serializer_examples(klass: Type[S]) -> Type[S]:
    """Applies child serializer examples to the root `PolymorphicSerializer` class."""

    set_override(
        klass,
        "examples",
        [
            example
            for child in klass.model_serializer_mapping.values()
            if hasattr(child, "_spectacular_annotation")
            for example in child._spectacular_annotation.get("examples", [])  # type: ignore
        ],
    )
    return klass

Using it like so -

@inherit_poly_serializer_examples
class MySerializer(PolymorphicSerializer):
    model_serializer_mapping = {
        MyModel: MyModelSerializer,
    }

And then, to get this to work with https://github.com/ghazi-git/drf-standardized-errors, we need to set the examples on the view rather than the serializer (since it replaces the error serializers altogether), i.e.

@extend_schema_view(
    create=extend_schema(
        examples=[*MySerializer._spectacular_annotation["examples"]],  # type: ignore
    ),
)
class MyViewSet(ModelViewSet):

@axieum
Copy link

axieum commented Mar 13, 2023

I'd be interested to get your thoughts @tfranzel on the above, as this will tie in really nicely with the recent addition of #958 and #885. My approach above is obviously a workaround until we come up with a better way of discovering examples defined on sub-serializers.

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

No branches or pull requests

3 participants