diff --git a/pynetbox/core/response.py b/pynetbox/core/response.py index 29e124d9..49d9dc57 100644 --- a/pynetbox/core/response.py +++ b/pynetbox/core/response.py @@ -14,6 +14,7 @@ limitations under the License. """ import copy +from collections import OrderedDict import pynetbox.core.app from six.moves.urllib.parse import urlsplit @@ -341,7 +342,7 @@ def serialize(self, nested=False, init=False): v.id if isinstance(v, Record) else v for v in current_val ] if i in LIST_AS_SET: - current_val = list(set(current_val)) + current_val = list(OrderedDict.fromkeys(current_val)) ret[i] = current_val return ret diff --git a/tests/unit/test_response.py b/tests/unit/test_response.py index f37d4302..dddd32e2 100644 --- a/tests/unit/test_response.py +++ b/tests/unit/test_response.py @@ -209,3 +209,25 @@ def test_endpoint_from_url(self): ) ret = test._endpoint_from_url(test.url) self.assertEqual(ret.name, "test-endpoint") + + def test_serialize_tag_list_order(self): + """Add tests to ensure we're preserving tag order + + This test could still give false-negatives, but making the tag list + longer helps mitigate that. + """ + + test_tags = [ + "one", + "two", + "three", + "four", + "five", + "six", + "seven", + "eight", + "nine", + "ten", + ] + test = Record({"id": 123, "tags": test_tags}, None, None).serialize() + self.assertEqual(test["tags"], test_tags)