Skip to content
This repository was archived by the owner on Feb 28, 2026. It is now read-only.

Fix TypeError: 'NoneType' object is not iterable during deserialization - #193

Closed
Jiashuo Li (jiasli) wants to merge 1 commit into
Azure:masterfrom
jiasli:extractor
Closed

Fix TypeError: 'NoneType' object is not iterable during deserialization#193
Jiashuo Li (jiasli) wants to merge 1 commit into
Azure:masterfrom
jiasli:extractor

Conversation

@jiasli

Copy link
Copy Markdown

msrest can't handle attribute map >= 3 layers during deserialization.

For example, for LongTermEnvironmentCreateOrUpdateParameters

'data_retention': {'key': 'properties.warmStoreConfiguration.dataRetention', 'type': 'duration'},

properties.warmStoreConfiguration.dataRetention has 3 layers. The regex matching this pattern is

'properties\..+?\..+?'

The CLI code I use to construct the body is

body= {}
body['kind'] ='LongTerm'body['location'] =location# strbody['tags'] =tags# dictionarybody.setdefault('sku', {})['name'] =sku_name# strbody.setdefault('sku', {})['capacity'] =sku_capacity# numberbody['time_series_id_properties'] = [{"name": "deviceId", "type": "String"}, {"name": "deviceId2", "type": "String"}] # [TimeSeriesIdProperty]body['storage_configuration'] = {"account_name": storage_account_name, "management_key": storage_management_key} # LongTermStorageConfigurationInputbody['data_retention'] =data_retentionreturnclient.create_or_update(resource_group_name=resource_group, environment_name=name, parameters=body)

Got error:

cli.azure.cli.core.util : Unable to build a model: Unable to deserialize to object: type, TypeError: 'NoneType' object is not iterable, DeserializationError: Unable to deserialize to object: type, TypeError: 'NoneType' object is not iterable
Traceback (most recent call last):
File "d:\cli\msrest-for-python\msrest\serialization.py", line 1328, in _deserialize
found_value = key_extractor(attr, attr_desc, data)
File "d:\cli\msrest-for-python\msrest\serialization.py", line 1110, in rest_key_case_insensitive_extractor
working_data = attribute_key_case_insensitive_extractor(working_key, None, working_data)
File "d:\cli\msrest-for-python\msrest\serialization.py", line 1132, in attribute_key_case_insensitive_extractor
for key in data:
TypeError: 'NoneType' object is not iterable

This is because it first tries to extract properties from the body and gets None, but it doesn’t stop there because warmStoreConfiguration.dataRetention still has . and it continues to extract warmStoreConfiguration from None which results in an error.

This PR makes the loop continue only if working_data evaluates to True.

@lmazuel

Laurent Mazuel (lmazuel) commented Mar 20, 2020

Copy link
Copy Markdown
Member

Jiashuo Li (@jiasli) I'm still not sure, but right now my inclination is more to add a new extractor:

defrest_key_extractor_fail_safe(*args, **kwargs):
try:
returnrest_key_extractor(*args, **kwargs):
exceptException:
returnNone

and then plug this one in the case of the serializer. Making the whole extractor robust, will impact error on regular deserialization from server (this is used in both context).

working_data = data

while '.' in key:
while '.' in key and working_data:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's not enough, when it's None you will end the loop and still fail line 1098

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch. I only looked at the code at L1113~L1114, but didn't notice L1098. I thought they were the same. :P

@lmazuel

Copy link
Copy Markdown
Member

Jiashuo Li (@jiasli) see my comment, I can't merge it as is, and Rodge Fu (@RodgeFu) also found #197 as related issue, so I'm fixing it in #198
That being said, to be sure you're credited in solving this issue I cherry-picked your commit (even if I didn't fix it like you proposed exactly)

@jiasli

Copy link
Copy Markdown
Author

Thanks for looking at this issue Laurent Mazuel (@lmazuel). I am fine with it as long as the issue is properly addressed. Meanwhile, I switched my code to use the Model directly, therefore bypassing this issue.

@jiasli
Jiashuo Li (jiasli) deleted the extractor branch April 7, 2020 17:38
Sign up for freeto subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

@jiasli@lmazuel