question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`is_patched_serializer` fails with `DictField`

See original GitHub issue

Describe the bug My team has a list endpoint that returns a dictionary values that are generated by methods that aren’t serializers. We’d like to document at least that this endpoint returns a list of objects. I tried adding the following extend_schema:

    @extend_schema(
        responses=serializers.ListSerializer(child=serializers.DictField()),
    )
    def get(self, request: Request, *args: Any, **kwargs: Any) -> Response:
        data = # complex code to produce list of dicts here.
        return Response(data, status=200)

drf-spectacular raised the following exception:

Traceback (most recent call last):
  File "manage.py", line 11, in <module>
    execute_from_command_line(sys.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 381, in execute_from_command_line
    utility.execute()
  File "/usr/local/lib/python3.7/site-packages/django/core/management/__init__.py", line 375, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 323, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/usr/local/lib/python3.7/site-packages/django/core/management/base.py", line 364, in execute
    output = self.handle(*args, **options)
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/management/commands/spectacular.py", line 50, in handle
    schema = generator.get_schema(request=None, public=True)
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/generators.py", line 230, in get_schema
    paths=self.parse(request, public),
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/generators.py", line 207, in parse
    operation = view.schema.get_operation(path, path_regex, method, self.registry)
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/utils.py", line 222, in get_operation
    return super().get_operation(path, path_regex, method, registry)
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/openapi.py", line 83, in get_operation
    operation['responses'] = self._get_response_bodies()
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/openapi.py", line 975, in _get_response_bodies
    return {'200': self._get_response_for_code(response_serializers, '200')}
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/openapi.py", line 1036, in _get_response_for_code
    paginated_name = f'Paginated{self._get_serializer_name(serializer, "response")}List'
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/openapi.py", line 1119, in _get_serializer_name
    return self._get_serializer_name(serializer.child, direction)
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/openapi.py", line 1126, in _get_serializer_name
    if is_patched_serializer(serializer, direction):
  File "/usr/local/lib/python3.7/site-packages/drf_spectacular/plumbing.py", line 101, in is_patched_serializer
    and serializer.partial
AttributeError: 'DictField' object has no attribute 'partial'

Expected behavior

Since this should be a valid serializer (test proving that is below), I’d expect the is_patched_serializer to handle a DictField.

    def test_list_serializer(self):
        from rest_framework.serializers import DictField, ListSerializer

        class MyListSerializer(ListSerializer):
            child = DictField()

        my_list = [{}, {'a': 'b'}]

        serializer = MyListSerializer(my_list)
        assert serializer.data == [{}, {'a': 'b'}]

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:12 (7 by maintainers)

github_iconTop GitHub Comments

1reaction
tfranzelcommented, Jun 9, 2021

that was a small but important detail

0reactions
tfranzelcommented, Jun 16, 2021

closing this issue for now. feel free to comment if anything is missing or not working and we will follow-up.

Read more comments on GitHub >

github_iconTop Results From Across the Web

DRF Serializer fields DictField() AttributeError: 'str' object has ...
In django rest framework If i use DictField() in serializer its given me ... 'str' object has no attribute 'items' error.please take a...
Read more >
Serializing/validating an unknown JSON structure
have a default JSON serializer which returns everything in the JSON (regardless of it's structure), provided it's valid. optionally add specify validators ...
Read more >
DictField in serializers - Django REST Framework
This article revolves around DictField in Serializers in Django REST ... an error will be raised if None is passed to a serializer...
Read more >
Release Notes - Django REST framework中文站点
Only call FileField.url once in serialization, for improved performance. Fix an edge case where throttling calculations could error after a configuration ...
Read more >
djangorestframework-dataclasses - PyPI
A dataclasses serializer for Django REST Framework. ... for which DRF does not ship a serializer field, some new serializer field types are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found