Skip to content

Commit aa4cf6d

Browse files
authored
Store unknown model properties in _data_store map (#837)
The discarding when coming from the API is not very useful, let's try to store raw data.
1 parent f3a1f5d commit aa4cf6d

File tree

4 files changed

+45
-4
lines changed

4 files changed

+45
-4
lines changed

.generator/templates/model_utils.mustache

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,8 @@ class ModelNormal(OpenApiModel):
494494
self._configuration is not None and \
495495
self._configuration.discard_unknown_keys and \
496496
self.additional_properties_type is None:
497-
# discard variable.
497+
# If it's returned from the API, store it if we need to send it back
498+
self.__dict__["_data_store"][var_name] = var_value
498499
continue
499500
setattr(self, var_name, var_value)
500501
return self

src/datadog_api_client/v1/model_utils.py

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/datadog_api_client/v2/model_utils.py

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test_deserialization.py

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@
88
from datadog_api_client.v2.model.logs_aggregate_response import LogsAggregateResponse
99
from datadog_api_client.v2.model.logs_archive import LogsArchive
1010
from datadog_api_client.v2.model.logs_archive_destination import LogsArchiveDestination
11-
from datadog_api_client.v2.model_utils import validate_and_convert_types as validate_and_convert_types_v2
11+
from datadog_api_client.v2.model.user_response import UserResponse
12+
from datadog_api_client.v2.model_utils import (
13+
validate_and_convert_types as validate_and_convert_types_v2,
14+
model_to_dict as model_to_dict_v2,
15+
)
1216
from datadog_api_client.v2 import Configuration as ConfigurationV2
1317

1418

@@ -283,3 +287,37 @@ def test_one_of_primitive_types():
283287
)
284288
assert isinstance(deserialized_data, LogsAggregateResponse)
285289
assert deserialized_data.data.buckets[0].computes["c0"] == 435.3
290+
291+
292+
def test_unknown_model_value():
293+
body = """{
294+
"data": {
295+
"type": "users",
296+
"id": "6d716162-8b48-11ec-8120-da7ad0900002",
297+
"attributes": {
298+
"name": null,
299+
"created_at": "2022-02-11T14:39:33.168622+00:00",
300+
"modified_at": "2022-02-11T14:39:33.210204+00:00",
301+
"title": "user title",
302+
"verified": false,
303+
"service_account": false,
304+
"disabled": false,
305+
"allowed_login_methods": [],
306+
"status": "Pending"
307+
},
308+
"relationships": {
309+
"roles": { "data": [] },
310+
"org": {
311+
"data": { "type": "orgs", "id": "4dee724d-00cc-11ea-a77b-570c9d03c6c5" }
312+
}
313+
}
314+
}
315+
}"""
316+
config = ConfigurationV2()
317+
deserialized_data = validate_and_convert_types_v2(
318+
json.loads(body), (UserResponse,), ["received_data"], True, True, config
319+
)
320+
assert isinstance(deserialized_data, UserResponse)
321+
serialized = model_to_dict_v2(deserialized_data)
322+
assert "allowed_login_methods" in serialized["data"]["attributes"]
323+
assert serialized["data"]["attributes"]["allowed_login_methods"] == []

0 commit comments

Comments
 (0)