Skip to content

Commit

Permalink
Merge pull request #1487 from JohnVillalovos/jlvillal/check_attrs
Browse files Browse the repository at this point in the history
fix: catch invalid type used to initialize RESTObject
  • Loading branch information
nejch committed Jun 10, 2021
2 parents 161bb0b + c7bcc25 commit 600a2c1
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 0 deletions.
7 changes: 7 additions & 0 deletions gitlab/base.py
Expand Up @@ -20,6 +20,7 @@
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

Expand Down Expand Up @@ -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,
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/test_base.py
Expand Up @@ -19,6 +19,7 @@

import pytest

import gitlab
from gitlab import base


Expand Down Expand Up @@ -85,6 +86,10 @@ def test_instantiate(self, fake_gitlab, fake_manager):
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
Expand Down

0 comments on commit 600a2c1

Please sign in to comment.