Skip to content

Commit

Permalink
[Cosmos] Fixed incorrect ID type error (#11798)
Browse files Browse the repository at this point in the history
* Fixed incorrect ID type error

* Fix pylint
  • Loading branch information
annatisch authored Jun 4, 2020
1 parent 0ec1601 commit 9f67111
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 7 deletions.
5 changes: 5 additions & 0 deletions sdk/cosmos/azure-cosmos/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion sdk/cosmos/azure-cosmos/azure/cosmos/_version.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ----------------------------------------------------------------------------------------------------------
Expand Down
5 changes: 5 additions & 0 deletions sdk/cosmos/azure-cosmos/test/test_crud.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 9f67111

Please sign in to comment.