From c93bd14ae3cfc5a8667ab027db5f36b710cbb71b Mon Sep 17 00:00:00 2001 From: "T. Franzel" Date: Sun, 28 Jan 2024 13:16:41 +0100 Subject: [PATCH] fix yaml encoder for OrderedDict objects #1158 --- drf_spectacular/renderers.py | 5 +++++ tests/test_regressions.py | 4 +++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/drf_spectacular/renderers.py b/drf_spectacular/renderers.py index dfdef194..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 @@ -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