diff options
author | John L. Villalovos <john@sodarock.com> | 2021-05-31 10:47:03 -0700 |
---|---|---|
committer | John L. Villalovos <john@sodarock.com> | 2021-06-10 07:39:58 -0700 |
commit | c7bcc25a361f9df440f9c972672e5eec3b057625 (patch) | |
tree | de9bc431060437fe259115c920c67389a0cccb5f | |
parent | 161bb0bf1684374ed01c4e3bc8ebc2f5afe7546b (diff) | |
download | gitlab-c7bcc25a361f9df440f9c972672e5eec3b057625.tar.gz |
fix: catch invalid type used to initialize RESTObject
Sometimes we have errors where we don't get a dictionary passed to
RESTObject.__init__() method. This breaks things but in confusing
ways.
Check in the __init__() method and raise an exception if it occurs.
-rw-r--r-- | gitlab/base.py | 7 | ||||
-rw-r--r-- | tests/unit/test_base.py | 5 |
2 files changed, 12 insertions, 0 deletions
diff --git a/gitlab/base.py b/gitlab/base.py index 689b68c..bea1901 100644 --- a/gitlab/base.py +++ b/gitlab/base.py @@ -20,6 +20,7 @@ from types import ModuleType from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type from gitlab import types as g_types +from gitlab.exceptions import GitlabParsingError from .client import Gitlab, GitlabList @@ -51,6 +52,12 @@ class RESTObject(object): manager: "RESTManager" def __init__(self, manager: "RESTManager", attrs: Dict[str, Any]) -> None: + if not isinstance(attrs, dict): + raise GitlabParsingError( + "Attempted to initialize RESTObject with a non-dictionary value: " + "{!r}\nThis likely indicates an incorrect or malformed server " + "response.".format(attrs) + ) self.__dict__.update( { "manager": manager, diff --git a/tests/unit/test_base.py b/tests/unit/test_base.py index b3a58fc..8872dbd 100644 --- a/tests/unit/test_base.py +++ b/tests/unit/test_base.py @@ -19,6 +19,7 @@ import pickle import pytest +import gitlab from gitlab import base @@ -85,6 +86,10 @@ class TestRESTObject: assert fake_manager == obj.manager assert fake_gitlab == obj.manager.gitlab + def test_instantiate_non_dict(self, fake_gitlab, fake_manager): + with pytest.raises(gitlab.exceptions.GitlabParsingError): + FakeObject(fake_manager, ["a", "list", "fails"]) + def test_picklability(self, fake_manager): obj = FakeObject(fake_manager, {"foo": "bar"}) original_obj_module = obj._module |