Skip to content

Commit e543f2c

Browse files
authored
[Cosmos] fix resource validation to allow longer strings (#36569)
* relevant changes * Update CHANGELOG.md * Update test_resource_id.py * Update test_resource_id_async.py * test fixes * test fix
1 parent da60a42 commit e543f2c

File tree

4 files changed

+35
-7
lines changed

4 files changed

+35
-7
lines changed

sdk/cosmos/azure-cosmos/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#### Bugs Fixed
1010
* Consolidated Container Properties Cache to be in the Client to cache partition key definition and container rid to avoid unnecessary container reads. See [PR 35731](https://github.com/Azure/azure-sdk-for-python/pull/35731)
11+
* Fixed SDK regex validation that would not allow for item ids to be longer than 255 characters. See [PR 36569](https://github.com/Azure/azure-sdk-for-python/pull/36569).
1112

1213
#### Other Changes
1314

sdk/cosmos/azure-cosmos/azure/cosmos/_base.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,9 +65,9 @@
6565

6666
# Cosmos resource ID validation regex breakdown:
6767
# ^ Match start of string.
68-
# [^/\#?]{0,255} Match any character that is not /\#? for between 0-255 characters.
68+
# [^/\#?] Match any character that is not /\#?\n\r\t.
6969
# $ End of string
70-
_VALID_COSMOS_RESOURCE = re.compile(r"^[^/\\#?\t\r\n]{0,255}$")
70+
_VALID_COSMOS_RESOURCE = re.compile(r"^[^/\\#?\t\r\n]*$")
7171

7272

7373
def _get_match_headers(kwargs: Dict[str, Any]) -> Tuple[Optional[str], Optional[str]]:

sdk/cosmos/azure-cosmos/test/test_resource_id.py

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import azure.cosmos
1010
import test_config
11-
from azure.cosmos import PartitionKey, cosmos_client
11+
from azure.cosmos import PartitionKey, cosmos_client, exceptions, http_constants
1212

1313

1414
@pytest.mark.cosmosEmulator
@@ -65,7 +65,7 @@ def test_id_unicode_validation(self):
6565
self.client.delete_database(resource_id1)
6666
self.client.delete_database(resource_id2)
6767

68-
def test_create_illegal_characters_async(self):
68+
def test_create_illegal_characters(self):
6969
database_id = str(uuid.uuid4())
7070
container_id = str(uuid.uuid4())
7171
partition_key = PartitionKey(path="/id")
@@ -86,7 +86,7 @@ def test_create_illegal_characters_async(self):
8686
"ID\r_with_return_carriage",
8787
"ID_with_newline\n",
8888
"ID_with_newline\n2",
89-
"ID_with_more_than_255" + "_" * 255,
89+
"ID_with_more_than_255" + "_add" * 255,
9090
"ID_with_trailing_spaces "
9191
]
9292

@@ -97,23 +97,37 @@ def test_create_illegal_characters_async(self):
9797
self.fail("Database create should have failed for id {}".format(resource_id))
9898
except ValueError as e:
9999
assert str(e) in error_strings
100+
# Let service throw size exception
101+
except exceptions.CosmosHttpResponseError as e:
102+
assert e.status_code == http_constants.StatusCodes.BAD_REQUEST
103+
assert "Ensure to provide a unique non-empty string less than '255' characters." in e.message
100104

101105
try:
102106
created_database.create_container(id=resource_id, partition_key=partition_key)
103107
self.fail("Container create should have failed for id {}".format(resource_id))
104108
except ValueError as e:
105109
assert str(e) in error_strings
110+
except exceptions.CosmosHttpResponseError as e:
111+
assert e.status_code == http_constants.StatusCodes.BAD_REQUEST
112+
assert "Ensure to provide a unique non-empty string less than '255' characters." in e.message
106113

107114
try:
108115
created_container.create_item({"id": resource_id})
109116
self.fail("Item create should have failed for id {}".format(resource_id))
110117
except ValueError as e:
111118
assert str(e) in error_strings
119+
except exceptions.CosmosHttpResponseError as e:
120+
assert e.status_code == http_constants.StatusCodes.BAD_REQUEST
121+
assert "Ensure to provide a unique non-empty string less than '1024' characters." in e.message
122+
112123
try:
113124
created_container.upsert_item({"id": resource_id})
114125
self.fail("Item upsert should have failed for id {}".format(resource_id))
115126
except ValueError as e:
116127
assert str(e) in error_strings
128+
except exceptions.CosmosHttpResponseError as e:
129+
assert e.status_code == http_constants.StatusCodes.BAD_REQUEST
130+
assert "Ensure to provide a unique non-empty string less than '1024' characters." in e.message
117131

118132
self.client.delete_database(database_id)
119133

sdk/cosmos/azure-cosmos/test/test_resource_id_async.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import pytest
88

99
import test_config
10-
from azure.cosmos import PartitionKey
10+
from azure.cosmos import PartitionKey, http_constants, exceptions
1111
from azure.cosmos.aio import CosmosClient, DatabaseProxy
1212

1313

@@ -92,7 +92,7 @@ async def test_create_illegal_characters_async(self):
9292
"ID\r_with_return_carriage",
9393
"ID_with_newline\n",
9494
"ID_with_newline\n2",
95-
"ID_with_more_than_255" + "_" * 255,
95+
"ID_with_more_than_255" + "_added" * 255,
9696
"ID_with_trailing_spaces "
9797
]
9898

@@ -103,23 +103,36 @@ async def test_create_illegal_characters_async(self):
103103
self.fail("Database create should have failed for id {}".format(resource_id))
104104
except ValueError as e:
105105
assert str(e) in error_strings
106+
except exceptions.CosmosHttpResponseError as e:
107+
assert e.status_code == http_constants.StatusCodes.BAD_REQUEST
108+
assert "Ensure to provide a unique non-empty string less than '255' characters." in e.message
106109

107110
try:
108111
await created_database.create_container(id=resource_id, partition_key=partition_key)
109112
self.fail("Container create should have failed for id {}".format(resource_id))
110113
except ValueError as e:
111114
assert str(e) in error_strings
115+
except exceptions.CosmosHttpResponseError as e:
116+
assert e.status_code == http_constants.StatusCodes.BAD_REQUEST
117+
assert "Ensure to provide a unique non-empty string less than '255' characters." in e.message
112118

113119
try:
114120
await created_container.create_item({"id": resource_id})
115121
self.fail("Item create should have failed for id {}".format(resource_id))
116122
except ValueError as e:
117123
assert str(e) in error_strings
124+
except exceptions.CosmosHttpResponseError as e:
125+
assert e.status_code == http_constants.StatusCodes.BAD_REQUEST
126+
assert "Ensure to provide a unique non-empty string less than '1024' characters." in e.message
127+
118128
try:
119129
await created_container.upsert_item({"id": resource_id})
120130
self.fail("Item upsert should have failed for id {}".format(resource_id))
121131
except ValueError as e:
122132
assert str(e) in error_strings
133+
except exceptions.CosmosHttpResponseError as e:
134+
assert e.status_code == http_constants.StatusCodes.BAD_REQUEST
135+
assert "Ensure to provide a unique non-empty string less than '1024' characters." in e.message
123136

124137
await self.client.delete_database(created_database)
125138

0 commit comments

Comments
 (0)