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

Cannot use @extend_schema_serializer(component_name="...") with djangorestframework-dataclasses #1004

Closed
johnthagen opened this issue Jun 12, 2023 · 7 comments
Labels
bug Something isn't working fix confirmation pending issue has been fixed and confirmation from issue reporter is pending

Comments

@johnthagen
Copy link
Contributor

johnthagen commented Jun 12, 2023

Describe the bug

The component_name argument to @extend_schema_serializer does not work on DatclassSerializers from djangorestframework-dataclasses.

To Reproduce

from drf_spectacular.utils import extend_schema_serializer
from rest_framework_dataclasses.serializers import DataclassSerializer

@dataclass
class Car:
    name: str


@extend_schema_serializer(component_name="Truck")
class CarSerializer(DataclassSerializer[Car]):
    class Meta:
        dataclass = Car

Expected behavior

I would expect the generated component name to be Truck but it is still Car.

Environment

djangorestframework             3.14.0
drf-spectacular                 0.26.2
djangorestframework-dataclasses 1.2.0
  • Python 3.10.11
@tfranzel tfranzel added the bug Something isn't working label Jun 12, 2023
@tfranzel
Copy link
Owner

not quite sure what happened here, since this was supposed to be fixed by #839 I believe

@johnthagen
Copy link
Contributor Author

@tfranzel Thanks for confirming that I was at least using this as intended (I wasn't 100% sure).

@tfranzel
Copy link
Owner

the tests cover 2 approaches. one with ref_name and one with decoration directly on the dataclass.

https://github.com/tfranzel/drf-spectacular/blob/master/tests/contrib/test_rest_framework_dataclasses.py#L43

decorating on the actual serializer was somehow not covered in the tests. not sure if we had a reason for that. I have to contemplate this, since it was a while ago.

@johnthagen
Copy link
Contributor Author

johnthagen commented Jun 14, 2023

@tfranzel Thanks for sharing that @extend_schema_serializer could go directly on a dataclass. Since the name as _serialier in the name, I didn't even think to try it on a dataclass. That also worked for my use case.

Maybe there would be some way in the docs to also mention that this decorator works on dataclasses used with DataclassSerializer?

@tfranzel tfranzel added the fix confirmation pending issue has been fixed and confirmation from issue reporter is pending label Jun 16, 2023
@tfranzel
Copy link
Owner

Closed the gap. I think this just simply got overlooked due to the mainly inline usage of DataclassSerializer. However, one would expect this to work, while the dataclass decoration is a nifty but unexpected functionality.

@johnthagen
Copy link
Contributor Author

@tfranzel I tested the latest master branch and it works as expected!

Thanks!

This issue can be closed.

@tfranzel
Copy link
Owner

perfect. will probably release in the upcoming days after I looked into another issue.

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