Skip to content

Commit fbeb468

Browse files
committed
set up model classes to ignore unknown dict keys
1 parent 3dcb5de commit fbeb468

File tree

3 files changed

+62
-7
lines changed

3 files changed

+62
-7
lines changed

sign_client/sign_client/model.py

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,13 +41,24 @@ def default(self, o):
4141
return new_dct
4242

4343

44+
def remove_unknown_keys(dct: dict, cls):
45+
known_keys = set()
46+
for field in dataclasses.fields(cls):
47+
known_keys.add(field.name)
48+
new_dct = {}
49+
for k, v in dct.items():
50+
if k in known_keys:
51+
new_dct[k] = v
52+
return new_dct
53+
54+
4455
@dataclass
4556
class PageInfo:
4657
nextCursor: str = None
4758

4859
@classmethod
4960
def from_dict(cls, dct):
50-
return cls(**dct)
61+
return cls(**remove_unknown_keys(dct, cls))
5162

5263

5364
@dataclass
@@ -62,7 +73,7 @@ class UserInfo:
6273

6374
@classmethod
6475
def from_dict(cls, dct):
65-
return cls(**dct)
76+
return cls(**remove_unknown_keys(dct, cls))
6677

6778

6879
@dataclass
@@ -83,7 +94,7 @@ class UserStateInfo:
8394

8495
@classmethod
8596
def from_dict(cls, dct):
86-
return cls(**dct)
97+
return cls(**remove_unknown_keys(dct, cls))
8798

8899

89100
@dataclass
@@ -106,7 +117,7 @@ class DetailedUserInfo:
106117

107118
@classmethod
108119
def from_dict(cls, dct):
109-
return cls(**dct)
120+
return cls(**remove_unknown_keys(dct, cls))
110121

111122

112123
@dataclass
@@ -118,7 +129,7 @@ class GroupInfo:
118129

119130
@classmethod
120131
def from_dict(cls, dct):
121-
return cls(**dct)
132+
return cls(**remove_unknown_keys(dct, cls))
122133

123134

124135
@dataclass
@@ -130,7 +141,7 @@ class DetailedGroupInfo:
130141

131142
@classmethod
132143
def from_dict(cls, dct):
133-
return cls(**dct)
144+
return cls(**remove_unknown_keys(dct, cls))
134145

135146

136147
@dataclass
@@ -167,7 +178,7 @@ def from_dict(cls, dct):
167178
if dct is None:
168179
return None
169180
new_dct = {k: BooleanSettingsInfo.from_dict(v) for k, v in dct.items()}
170-
return cls(**new_dct)
181+
return cls(**remove_unknown_keys(new_dct, cls))
171182

172183

173184
@dataclass
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
from sign_client.model import UserGroupsInfo
2+
import json
3+
4+
def test_deserialize():
5+
"""Test deserialization of a Sign API response"""
6+
# `widgetCreationVisible` is not part of the UserGroupsInfo
7+
# test will pass if user_groups_info is constructed with no errors
8+
9+
# user permission info
10+
api_resp_json = """
11+
{
12+
"groupInfoList": [
13+
{
14+
"id": "",
15+
"isGroupAdmin": false,
16+
"isPrimaryGroup": false,
17+
"status": "",
18+
"createdDate": "date",
19+
"name": "",
20+
"settings": {
21+
"libaryDocumentCreationVisible": {
22+
"value": false,
23+
"inherited": false
24+
},
25+
"sendRestrictedToWorkflows": {
26+
"value": false,
27+
"inherited": false
28+
},
29+
"userCanSend": {
30+
"value": false,
31+
"inherited": false
32+
},
33+
"widgetCreationVisible": {
34+
"value": false,
35+
"inherited": false
36+
}
37+
}
38+
}
39+
]
40+
}
41+
"""
42+
dct = json.loads(api_resp_json)
43+
UserGroupsInfo.from_dict(dct)

0 commit comments

Comments
 (0)