|
19 | 19 |
|
20 | 20 | from knack.log import get_logger |
21 | 21 | from knack.util import CLIError, to_snake_case, to_camel_case |
| 22 | +from azure.core.serialization import is_generated_model, attribute_list |
22 | 23 |
|
23 | 24 | logger = get_logger(__name__) |
24 | 25 |
|
@@ -647,16 +648,18 @@ def todict(obj, post_processor=None): |
647 | 648 | # This is the only difference with knack.util.todict because for typespec generated SDKs |
648 | 649 | # The base model stores data in obj.__dict__['_data'] instead of in obj.__dict__ |
649 | 650 | # We need to call obj.as_dict() to extract data for this kind of model |
650 | | - if hasattr(obj, 'as_dict') and not hasattr(obj, '_attribute_map'): |
651 | | - result = {to_camel_case(k): todict(v, post_processor) for k, v in obj.as_dict().items()} |
| 651 | + if is_generated_model(obj): |
| 652 | + # returns True for both msrest model and new hybrid model |
| 653 | + # the backcompat methods handle both models the same way, so you don't need to have separate code |
| 654 | + # for old vs new models |
| 655 | + result = { |
| 656 | + to_camel_case(attr): todict(getattr(obj, attr), post_processor) |
| 657 | + for attr in attribute_list(obj) |
| 658 | + } |
| 659 | + # don't need to verify whether something is callable or private, as attribute_list() already does that |
652 | 660 | return post_processor(obj, result) if post_processor else result |
653 | 661 | if hasattr(obj, '_asdict'): |
654 | 662 | return todict(obj._asdict(), post_processor) |
655 | | - if hasattr(obj, '__dict__'): |
656 | | - result = {to_camel_case(k): todict(v, post_processor) |
657 | | - for k, v in obj.__dict__.items() |
658 | | - if not callable(v) and not k.startswith('_')} |
659 | | - return post_processor(obj, result) if post_processor else result |
660 | 663 | return obj |
661 | 664 |
|
662 | 665 |
|
|
0 commit comments