Make insertion order of SwaggerDict extra parameters consistent

openapi3
Cristi Vîjdea 2018-04-27 12:12:36 +03:00
parent ca00ed35be
commit 209201b9a5
2 changed files with 16 additions and 1 deletions

View File

@ -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 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. 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) setattr(self, attr, val)
@staticmethod @staticmethod

View File

@ -1,3 +1,6 @@
from collections import OrderedDict
from random import shuffle
from drf_yasg import openapi from drf_yasg import openapi
@ -51,3 +54,15 @@ def test_trailing_underscore_stripped():
del sd.in_ del sd.in_
assert 'in' not in sd assert 'in' not in sd
assert not hasattr(sd, 'in__') 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())