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

swagger_serializer_method does not work with partialmethod #870

Open
sevdog opened this issue Oct 17, 2023 · 0 comments
Open

swagger_serializer_method does not work with partialmethod #870

sevdog opened this issue Oct 17, 2023 · 0 comments

Comments

@sevdog
Copy link

sevdog commented Oct 17, 2023

Bug Report

When attempting to use the swagger_serializer_method on a partial object generated with functool.partialmethod the _swagger_serializer is not kept in the final class method.

This prevents from generating a correct swagger file since this approach is used to generate more than 1 fields and thus it is not possible to set the decorator on the original method.

Description

A clear and concise description of the problem...

Minimal Reproduction

from functools import partialmethod
from drf_yasg.utils import swagger_serializer_method
from rest_framework import serializers


class S1(serializers.Serializer):
    f1 = serializers.CharField()


class S2(serializers.Serializer):
    f2 = serializers.SerializeMethodField()

    def _m(self, value, otherser):
        return otherser(value).data

    get_f2 = swagger_serializer_method(S1)(partialmethod(_m, otherser=S1))


class S3(serializers.Serializer):
    f3 = serializers.SerializerMethodField()

    @swagger_serializer_method(S1)
    def get_f3(self, value):
        return S1(value).data

print(S3.get_f3._swagger_serializer)
print(S2.get_f3._swagger_serializer) # AttributeError: 'functools.partial' object has no attribute '_swagger_serializer'

Stack trace / Error message

AttributeError: 'functools.partial' object has no attribute 

Your Environment

Python 3.10.12
djangorestframework==3.14.0
drf-yasg==1.21.7
Django==3.2.22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant