-
Notifications
You must be signed in to change notification settings - Fork 260
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
Comments
Hi @kalosisz i don't think the issues lies directly with applying In theory this should be an easy, but I have to think about whether this would break the flow of behavior (additive or replacement). |
I've found that applying the following decorator to any parent 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): |
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
The text was updated successfully, but these errors were encountered: