TypeError with partial=True
See original GitHub issueMR #18 created a new issue.
Consider this simple serializer:
class MyPolymorphicSerializer(PolymorphicSerializer):
pass # Mostly irrelevant
class MySerializer(Serializer):
foo = CharField()
bar = MyPolymorphicSerializer(required=false)
I use standard DRF viewsets to do a PATCH request (i.e. serializer has partial=True
) to an endpoint using MySerializer
, with JSON content: {"foo": "my_foo"}
. We exclude bar
, which for PATCH requests should mean that this field is virtually ignored and kept as it is before the request. This worked as expected before this MR.
Now however, it will raise this error:
File "/usr/local/lib/python3.8/site-packages/rest_framework/mixins.py", line 67, in update
serializer.is_valid(raise_exception=True)
File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 234, in is_valid
self._validated_data = self.run_validation(self.initial_data)
File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 433, in run_validation
value = self.to_internal_value(data)
File "/usr/local/lib/python3.8/site-packages/rest_framework/serializers.py", line 490, in to_internal_value
validated_value = field.run_validation(primitive_value)
File "/usr/local/lib/python3.8/site-packages/rest_polymorphic/serializers.py", line 95, in run_validation
resource_type = self._get_resource_type_from_mapping(data)
File "/usr/local/lib/python3.8/site-packages/rest_polymorphic/serializers.py", line 112, in _get_resource_type_from_mapping
return mapping[self.resource_type_field_name]
TypeError: 'type' object is not subscriptable
This happens because normally, Serializer.run_validation
does checks (validate_empty_values
) to ensure there is some data to begin with. Now however, it tries to determine the resource_type
before running the base implementation. When determining the resource_type
, it tries to do empty['somekey']
, which of course results in a TypeError
.
Note that there might be other similar edge-cases I didn’t cover here.
IMO this issue is more critical than the one solved by #18 in the first place.
Issue Analytics
- State:
- Created 3 years ago
- Comments:7 (4 by maintainers)
Hi. Thanks. I haven’t used this library for a long time and I’m not interested in doing any fixes right now. If you have some free time, you can send a PR if you want. I will take a look. Thanks
I’m not working w/DRF nor django-polymorphic rigth now. Is there any reason why adding a check for empty data on my previous PR wouldn’t fix your problem?
I’ll try to come up with a fix later 😃