Skip to content

Commit 600a2c1

Browse files
authored
Merge pull request #1487 from JohnVillalovos/jlvillal/check_attrs
fix: catch invalid type used to initialize RESTObject
2 parents 161bb0b + c7bcc25 commit 600a2c1

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

gitlab/base.py

+7
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
from typing import Any, Dict, Iterable, NamedTuple, Optional, Tuple, Type
2121

2222
from gitlab import types as g_types
23+
from gitlab.exceptions import GitlabParsingError
2324

2425
from .client import Gitlab, GitlabList
2526

@@ -51,6 +52,12 @@ class RESTObject(object):
5152
manager: "RESTManager"
5253

5354
def __init__(self, manager: "RESTManager", attrs: Dict[str, Any]) -> None:
55+
if not isinstance(attrs, dict):
56+
raise GitlabParsingError(
57+
"Attempted to initialize RESTObject with a non-dictionary value: "
58+
"{!r}\nThis likely indicates an incorrect or malformed server "
59+
"response.".format(attrs)
60+
)
5461
self.__dict__.update(
5562
{
5663
"manager": manager,

tests/unit/test_base.py

+5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
import pytest
2121

22+
import gitlab
2223
from gitlab import base
2324

2425

@@ -85,6 +86,10 @@ def test_instantiate(self, fake_gitlab, fake_manager):
8586
assert fake_manager == obj.manager
8687
assert fake_gitlab == obj.manager.gitlab
8788

89+
def test_instantiate_non_dict(self, fake_gitlab, fake_manager):
90+
with pytest.raises(gitlab.exceptions.GitlabParsingError):
91+
FakeObject(fake_manager, ["a", "list", "fails"])
92+
8893
def test_picklability(self, fake_manager):
8994
obj = FakeObject(fake_manager, {"foo": "bar"})
9095
original_obj_module = obj._module

0 commit comments

Comments
 (0)