Skip to content

Commit 6f7b0e6

Browse files
Handle unparseable objects in the Python client. (DataDog#2009)
* fix * Update the generator with the changes. * Add test. * Formatting. * Make sure the wait step is invalid.
1 parent f420ca6 commit 6f7b0e6

File tree

3 files changed

+55
-18
lines changed

3 files changed

+55
-18
lines changed

.generator/src/generator/templates/model_utils.j2

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,10 @@ class OpenApiModel(object):
147147
self._check_type,
148148
configuration=self._configuration,
149149
)
150+
if isinstance(value, list):
151+
for x in value:
152+
if isinstance(x, UnparsedObject):
153+
self._unparsed = True
150154
if name in self.validations:
151155
check_validations(self.validations[name], name, value, self._configuration)
152156
self.__dict__["_data_store"][name] = value
@@ -1332,16 +1336,19 @@ def validate_and_convert_types(
13321336
for index, inner_value in enumerate(input_value):
13331337
inner_path = list(path_to_item)
13341338
inner_path.append(index)
1335-
result.append(
1336-
validate_and_convert_types(
1337-
inner_value,
1338-
inner_required_types,
1339-
inner_path,
1340-
spec_property_naming,
1341-
check_type,
1342-
configuration=configuration,
1339+
try:
1340+
result.append(
1341+
validate_and_convert_types(
1342+
inner_value,
1343+
inner_required_types,
1344+
inner_path,
1345+
spec_property_naming,
1346+
check_type,
1347+
configuration=configuration,
1348+
)
13431349
)
1344-
)
1350+
except TypeError:
1351+
result.append(UnparsedObject(**inner_value))
13451352
return result
13461353
elif isinstance(input_value, dict):
13471354
if input_value == {}:

src/datadog_api_client/model_utils.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,10 @@ def set_attribute(self, name, value):
149149
self._check_type,
150150
configuration=self._configuration,
151151
)
152+
if isinstance(value, list):
153+
for x in value:
154+
if isinstance(x, UnparsedObject):
155+
self._unparsed = True
152156
if name in self.validations:
153157
check_validations(self.validations[name], name, value, self._configuration)
154158
self.__dict__["_data_store"][name] = value
@@ -1335,16 +1339,19 @@ def validate_and_convert_types(
13351339
for index, inner_value in enumerate(input_value):
13361340
inner_path = list(path_to_item)
13371341
inner_path.append(index)
1338-
result.append(
1339-
validate_and_convert_types(
1340-
inner_value,
1341-
inner_required_types,
1342-
inner_path,
1343-
spec_property_naming,
1344-
check_type,
1345-
configuration=configuration,
1342+
try:
1343+
result.append(
1344+
validate_and_convert_types(
1345+
inner_value,
1346+
inner_required_types,
1347+
inner_path,
1348+
spec_property_naming,
1349+
check_type,
1350+
configuration=configuration,
1351+
)
13461352
)
1347-
)
1353+
except TypeError:
1354+
result.append(UnparsedObject(**inner_value))
13481355
return result
13491356
elif isinstance(input_value, dict):
13501357
if input_value == {}:

tests/test_deserialization.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
from datadog_api_client.configuration import Configuration
44
from datadog_api_client.model_utils import validate_and_convert_types, model_to_dict
55

6+
from datadog_api_client.model_utils import UnparsedObject
7+
from datadog_api_client.v1.model.synthetics_api_step import SyntheticsAPIStep
68
from datadog_api_client.v1.model.synthetics_api_test import SyntheticsAPITest
79
from datadog_api_client.v1.model.synthetics_browser_test import SyntheticsBrowserTest
810
from datadog_api_client.v1.model.synthetics_assertion import SyntheticsAssertion
@@ -317,3 +319,24 @@ def test_unknown_model_value():
317319
serialized = model_to_dict(deserialized_data)
318320
assert "allowed_login_methods" in serialized["data"]["attributes"]
319321
assert serialized["data"]["attributes"]["allowed_login_methods"] == []
322+
323+
324+
def test_get_api_test():
325+
value = [
326+
{
327+
"name": "wait",
328+
"subtype": "http",
329+
"extractedValues": [],
330+
"allowFailure": False,
331+
"isCritical": True,
332+
"retry": {"count": 0, "interval": 300},
333+
"assertions": [],
334+
"request": {"method": "GET", "url": "https://example.org", "httpVersion": "any"},
335+
"id": "5p7-km2-d22",
336+
},
337+
{"name": "Wait", "subtype": "wait", "id": "rjn-fmj-sqw"},
338+
]
339+
required_types_mixed = ([SyntheticsAPIStep],)
340+
path_to_item = ["received_data", "config", "steps"]
341+
converted_value = validate_and_convert_types(value, required_types_mixed, path_to_item, True, True, Configuration())
342+
assert isinstance(converted_value[1], UnparsedObject)

0 commit comments

Comments
 (0)