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

SerializerMethodField - dict with known keys #184

Closed
jmaslanka opened this issue Oct 29, 2020 · 4 comments
Closed

SerializerMethodField - dict with known keys #184

jmaslanka opened this issue Oct 29, 2020 · 4 comments
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending low priority issues that would be nice to fix but have low impact

Comments

@jmaslanka
Copy link

Hey, let's assume we have this field in a serializer:

field = SerializerMethodField()

def get_field(self, obj):
    return {
        "type": "some_type",
        "data": OtherSerializer(obj.objects, many=True).data,
        ...
    }

Is there a way to represent it as:

{
    "type": "some_type",
    "data": {
        <serializer data>
    },
    ....
}

I've tried:

  • using typing.TypedDict as type hint to get_field - results in "Unknown type hint"
  • @extend_schema_field accepts only either serializer or OpenApiTypes. I don't see a way to edit additionalProperties for OpenApiTypes.OBJECT.

Seems like a common problem to me. Any ideas?

@tfranzel
Copy link
Owner

hi @jmaslanka, indeed that still incomplete. better type hint resolution is still a todo. it currently only support Optional, basic types, and OpenApiTypes.

@jmaslanka
Copy link
Author

Hey @tfranzel , thanks for the response. I ended up creating simple serializer, like:

class FieldData(serializers.Serializer):
    type = CharField()
    data = OtherSerializer(many=True)

and linked it using extend_schema_field.

@tfranzel
Copy link
Owner

@jmaslanka good stuff! luckily there multiple ways to achieve this. better type hint parsing is definitely on my list.

@tfranzel tfranzel added enhancement New feature or request low priority issues that would be nice to fix but have low impact labels Nov 1, 2020
@tfranzel
Copy link
Owner

fyi: you could have also done this:

from drf_spectacular.utils import extend_schema_field, inline_serializer

@extend_schema_field(inline_serializer(
    name='FieldData',
    fields={
        "type": CharField(),
        "data": OtherSerializer(many=True),
    }
))
def get_field(self, obj):
     pass

also we improved type hint functionality including basic TypedDict. however mixing type defintions with Serializers and Serializer fields may look ok, but are probably wrong according to mypy. so my above example is probably the "cleanest" solution. TypedDict with arbitrarily mixed basic types should work now.

@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Nov 15, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request fix confirmation pending issue has been fixed and confirmation from issue reporter is pending low priority issues that would be nice to fix but have low impact
Projects
None yet
Development

No branches or pull requests

2 participants