{Core} Adopt serialization func from azure-core for CLI's todict formatting#31775
{Core} Adopt serialization func from azure-core for CLI's todict formatting#31775
todict formatting#31775Conversation
️✔️AzureCLI-FullTest
|
️✔️AzureCLI-BreakingChangeTest
|
|
Thank you for your contribution! We will review the pull request and get back to you soon. |
|
The git hooks are available for azure-cli and azure-cli-extensions repos. They could help you run required checks before creating the PR. Please sync the latest code with latest dev branch (for azure-cli) or main branch (for azure-cli-extensions). pip install azdev --upgrade
azdev setup -c <your azure-cli repo path> -r <your azure-cli-extensions repo path>
|
There was a problem hiding this comment.
Pull Request Overview
This PR updates the CLI’s todict utility to use Azure Core’s serialization helpers (is_generated_model and attribute_list) instead of the older as_dict logic, ensuring correct handling of renamed/ formatted SDK model attributes.
- Import
is_generated_modelandattribute_listfromazure.core.serialization - Replace the old
as_dictbranch with a single path usingis_generated_model - Remove legacy typespec
as_dicthandling
Comments suppressed due to low confidence (2)
src/azure-cli-core/azure/cli/core/util.py:654
- Add unit tests for this new
is_generated_modelbranch to cover both swagger and typespec generated models, verifying thatattribute_listcorrectly enumerates renamed/formatted attributes.
if is_generated_model(obj):
src/azure-cli-core/azure/cli/core/util.py:629
- Update the function docstring to mention the support for Azure Core generated models via
is_generated_modelandattribute_list.
def todict(obj, post_processor=None):
todict formatting
| if is_generated_model(obj): | ||
| result = {} | ||
| for attr in attribute_list(obj): | ||
| if hasattr(obj, attr): |
There was a problem hiding this comment.
We need to check whether the attr exist because we may import SDK model and then do some customization like keyvault certificate L122-127
azure-cli/src/azure-cli/azure/cli/command_modules/keyvault/custom.py
Lines 69 to 128 in bb2b31a
Some attributes are deleted
| # The way to detect if it's a typespec generated model is to check the private `_is_model` attribute | ||
| # azure-core provided new function `attribute_list` to list all attribute names | ||
| # so that we don't need to use raw __dict__ directly | ||
| if getattr(obj, "_is_model", False): |
There was a problem hiding this comment.
How did it handle the nested case? When the internal property is also an object?
There was a problem hiding this comment.
we will call todict recursively
Related command
All cli commands
Description
Previously we added dict transformation for typespec generated SDK models in #30339, we called typespec generated SDK model's
as_dictfunc to get each attribute name and attribute value. But this still causes issues like #31529 reported whenWe can take the
expiredattribute from Keyvault SDK as an example:The
expiresis renamed fromexpin http response and hasunix-timestamptime format. By callingas_dict{'expires': '2027-05-28T16:43:07+00:00'}(from swagger generated SDK model){'exp': 1811522587}(from typespec generated SDK model).The renaming and formatting was not applied to
as_dictand this behavior applies for all typespec generated SDK models.To resolve this breaking change, SDK team added related serialization support in azure-core with Azure/azure-sdk-for-python#41445, Azure/azure-sdk-for-python#41571. They now have two functions to handle both swagger generated SDK and typespec generated SDK:
is_generated_modelto identify auto generated SDK modelsattribute_listto list all attribute names which are after renaminggetattr(obj, attr)for the attribute we get fromattribute_listto get the attribute value which is after formattingThis PR changes the dict formatting for typespec generated SDK models with new
attribute_listfunc. We didn't adoptis_generated_modelbecause we don't want to change swagger generated SDK models' dict formatting to avoid any regressions.Testing Guide
History Notes
[Component Name 1] BREAKING CHANGE:
az command a: Make some customer-facing breaking change[Component Name 2]
az command b: Add some customer-facing featureThis checklist is used to make sure that common guidelines for a pull request are followed.
The PR title and description has followed the guideline in Submitting Pull Requests.
I adhere to the Command Guidelines.
I adhere to the Error Handling Guidelines.