4040from ..documents import IndexingMode
4141from ..partition_key import PartitionKey
4242from .._cosmos_responses import CosmosDict
43- from .._global_secondary_index import GlobalSecondaryIndexDefinition
43+ from .._global_secondary_index import GlobalSecondaryIndexDefinition , _normalize_gsi_container_properties
4444
4545
4646__all__ = ("DatabaseProxy" ,)
@@ -469,9 +469,7 @@ async def create_container( # pylint:disable=docstring-should-be-keyword, too-ma
469469 if full_text_policy is not None :
470470 definition ["fullTextPolicy" ] = full_text_policy
471471 if global_secondary_index_definition is not None :
472- gsi_dict = (global_secondary_index_definition ._to_dict ()
473- if hasattr (global_secondary_index_definition , '_to_dict' )
474- else global_secondary_index_definition )
472+ gsi_dict = await self ._resolve_gsi_definition (global_secondary_index_definition )
475473 definition ["globalSecondaryIndexDefinition" ] = gsi_dict
476474 definition ["materializedViewDefinition" ] = gsi_dict
477475 request_options = _build_options (kwargs )
@@ -480,6 +478,7 @@ async def create_container( # pylint:disable=docstring-should-be-keyword, too-ma
480478 data = await self .client_connection .CreateContainer (
481479 database_link = self .database_link , collection = definition , options = request_options , ** kwargs
482480 )
481+ _normalize_gsi_container_properties (data )
483482 if not return_properties :
484483 return ContainerProxy (self .client_connection , self .database_link , data ["id" ], properties = data )
485484 return ContainerProxy (self .client_connection , self .database_link , data ["id" ], properties = data ), data
@@ -772,6 +771,32 @@ def get_container_client(self, container: Union[str, ContainerProxy, dict[str, A
772771 id_value = str (container ['id' ])
773772 return ContainerProxy (self .client_connection , self .database_link , id_value )
774773
774+ async def _resolve_gsi_definition (
775+ self ,
776+ global_secondary_index_definition : Union ["GlobalSecondaryIndexDefinition" , dict [str , Any ]],
777+ ) -> dict [str , Any ]:
778+ """Serialize a GSI definition and populate the source container's resource id (_rid).
779+
780+ The service resolves the source container by its resource id (``sourceCollectionRid``).
781+ When the caller only provides the source container id, read the source container to
782+ obtain its ``_rid`` and populate ``sourceCollectionRid`` on the wire payload.
783+
784+ :param global_secondary_index_definition: The GSI definition object or raw dict.
785+ :type global_secondary_index_definition:
786+ ~azure.cosmos.GlobalSecondaryIndexDefinition or dict[str, Any]
787+ :returns: The serialized GSI definition including ``sourceCollectionRid``.
788+ :rtype: dict[str, Any]
789+ """
790+ gsi_dict = (global_secondary_index_definition ._to_dict () # pylint: disable=protected-access
791+ if hasattr (global_secondary_index_definition , '_to_dict' )
792+ else dict (global_secondary_index_definition ))
793+ if not gsi_dict .get ("sourceCollectionRid" ):
794+ source_container_id = gsi_dict .get ("sourceCollectionId" )
795+ if source_container_id :
796+ source_properties = await self .get_container_client (source_container_id ).read ()
797+ gsi_dict ["sourceCollectionRid" ] = source_properties ["_rid" ]
798+ return gsi_dict
799+
775800 @distributed_trace
776801 def list_containers (
777802 self ,
@@ -1109,15 +1134,14 @@ async def replace_container( # pylint:disable=docstring-should-be-keyword
11091134 if value is not None
11101135 }
11111136 if global_secondary_index_definition is not None :
1112- gsi_dict = (global_secondary_index_definition ._to_dict ()
1113- if hasattr (global_secondary_index_definition , '_to_dict' )
1114- else global_secondary_index_definition )
1137+ gsi_dict = await self ._resolve_gsi_definition (global_secondary_index_definition )
11151138 parameters ["globalSecondaryIndexDefinition" ] = gsi_dict
11161139 parameters ["materializedViewDefinition" ] = gsi_dict
11171140
11181141 container_properties = await self .client_connection .ReplaceContainer (
11191142 container_link , collection = parameters , options = request_options , ** kwargs
11201143 )
1144+ _normalize_gsi_container_properties (container_properties )
11211145
11221146 if not return_properties :
11231147 return ContainerProxy (
0 commit comments