Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Cosmos] Fixed incorrect ID type error #11798

Merged
merged 2 commits into from
Jun 4, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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