Open salomvary opened 8 hours ago
This is how far I got trying to reproduce the problem by adding a test case to test_regressions.py
, but this test passes:
@mock.patch('drf_spectacular.settings.spectacular_settings.OAS_VERSION', '3.1.0')
def test_extend_schema_field_on_serializer(no_warnings):
class X(models.Model):
position = models.IntegerField()
class ParentSerializer(serializers.Serializer):
field = serializers.SerializerMethodField()
@extend_schema_field(int)
def get_field(self, instance):
return None
@extend_schema_field(ParentSerializer)
class XChildSerializer(ParentSerializer):
pass
class XSerializer(serializers.ModelSerializer):
field = XChildSerializer(read_only=True, source="*")
class Meta:
model = X
fields = ("id", "position", "field")
class XView(views.APIView):
serializer_class = XSerializer
def get(self, request):
pass # pragma: no cover
def post(self, request):
pass # pragma: no cover
@extend_schema_field(ParentSerializer)
class YChildSerializer(ParentSerializer):
pass
class YSerializer(serializers.ModelSerializer):
field = YChildSerializer()
class Meta:
model = X
fields = ("id", "position", "field")
class YView(views.APIView):
serializer_class = YSerializer
def get(self, request):
pass # pragma: no cover
def post(self, request):
pass # pragma: no cover
schema = generate_schema(None, patterns=[path('x', XView.as_view()), path('y', YView.as_view())])
assert 'X' in schema['components']['schemas']
assert 'Y' in schema['components']['schemas']
assert 'Parent' in schema['components']['schemas']
assert 'XChild' not in schema['components']['schemas']
Using Version: 0.28.0.
Describe the bug
As in title. Both when using the management command to generate the schema or the schema view.
The stack trace looks like this:
To Reproduce I've tried but failed to create a snippet, the problem only occurs a specific, rather complex project.
The change that triggered the issue looks something like this:
The interesting part is annotating
XChildSerializer
with@extend_schema_field(ParentSerializer)
. If I remove the annotation, the problem goes away. In my real project I also have anotherYChildSerializer
with the same annotation, and that does not have this problem.Expected behavior
Schema should be generated without exception.
I've been trying to figure out what might be wrong using the debugger, but I don't understand the internals of drf-spectacular enough to have any clue. Any pointers would be appreciated.