Skip to content
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

Handle unparseable objects in the Python client. #2009

Merged
merged 6 commits into from
Jun 17, 2024
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
25 changes: 16 additions & 9 deletions .generator/src/generator/templates/model_utils.j2
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,10 @@ class OpenApiModel(object):
self._check_type,
configuration=self._configuration,
)
if isinstance(value, list):
for x in value:
if isinstance(x, UnparsedObject):
self._unparsed = True
if name in self.validations:
check_validations(self.validations[name], name, value, self._configuration)
self.__dict__["_data_store"][name] = value
Expand Down Expand Up @@ -1332,16 +1336,19 @@ def validate_and_convert_types(
for index, inner_value in enumerate(input_value):
inner_path = list(path_to_item)
inner_path.append(index)
result.append(
validate_and_convert_types(
inner_value,
inner_required_types,
inner_path,
spec_property_naming,
check_type,
configuration=configuration,
try:
result.append(
validate_and_convert_types(
inner_value,
inner_required_types,
inner_path,
spec_property_naming,
check_type,
configuration=configuration,
)
)
)
except TypeError:
result.append(UnparsedObject(**inner_value))
return result
elif isinstance(input_value, dict):
if input_value == {}:
Expand Down
25 changes: 16 additions & 9 deletions src/datadog_api_client/model_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,10 @@ def set_attribute(self, name, value):
self._check_type,
configuration=self._configuration,
)
if isinstance(value, list):
for x in value:
if isinstance(x, UnparsedObject):
self._unparsed = True
if name in self.validations:
check_validations(self.validations[name], name, value, self._configuration)
self.__dict__["_data_store"][name] = value
Expand Down Expand Up @@ -1335,16 +1339,19 @@ def validate_and_convert_types(
for index, inner_value in enumerate(input_value):
inner_path = list(path_to_item)
inner_path.append(index)
result.append(
validate_and_convert_types(
inner_value,
inner_required_types,
inner_path,
spec_property_naming,
check_type,
configuration=configuration,
try:
result.append(
validate_and_convert_types(
inner_value,
inner_required_types,
inner_path,
spec_property_naming,
check_type,
configuration=configuration,
)
)
)
except TypeError:
result.append(UnparsedObject(**inner_value))
return result
elif isinstance(input_value, dict):
if input_value == {}:
Expand Down
23 changes: 23 additions & 0 deletions tests/test_deserialization.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
from datadog_api_client.configuration import Configuration
from datadog_api_client.model_utils import validate_and_convert_types, model_to_dict

from datadog_api_client.model_utils import UnparsedObject
from datadog_api_client.v1.model.synthetics_api_step import SyntheticsAPIStep
from datadog_api_client.v1.model.synthetics_api_test import SyntheticsAPITest
from datadog_api_client.v1.model.synthetics_browser_test import SyntheticsBrowserTest
from datadog_api_client.v1.model.synthetics_assertion import SyntheticsAssertion
Expand Down Expand Up @@ -317,3 +319,24 @@ def test_unknown_model_value():
serialized = model_to_dict(deserialized_data)
assert "allowed_login_methods" in serialized["data"]["attributes"]
assert serialized["data"]["attributes"]["allowed_login_methods"] == []


def test_get_api_test():
value = [
{
"name": "wait",
"subtype": "http",
"extractedValues": [],
"allowFailure": False,
"isCritical": True,
"retry": {"count": 0, "interval": 300},
"assertions": [],
"request": {"method": "GET", "url": "https://example.org", "httpVersion": "any"},
"id": "5p7-km2-d22",
},
{"name": "Wait", "subtype": "wait", "id": "rjn-fmj-sqw"},
]
required_types_mixed = ([SyntheticsAPIStep],)
path_to_item = ["received_data", "config", "steps"]
converted_value = validate_and_convert_types(value, required_types_mixed, path_to_item, True, True, Configuration())
assert isinstance(converted_value[1], UnparsedObject)
Loading