Skip to content

Commit

Permalink
Merge pull request #1160 from tfranzel/fix_ordered_dict
Browse files Browse the repository at this point in the history
Fix ordered dict
  • Loading branch information
tfranzel authored Jan 28, 2024
2 parents a616766 + c93bd14 commit 22b78d6
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
7 changes: 6 additions & 1 deletion 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 All @@ -12,7 +13,7 @@ class OpenApiYamlRenderer(BaseRenderer):
media_type = 'application/vnd.oai.openapi'
format = 'yaml'

def render(self, data, media_type=None, renderer_context=None):
def render(self, data, accepted_media_type=None, renderer_context=None):
# disable yaml advanced feature 'alias' for clean, portable, and readable output
class Dumper(yaml.SafeDumper):
def ignore_aliases(self, data):
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 22b78d6

Please sign in to comment.