diff --git a/drf_spectacular/renderers.py b/drf_spectacular/renderers.py index 1e4c0373..949331b7 100644 --- a/drf_spectacular/renderers.py +++ b/drf_spectacular/renderers.py @@ -1,3 +1,4 @@ +from collections import OrderedDict from datetime import time, timedelta from decimal import Decimal from uuid import UUID @@ -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): @@ -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, diff --git a/tests/test_regressions.py b/tests/test_regressions.py index 0e33b356..9bdac659 100644 --- a/tests/test_regressions.py +++ b/tests/test_regressions.py @@ -1,3 +1,4 @@ +import collections import datetime import re import typing @@ -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