diff --git a/sdk/cosmos/azure-cosmos/CHANGELOG.md b/sdk/cosmos/azure-cosmos/CHANGELOG.md index 24a11dd36193..075644d2aeaf 100644 --- a/sdk/cosmos/azure-cosmos/CHANGELOG.md +++ b/sdk/cosmos/azure-cosmos/CHANGELOG.md @@ -1,3 +1,8 @@ +## 4.0.1 (Unreleased) + +- Fixed error raised when a non string ID is used in an item. It now raises TypeError rather than AttributeError. Issue 11793 - thank you @Rabbit994. + + ## 4.0.0 (2020-05-20) - Stable release. diff --git a/sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py b/sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py index 3fa09e0ed51e..f9183e739762 100644 --- a/sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py +++ b/sdk/cosmos/azure-cosmos/azure/cosmos/_cosmos_client_connection.py @@ -31,6 +31,7 @@ from urllib3.util.retry import Retry from azure.core.paging import ItemPaged # type: ignore from azure.core import PipelineClient # type: ignore +from azure.core.exceptions import raise_with_traceback # type: ignore from azure.core.pipeline.policies import ( # type: ignore HTTPPolicy, ContentDecodePolicy, @@ -2480,11 +2481,14 @@ def __CheckAndUnifyQueryFormat(self, query_body): def __ValidateResource(resource): id_ = resource.get("id") if id_: - if id_.find("/") != -1 or id_.find("\\") != -1 or id_.find("?") != -1 or id_.find("#") != -1: - raise ValueError("Id contains illegal chars.") - - if id_[-1] == " ": - raise ValueError("Id ends with a space.") + try: + if id_.find("/") != -1 or id_.find("\\") != -1 or id_.find("?") != -1 or id_.find("#") != -1: + raise ValueError("Id contains illegal chars.") + + if id_[-1] == " ": + raise ValueError("Id ends with a space.") + except AttributeError: + raise_with_traceback(TypeError, message="Id type must be a string.") # Adds the partition key to options def _AddPartitionKey(self, collection_link, document, options): diff --git a/sdk/cosmos/azure-cosmos/azure/cosmos/_version.py b/sdk/cosmos/azure-cosmos/azure/cosmos/_version.py index e2f89349120a..db751d88b3e5 100644 --- a/sdk/cosmos/azure-cosmos/azure/cosmos/_version.py +++ b/sdk/cosmos/azure-cosmos/azure/cosmos/_version.py @@ -19,4 +19,4 @@ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. -VERSION = "4.0.0" +VERSION = "4.0.1" diff --git a/sdk/cosmos/azure-cosmos/samples/access_cosmos_with_resource_token.py b/sdk/cosmos/azure-cosmos/samples/access_cosmos_with_resource_token.py index 9efb03da0ac4..87cc629a9f51 100644 --- a/sdk/cosmos/azure-cosmos/samples/access_cosmos_with_resource_token.py +++ b/sdk/cosmos/azure-cosmos/samples/access_cosmos_with_resource_token.py @@ -14,7 +14,7 @@ # https://docs.microsoft.com/azure/cosmos-db/create-sql-api-python#create-a-database-account # # 2. Microsoft Azure Cosmos -# pip install azure-cosmos==4.0.0 +# pip install azure-cosmos>=4.0.0 # ---------------------------------------------------------------------------------------------------------- # Sample - how to get and use resource token that allows restricted access to data # ---------------------------------------------------------------------------------------------------------- diff --git a/sdk/cosmos/azure-cosmos/test/test_crud.py b/sdk/cosmos/azure-cosmos/test/test_crud.py index d8e4480381a3..ca9294428405 100644 --- a/sdk/cosmos/azure-cosmos/test/test_crud.py +++ b/sdk/cosmos/azure-cosmos/test/test_crud.py @@ -939,6 +939,11 @@ def test_document_upsert(self): self.assertEqual(created_document['id'], document_definition['id']) + # test error for non-string id + with pytest.raises(TypeError): + document_definition['id'] = 7 + created_collection.upsert_item(body=document_definition) + # read documents after creation and verify updated count documents = list(created_collection.read_all_items()) self.assertEqual(