44# Licensed under the MIT License. See License.txt in the project root for
55# license information.
66# --------------------------------------------------------------------------
7+ # pylint: disable=protected-access
78import base64
89from functools import partial
910from json import JSONEncoder
1920 "as_attribute_dict" ,
2021 "attribute_list" ,
2122 "TypeHandlerRegistry" ,
23+ "get_backcompat_attr_name" ,
2224]
2325TZ_UTC = timezone .utc
2426
@@ -317,7 +319,7 @@ def _is_readonly(p: Any) -> bool:
317319 :rtype: bool
318320 """
319321 try :
320- return p ._visibility == ["read" ] # pylint: disable=protected-access
322+ return p ._visibility == ["read" ]
321323 except AttributeError :
322324 return False
323325
@@ -332,6 +334,20 @@ def _as_attribute_dict_value(v: Any, *, exclude_readonly: bool = False) -> Any:
332334 return as_attribute_dict (v , exclude_readonly = exclude_readonly ) if is_generated_model (v ) else v
333335
334336
337+ def _get_backcompat_name (rest_field : Any , default_attr_name : str ) -> str :
338+ """Get the backcompat name for an attribute.
339+
340+ :param any rest_field: The rest field to get the backcompat name from.
341+ :param str default_attr_name: The default attribute name to use if no backcompat name
342+ :return: The backcompat name.
343+ :rtype: str
344+ """
345+ try :
346+ return rest_field ._original_tsp_name or default_attr_name
347+ except AttributeError :
348+ return default_attr_name
349+
350+
335351def _get_flattened_attribute (obj : Any ) -> Optional [str ]:
336352 """Get the name of the flattened attribute in a generated TypeSpec model if one exists.
337353
@@ -348,11 +364,9 @@ def _get_flattened_attribute(obj: Any) -> Optional[str]:
348364 if flattened_items is None :
349365 return None
350366
351- for k , v in obj ._attr_to_rest_field .items (): # pylint: disable=protected-access
367+ for k , v in obj ._attr_to_rest_field .items ():
352368 try :
353- if set (v ._class_type ._attr_to_rest_field .keys ()).intersection ( # pylint: disable=protected-access
354- set (flattened_items )
355- ):
369+ if set (v ._class_type ._attr_to_rest_field .keys ()).intersection (set (flattened_items )):
356370 return k
357371 except AttributeError :
358372 # if the attribute does not have _class_type, it is not a typespec generated model
@@ -372,12 +386,12 @@ def attribute_list(obj: Any) -> List[str]:
372386 raise TypeError ("Object is not a generated SDK model." )
373387 if hasattr (obj , "_attribute_map" ):
374388 # msrest model
375- return list (obj ._attribute_map .keys ()) # pylint: disable=protected-access
389+ return list (obj ._attribute_map .keys ())
376390 flattened_attribute = _get_flattened_attribute (obj )
377391 retval : List [str ] = []
378- for attr_name , rest_field in obj ._attr_to_rest_field .items (): # pylint: disable=protected-access
392+ for attr_name , rest_field in obj ._attr_to_rest_field .items ():
379393 if flattened_attribute == attr_name :
380- retval .extend (attribute_list (rest_field ._class_type )) # pylint: disable=protected-access
394+ retval .extend (attribute_list (rest_field ._class_type ))
381395 else :
382396 retval .append (attr_name )
383397 return retval
@@ -410,16 +424,16 @@ def as_attribute_dict(obj: Any, *, exclude_readonly: bool = False) -> Dict[str,
410424 # create a reverse mapping from rest field name to attribute name
411425 rest_to_attr = {}
412426 flattened_attribute = _get_flattened_attribute (obj )
413- for attr_name , rest_field in obj ._attr_to_rest_field .items (): # pylint: disable=protected-access
427+ for attr_name , rest_field in obj ._attr_to_rest_field .items ():
414428
415429 if exclude_readonly and _is_readonly (rest_field ):
416430 # if we're excluding readonly properties, we need to track them
417- readonly_props .add (rest_field ._rest_name ) # pylint: disable=protected-access
431+ readonly_props .add (rest_field ._rest_name )
418432 if flattened_attribute == attr_name :
419- for fk , fv in rest_field ._class_type ._attr_to_rest_field .items (): # pylint: disable=protected-access
420- rest_to_attr [fv ._rest_name ] = fk # pylint: disable=protected-access
433+ for fk , fv in rest_field ._class_type ._attr_to_rest_field .items ():
434+ rest_to_attr [fv ._rest_name ] = fk
421435 else :
422- rest_to_attr [rest_field ._rest_name ] = attr_name # pylint: disable=protected-access
436+ rest_to_attr [rest_field ._rest_name ] = attr_name
423437 for k , v in obj .items ():
424438 if exclude_readonly and k in readonly_props : # pyright: ignore
425439 continue
@@ -429,10 +443,8 @@ def as_attribute_dict(obj: Any, *, exclude_readonly: bool = False) -> Dict[str,
429443 else :
430444 is_multipart_file_input = False
431445 try :
432- is_multipart_file_input = next ( # pylint: disable=protected-access
433- rf
434- for rf in obj ._attr_to_rest_field .values () # pylint: disable=protected-access
435- if rf ._rest_name == k # pylint: disable=protected-access
446+ is_multipart_file_input = next (
447+ rf for rf in obj ._attr_to_rest_field .values () if rf ._rest_name == k
436448 )._is_multipart_file_input
437449 except StopIteration :
438450 pass
@@ -444,3 +456,36 @@ def as_attribute_dict(obj: Any, *, exclude_readonly: bool = False) -> Dict[str,
444456 except AttributeError as exc :
445457 # not a typespec generated model
446458 raise TypeError ("Object must be a generated model instance." ) from exc
459+
460+
461+ def get_backcompat_attr_name (model : Any , attr_name : str ) -> str :
462+ """Get the backcompat attribute name for a given attribute.
463+
464+ This function takes an attribute name and returns the backcompat name (original TSP name)
465+ if one exists, otherwise returns the attribute name itself.
466+
467+ :param any model: The model instance.
468+ :param str attr_name: The attribute name to get the backcompat name for.
469+ :return: The backcompat attribute name (original TSP name) or the attribute name itself.
470+ :rtype: str
471+ """
472+ if not is_generated_model (model ):
473+ raise TypeError ("Object must be a generated model instance." )
474+
475+ # Check if attr_name exists in the model's attributes
476+ flattened_attribute = _get_flattened_attribute (model )
477+ for field_attr_name , rest_field in model ._attr_to_rest_field .items ():
478+ # Check if this is the attribute we're looking for
479+ if field_attr_name == attr_name :
480+ # Return the original TSP name if it exists, otherwise the attribute name
481+ return _get_backcompat_name (rest_field , attr_name )
482+
483+ # If this is a flattened attribute, check inside it
484+ if flattened_attribute == field_attr_name :
485+ for fk , fv in rest_field ._class_type ._attr_to_rest_field .items ():
486+ if fk == attr_name :
487+ # Return the original TSP name for this flattened property
488+ return _get_backcompat_name (fv , fk )
489+
490+ # If not found in the model, just return the attribute name as-is
491+ return attr_name
0 commit comments