Skip to content

Commit

Permalink
fix yaml encoder for OrderedDict objects #1158
Browse files Browse the repository at this point in the history
  • Loading branch information
tfranzel committed Jan 28, 2024
1 parent 9bfe009 commit c93bd14
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 1 deletion.
5 changes: 5 additions & 0 deletions drf_spectacular/renderers.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from collections import OrderedDict
from datetime import time, timedelta
from decimal import Decimal
from uuid import UUID
Expand Down Expand Up @@ -53,6 +54,10 @@ def safestring_representer(dumper, data):
return dumper.represent_str(data)
Dumper.add_representer(SafeString, safestring_representer)

def ordereddict_representer(dumper, data):
return dumper.represent_dict(dict(data))
Dumper.add_representer(OrderedDict, ordereddict_representer)

return yaml.dump(
data,
default_flow_style=False,
Expand Down
4 changes: 3 additions & 1 deletion tests/test_regressions.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import collections
import datetime
import re
import typing
Expand Down Expand Up @@ -2057,7 +2058,8 @@ class RouteNestedViewset(viewsets.ModelViewSet):
datetime.timedelta(days=1),
uuid.uuid4(),
Decimal(),
b'deadbeef'
b'deadbeef',
collections.OrderedDict([('a', 1), ('b', 2)]),
])
def test_yaml_encoder_parity(no_warnings, value):
# make sure our YAML renderer does not choke on objects that are fine with
Expand Down

0 comments on commit c93bd14

Please sign in to comment.