-
Notifications
You must be signed in to change notification settings - Fork 302
Description
Describe the bug
For code reuse, it's often useful to expose endpoints for the same data at different layers within the overall tree of data. If a DataclassSerializer
is used for both a parent @dataclass
and a child @dataclass
a warning is printed.
To Reproduce
from dataclasses import dataclass
from drf_spectacular.utils import extend_schema
from rest_framework.decorators import api_view
from rest_framework.request import Request
from rest_framework.response import Response
from rest_framework_dataclasses.serializers import DataclassSerializer
@dataclass
class Person:
name: str
age: int
@dataclass
class Party:
person: Person
num_persons: int
class PartySerializer(DataclassSerializer[Party]):
class Meta:
dataclass = Party
class PersonSerializer(DataclassSerializer[Person]):
class Meta:
dataclass = Person
@extend_schema(responses=PartySerializer)
@api_view()
def party(request: Request) -> Response:
pass
@extend_schema(responses=PersonSerializer)
@api_view()
def person(request: Request) -> Response:
pass
Results in a warning when the OpenAPI spec is queried:
views/api/test.py: Warning [person > PersonSerializer]:
Encountered 2 components with identical names "Person" and different classes <class 'views.api.test.PersonSerializer'> and <class 'rest_framework_dataclasses.serializers.DataclassSerializer'>.
This will very likely result in an incorrect schema. Try renaming one.
But, in this case, we do want to represent the same original @dataclass
in both endpoints and have them generate the same Person
object in the OpenAPI spec (i.e. we wouldn't want to generate two types in the API when really we mean a single type that we want clients to use interchangeably).
Also, as far as I can tell, the actual OpenAPI spec is generated correctly, the warning is the only issue. But of course if this is not documented/tested behavior of drf-spectacular
that would give me pause, thus why I wanted to report the issue.
The code in question is located at
drf-spectacular/drf_spectacular/plumbing.py
Lines 756 to 761 in 2b4d5ab
if query_class != registry_class and not suppress_collision_warning: | |
warn( | |
f'Encountered 2 components with identical names "{component.name}" and ' | |
f'different classes {query_class} and {registry_class}. This will very ' | |
f'likely result in an incorrect schema. Try renaming one.' | |
) |
Expected behavior
No warning printed.
Environment
- Python 3.11.8
drf-spectacular 0.27.2
djangorestframework-dataclasses 1.3.1
djangorestframework 3.15.2