From 9bfe0096c3d3dfbd4e1d1edf0e1c376b2f86e7ab Mon Sep 17 00:00:00 2001 From: "T. Franzel" Date: Sun, 28 Jan 2024 13:15:38 +0100 Subject: [PATCH 1/2] fix linter warning #1158 --- drf_spectacular/renderers.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/drf_spectacular/renderers.py b/drf_spectacular/renderers.py index 1e4c0373..dfdef194 100644 --- a/drf_spectacular/renderers.py +++ b/drf_spectacular/renderers.py @@ -12,7 +12,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): From c93bd14ae3cfc5a8667ab027db5f36b710cbb71b Mon Sep 17 00:00:00 2001 From: "T. Franzel" Date: Sun, 28 Jan 2024 13:16:41 +0100 Subject: [PATCH 2/2] 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