From fc6a1d9354124690e632323d46ee35c3b8c9c8d2 Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Thu, 19 Apr 2018 13:55:37 +0100 Subject: [PATCH 1/2] Fix default index for TaggedJSONSerializer.register() Change the default value of ``index`` to ``None`` in ``register()`` so that it is possible to insert a new tag as the penultimate item in the order list. --- flask/json/tag.py | 8 ++++---- tests/test_json_tag.py | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/flask/json/tag.py b/flask/json/tag.py index 1e51d6fc..2ff0cc6e 100644 --- a/flask/json/tag.py +++ b/flask/json/tag.py @@ -243,7 +243,7 @@ class TaggedJSONSerializer(object): for cls in self.default_tags: self.register(cls) - def register(self, tag_class, force=False, index=-1): + def register(self, tag_class, force=False, index=None): """Register a new tag with this serializer. :param tag_class: tag class to register. Will be instantiated with this @@ -251,8 +251,8 @@ class TaggedJSONSerializer(object): :param force: overwrite an existing tag. If false (default), a :exc:`KeyError` is raised. :param index: index to insert the new tag in the tag order. Useful when - the new tag is a special case of an existing tag. If -1 (default), - the tag is appended to the end of the order. + the new tag is a special case of an existing tag. If ``None`` + (default), the tag is appended to the end of the order. :raise KeyError: if the tag key is already registered and ``force`` is not true. @@ -266,7 +266,7 @@ class TaggedJSONSerializer(object): self.tags[key] = tag - if index == -1: + if index is None: self.order.append(tag) else: self.order.insert(index, tag) diff --git a/tests/test_json_tag.py b/tests/test_json_tag.py index 6f42539e..da5e6595 100644 --- a/tests/test_json_tag.py +++ b/tests/test_json_tag.py @@ -72,3 +72,19 @@ def test_tag_interface(): pytest.raises(NotImplementedError, t.check, None) pytest.raises(NotImplementedError, t.to_json, None) pytest.raises(NotImplementedError, t.to_python, None) + + +def test_tag_order(): + class Tag1(JSONTag): + key = ' 1' + + class Tag2(JSONTag): + key = ' 2' + + s = TaggedJSONSerializer() + + s.register(Tag1, index=-1) + assert isinstance(s.order[-2], Tag1) + + s.register(Tag2, index=None) + assert isinstance(s.order[-1], Tag2) From eaedb9320c7aad581daa05f1510b64393c082dbb Mon Sep 17 00:00:00 2001 From: Nick Pope Date: Thu, 19 Apr 2018 14:11:05 +0100 Subject: [PATCH 2/2] Fix index example for TaggedJSONSerializer.register() Made the example use a keyword, rather than a positional argument, as the intent here is to show how to insert a tag with higher precedence. As a positional argument, this would have been passed to ``force`` instead of ``index``. --- flask/json/tag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/flask/json/tag.py b/flask/json/tag.py index 2ff0cc6e..11c966c5 100644 --- a/flask/json/tag.py +++ b/flask/json/tag.py @@ -36,7 +36,7 @@ processes dicts first, so insert the new tag at the front of the order since def to_python(self, value): return OrderedDict(value) - app.session_interface.serializer.register(TagOrderedDict, 0) + app.session_interface.serializer.register(TagOrderedDict, index=0) :copyright: © 2010 by the Pallets team. :license: BSD, see LICENSE for more details.