From 209201b9a52826d3b8d617b571921cd6eb8ebda2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Cristi=20V=C3=AEjdea?= Date: Fri, 27 Apr 2018 12:12:36 +0300 Subject: [PATCH] Make insertion order of SwaggerDict extra parameters consistent --- src/drf_yasg/openapi.py | 2 +- tests/test_swaggerdict.py | 15 +++++++++++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/drf_yasg/openapi.py b/src/drf_yasg/openapi.py index 7e7210a..da0a9c3 100644 --- a/src/drf_yasg/openapi.py +++ b/src/drf_yasg/openapi.py @@ -116,7 +116,7 @@ class SwaggerDict(OrderedDict): which would result in the extra attributes being added first. For this reason, we defer the insertion of the attributes and require that subclasses call ._insert_extras__ at the end of their __init__ method. """ - for attr, val in self._extras__.items(): + for attr, val in sorted(self._extras__.items()): setattr(self, attr, val) @staticmethod diff --git a/tests/test_swaggerdict.py b/tests/test_swaggerdict.py index 1ea4029..eb7882c 100644 --- a/tests/test_swaggerdict.py +++ b/tests/test_swaggerdict.py @@ -1,3 +1,6 @@ +from collections import OrderedDict +from random import shuffle + from drf_yasg import openapi @@ -51,3 +54,15 @@ def test_trailing_underscore_stripped(): del sd.in_ assert 'in' not in sd assert not hasattr(sd, 'in__') + + +def test_extra_ordering(): + """Insertion order should also be consistent when setting undeclared parameters (kwargs) in SwaggerDict""" + extras = [('beta', 1), ('alpha', 2), ('omega', 3), ('gamma', 4)] + shuffled_extras = list(extras) + shuffle(shuffled_extras) + + s1 = openapi.SwaggerDict(**OrderedDict(extras)) + s2 = openapi.SwaggerDict(**OrderedDict(shuffled_extras)) + + assert list(s1.items()) == list(s2.items())