Repository navigation
DRD-spectacular integration with depth parameter #9897
|
Encountered this warning when accessing swagger in DRF with drf-spectacular when using depth=1 in a code example bellow. Warning [CoreSystemReleaseViewSet > CoreSystemReleaseSerializer > NestedSerializer]: Encountered 2 components with identical names "Nested" and different identities <class 'rest_framework.serializers.ModelSerializer.build_nested_field..NestedSerializer'> and <class 'rest_framework.serializers.ModelSerializer.build_nested_field..NestedSerializer'>. This will very likely result in an incorrect schema. Try renaming one. Here is the code. class MasterdataSerializerBase(serializers.ModelSerializer[ModelT]):
class Meta:
abstract = True
depth = 1
class CoreSystemReleaseSerializer(
MasterdataSerializerBase[models.CoreSystemRelease]
):
class Meta(MasterdataSerializerBase.Meta):
model = models.CoreSystemRelease
fields: t.ClassVar[list[str]] = [
"id",
"name",
"status",
"technical_configuration",
]
class CoreSystemReleaseViewSet(
OptimizedQuerySetMixin,
HistoryRevertMixin,
MasterdataViewSet,
):
queryset = models.CoreSystemRelease.objects.all()
serializer_class = serializers.CoreSystemReleaseSerializer
prefetch_related_fields = ("technical_configuration",)
router.register(
"core-system-releases",
views.CoreSystemReleaseViewSet,
basename="coresystemrelease",
) |
Replies: 1 comment
|
You haven't done anything wrong. DRF won't rename these by default, but you can give them unique names in your base serializer. Cache one class per related model so drf-spectacular sees the same identity every time: _nested_serializers = {}
class MasterdataSerializerBase(serializers.ModelSerializer[ModelT]):
class Meta:
abstract = True
depth = 1
def build_nested_field(self, field_name, relation_info, nested_depth):
field_class, field_kwargs = super().build_nested_field(field_name, relation_info, nested_depth)
model = relation_info.related_model
key = (model, nested_depth)
if key not in _nested_serializers:
_nested_serializers[key] = type(f"{model.__name__}NestedSerializer", (field_class,), {})
return _nested_serializers[key], field_kwargsWith that (Django 6.1, DRF 3.18.3, drf-spectacular 0.30.0) the warning goes away, and the schema has separate The cache matters. If you create a new class on every call, the same model nested from two serializers gives two classes with the same name, and you get the same warning again. If you'd rather avoid |
You haven't done anything wrong.
depthand drf-spectacular just don't work well together out of the box. For every relation, DRF builds the nested serializer on the fly as a local class that is always namedNestedSerializer(build_nested_field). drf-spectacular derives the component name from the class name, so every relation becomes a component calledNested. The warning is real: I reproduced it with two FKs anddepth = 1, and both fields ended up pointing at the same#/components/schemas/Nested, so one of them is documented with the wrong model.DRF won't rename these by default, but you can give them unique names in your base serializer. Cache one class per related model so drf-spectac…