-
Notifications
You must be signed in to change notification settings - Fork 270
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
Schema generation fails with AttributeError: 'NoneType' object has no attribute 'copy'
#1342
Comments
This is how far I got trying to reproduce the problem by adding a test case to @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'] |
Hi
so according to the trace, the schema ends up as schema = self._map_serializer_field(force_instance(override), direction) # line 645 while processing the override. The decoration itself is fine and should work. I suspect there is a problem with the I can't really do much here without a reproduction. This code ran millions of times without issue, so you must have hit some rare edge-case. Please try to create a more accurate version of the offending serializer. I would even so far and say serializer would throw even used regularly in a view (without the decoration), as it hits the code in pretty much exactly the same way. |
@tfranzel Thanks for the insights, I am also suspecting an edge case, will try harder to reproduce/debug. For now my ugly workaround is to change the failing annotation to: @extend_schema_field(
{"allOf": {"$ref": "#/components/schemas/Parent"}, "readOnly": True}
) |
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.
The text was updated successfully, but these errors were encountered: