Skip to content

Commit ab05237

Browse files
iscai-msftiscai-msft
andauthored
[python] correctly deserialize xml errors (#9488)
Co-authored-by: iscai-msft <isabellavcai@gmail.com>
1 parent 3e5f28c commit ab05237

3 files changed

Lines changed: 34 additions & 6 deletions

File tree

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
changeKind: fix
3+
packages:
4+
- "@typespec/http-client-python"
5+
---
6+
7+
`_failsafe_deserialize_xml` xml errors

packages/http-client-python/generator/pygen/codegen/models/operation.py

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,19 @@ def imports( # pylint: disable=too-many-branches, disable=too-many-statements
448448
elif self.need_deserialize:
449449
file_import.add_submodule_import(relative_path, "_deserialize", ImportType.LOCAL)
450450
if self.default_error_deserialization(serialize_namespace) or self.non_default_errors:
451-
file_import.add_submodule_import(relative_path, "_failsafe_deserialize", ImportType.LOCAL)
451+
xml_non_default_errors = any(
452+
xml_serializable(str(e.default_content_type)) for e in self.non_default_errors
453+
)
454+
try:
455+
default_error = next(e for e in self.exceptions if "default" in e.status_codes and e.type)
456+
except StopIteration:
457+
default_error = None
458+
if xml_non_default_errors or (
459+
default_error and xml_serializable(str(default_error.default_content_type))
460+
):
461+
file_import.add_submodule_import(relative_path, "_failsafe_deserialize_xml", ImportType.LOCAL)
462+
else:
463+
file_import.add_submodule_import(relative_path, "_failsafe_deserialize", ImportType.LOCAL)
452464
return file_import
453465

454466
def get_response_from_status(self, status_code: Optional[Union[str, int]]) -> ResponseType:

packages/http-client-python/generator/pygen/codegen/serializers/builder_serializer.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,9 @@ def handle_error_response( # pylint: disable=too-many-statements, too-many-bran
10451045
retval.extend([f" {l}" for l in response_read])
10461046
retval.append(" map_error(status_code=response.status_code, response=response, error_map=error_map)")
10471047
error_model = ""
1048-
if builder.non_default_errors and self.code_model.options["models-mode"]:
1048+
if ( # pylint: disable=too-many-nested-blocks
1049+
builder.non_default_errors and self.code_model.options["models-mode"]
1050+
):
10491051
error_model = ", model=error"
10501052
condition = "if"
10511053
retval.append(" error = None")
@@ -1062,9 +1064,11 @@ def handle_error_response( # pylint: disable=too-many-statements, too-many-bran
10621064
is_operation_file=True, skip_quote=True, serialize_namespace=self.serialize_namespace
10631065
)
10641066
if self.code_model.options["models-mode"] == "dpg":
1065-
retval.append(
1066-
f" error = _failsafe_deserialize({type_annotation},{pylint_disable}\n response)"
1067-
)
1067+
if xml_serializable(str(e.default_content_type)):
1068+
fn = "_failsafe_deserialize_xml"
1069+
else:
1070+
fn = "_failsafe_deserialize"
1071+
retval.append(f" error = {fn}({type_annotation},{pylint_disable}\n response)")
10681072
else:
10691073
retval.extend(
10701074
[
@@ -1130,9 +1134,14 @@ def handle_error_response( # pylint: disable=too-many-statements, too-many-bran
11301134
if builder.non_default_errors:
11311135
retval.append(" else:")
11321136
if self.code_model.options["models-mode"] == "dpg":
1137+
default_exception = next(e for e in builder.exceptions if "default" in e.status_codes and e.type)
1138+
if xml_serializable(str(default_exception.default_content_type)):
1139+
fn = "_failsafe_deserialize_xml"
1140+
else:
1141+
fn = "_failsafe_deserialize"
11331142
retval.extend(
11341143
[
1135-
f"{indent}error = _failsafe_deserialize(",
1144+
f"{indent}error = {fn}(",
11361145
f"{indent} {default_error_deserialization}",
11371146
f"{indent} response,",
11381147
f"{indent})",

0 commit comments

Comments
 (0)