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

Ability to use PolymorphicProxySerializer in extend_schema_field #319

Closed
dkruk opened this issue Mar 1, 2021 · 3 comments
Closed

Ability to use PolymorphicProxySerializer in extend_schema_field #319

dkruk opened this issue Mar 1, 2021 · 3 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@dkruk
Copy link

dkruk commented Mar 1, 2021

Sorry if such a possibility already exists, I could not find it in the documentation.

I have JSONField in my Django model and DictField in a serializer for this model. JSONField can contains three different types on data with determined fieldsets only. So, I have thee different serializers for the JSONField.

Below is a schematic representation of how I wanted to document what data is expected in the data field, but it doesn't work.
Could you tell me how you can solve this issue?
Thanks.

from drf_spectacular.utils import PolymorphicProxySerializer, extend_schema_field
from rest_framework import serializers

class FirstDataSerializer(serializers.Serializer):
    ...

class SecondDataSerializer(serializers.Serializer):
    ...

class ThirdDataSerializer(serializers.Serializer):
    ...

@extend_schema_field(
    PolymorphicProxySerializer(
        component_name='DataField',
        serializers=[
            FirstDataSerializer,
            SecondDataSerializer,
            ThirdDataSerializer,
        ],
        resource_type_field_name='data_field',
    )
)
class DataField(serializers.DictField):
    ...

class SomeModeSerializer(serializers.ModelSerializer):
    name = serializers.CharField(max_length=200)
    data = serializers.ListField(child=DataField(), min_length=0, max_length=100)
@tfranzel
Copy link
Owner

tfranzel commented Mar 1, 2021

but it doesn't work.

hi! this is a bit underspecified. what is the error exactly? this looks ok to me on first glace, assuming First/Second/ThridDataSerializer have a data_field property

have you read the doc: https://drf-spectacular.readthedocs.io/en/latest/drf_spectacular.html#drf_spectacular.utils.PolymorphicProxySerializer ?

@dkruk
Copy link
Author

dkruk commented Mar 1, 2021

I see the following warnings in the terminal. On the UI side, it is shown that the field expects a list of strings.

Warning #0: unable to extract field metadata from field "<drf_spectacular.utils.PolymorphicProxySerializer object at 0x7f642f3e1e20>" because it appears to be neither a Field nor a Serializer. Proper handling may require an Extension or @extend_serializer_field. Skipping metadata extraction. 
Warning #1: could not resolve serializer field "<drf_spectacular.utils.PolymorphicProxySerializer object at 0x7f642f3e1e20>". defaulting to "string"

have you read the doc: https://drf-spectacular.readthedocs.io/en/latest/drf_spectacular.html#drf_spectacular.utils.PolymorphicProxySerializer ?

yes, I read the docs.

@tfranzel tfranzel added the bug Something isn't working label Mar 1, 2021
@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Mar 2, 2021
@dkruk
Copy link
Author

dkruk commented Mar 4, 2021

@tfranzel I tried the fix on my project - it works like a charm! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending
Projects
None yet
Development

No branches or pull requests

2 participants