Skip to content

fix: catch invalid type used to initialize RESTObject #1487

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions gitlab/base.py
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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