Skip to content

Commit 4254e54

Browse files
authored
{Core} Adopt serialization func from azure-core for CLI's todict formatting (#31775)
1 parent 02809f3 commit 4254e54

1 file changed

Lines changed: 7 additions & 3 deletions

File tree

  • src/azure-cli-core/azure/cli/core

src/azure-cli-core/azure/cli/core/util.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,7 @@ def todict(obj, post_processor=None):
644644
"""
645645
from datetime import date, time, datetime, timedelta
646646
from enum import Enum
647+
from azure.core.serialization import attribute_list
647648
if isinstance(obj, dict):
648649
result = {k: todict(v, post_processor) for (k, v) in obj.items()}
649650
return post_processor(obj, result) if post_processor else result
@@ -657,9 +658,12 @@ def todict(obj, post_processor=None):
657658
return str(obj)
658659
# This is the only difference with knack.util.todict because for typespec generated SDKs
659660
# The base model stores data in obj.__dict__['_data'] instead of in obj.__dict__
660-
# We need to call obj.as_dict() to extract data for this kind of model
661-
if hasattr(obj, 'as_dict') and not hasattr(obj, '_attribute_map'):
662-
result = {to_camel_case(k): todict(v, post_processor) for k, v in obj.as_dict().items()}
661+
# The way to detect if it's a typespec generated model is to check the private `_is_model` attribute
662+
# azure-core provided new function `attribute_list` to list all attribute names
663+
# so that we don't need to use raw __dict__ directly
664+
if getattr(obj, "_is_model", False):
665+
result = {to_camel_case(attr): todict(getattr(obj, attr), post_processor)
666+
for attr in attribute_list(obj) if hasattr(obj, attr)}
663667
return post_processor(obj, result) if post_processor else result
664668
if hasattr(obj, '_asdict'):
665669
return todict(obj._asdict(), post_processor)

0 commit comments

Comments
 (0)