Commit 0ccf783
Fix KeyError: 'version' in SessionContainer.get_session_token (#47798)
* [Cosmos] Fix KeyError: 'version' in SessionContainer.get_session_token
The sync and async `SessionContainer.get_session_token` accessed
`collection_pk_definition['version']` with bracket notation. When the
service-returned `partitionKey` payload omits the optional `version`
field (common for containers created without an explicit V2 partition
key), this raised `KeyError`, which was silently swallowed by the
surrounding `except (KeyError, AttributeError)`.
The net effect was that `get_session_token` returned an empty string,
so the client sent no `x-ms-session-token` header on the next read.
Against the Dedicated Gateway / Integrated Cache, every
Session-consistency read was rejected as a cache miss (verified at the
server with `ComputeRequest5M.CacheHitLevel`: 695 reads / 0
CacheFullHit pre-fix vs 678 reads / 677 CacheFullHit post-fix on the
same item).
Treating `version` as optional and passing `None` to `PartitionKey`
when it is missing matches the existing `PartitionKey` constructor
contract and restores Session-consistency cache hits.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* [Cosmos] Add regression tests for partitionKey without 'version'
Covers SessionContainer.get_session_token in tests/test_session_token_unit.py:
test_partitionkey_definition_without_version_returns_token
Mirrors the live failure: container_properties_cache holds a
partitionKey definition without 'version' (as the service returns it
for containers created without explicit V2 partition-key versioning).
Asserts a real per-partition token is returned (was '' before the
fix, because the KeyError was silently swallowed).
test_partitionkey_definition_with_version_returns_same_token
Pins the no-regression case: when 'version' IS present, behavior is
unchanged.
Verified by reverting the fix locally and observing that the without_version
test fails with AssertionError: '' != '0:1#100'.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Apply suggestions from code review
Co-authored-by: Nalu Tripician <27316859+NaluTripician@users.noreply.github.com>
* Fix KeyError in SessionContainer for optional version
Updated the default value of `partitionKey.version` to 1 in `SessionContainer.get_session_token` to match `PartitionKey` behavior.
* Strengthen regression tests to assert V1 EPK
The prior tests used a routing-map stub that discarded the ranges
argument and returned a fixed partition range regardless of the
computed effective partition key, so a wrong-EPK regression (e.g.
version=None falling through to the raw-binary encoding) would still
have passed the assertions on the returned session token.
Replace the stub with a capturing variant that records what
get_overlapping_ranges received, and add two tests:
test_missing_version_produces_v1_hash_epk_not_raw_binary
Computes the V1-hash EPK independently and asserts the captured
EPK matches. Fails if the fix ever regresses to
version=None (the actual EPK would be the raw-binary encoding).
test_v1_and_v2_produce_different_epks
Sanity check: V1 and V2 must produce distinct EPKs, otherwise
the V1-EPK assertion above becomes vacuous.
Verified by temporarily reverting to .get('version') (no default) and
observing the new EPK test fails with AssertionError comparing the
raw-binary encoding against the expected V1 hash.
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Update sdk/cosmos/azure-cosmos/CHANGELOG.md
Co-authored-by: Simon Moreno <30335873+simorenoh@users.noreply.github.com>
* Rename test to spell out effective partition keys (fix cspell)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
* Re-trigger CI (emulator collection-count flake)
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
---------
Co-authored-by: REDMOND\gaausfel <gaausfel@microsoft.com>
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Co-authored-by: Fabian Meiswinkel <fabianm@microsoft.com>
Co-authored-by: Nalu Tripician <27316859+NaluTripician@users.noreply.github.com>
Co-authored-by: Simon Moreno <30335873+simorenoh@users.noreply.github.com>1 parent 7ed6bad commit 0ccf783
3 files changed
Lines changed: 149 additions & 2 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
8 | 8 | | |
9 | 9 | | |
10 | 10 | | |
| 11 | + | |
11 | 12 | | |
12 | 13 | | |
13 | 14 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
119 | 119 | | |
120 | 120 | | |
121 | 121 | | |
122 | | - | |
| 122 | + | |
123 | 123 | | |
124 | 124 | | |
125 | 125 | | |
| |||
213 | 213 | | |
214 | 214 | | |
215 | 215 | | |
216 | | - | |
| 216 | + | |
217 | 217 | | |
218 | 218 | | |
219 | 219 | | |
| |||
Lines changed: 146 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
321 | 321 | | |
322 | 322 | | |
323 | 323 | | |
| 324 | + | |
| 325 | + | |
| 326 | + | |
| 327 | + | |
| 328 | + | |
| 329 | + | |
| 330 | + | |
| 331 | + | |
| 332 | + | |
| 333 | + | |
| 334 | + | |
| 335 | + | |
| 336 | + | |
| 337 | + | |
| 338 | + | |
| 339 | + | |
| 340 | + | |
| 341 | + | |
| 342 | + | |
| 343 | + | |
| 344 | + | |
| 345 | + | |
| 346 | + | |
| 347 | + | |
| 348 | + | |
| 349 | + | |
| 350 | + | |
| 351 | + | |
| 352 | + | |
| 353 | + | |
| 354 | + | |
| 355 | + | |
| 356 | + | |
| 357 | + | |
| 358 | + | |
| 359 | + | |
| 360 | + | |
| 361 | + | |
| 362 | + | |
| 363 | + | |
| 364 | + | |
| 365 | + | |
| 366 | + | |
| 367 | + | |
| 368 | + | |
| 369 | + | |
| 370 | + | |
| 371 | + | |
| 372 | + | |
| 373 | + | |
| 374 | + | |
| 375 | + | |
| 376 | + | |
| 377 | + | |
| 378 | + | |
| 379 | + | |
| 380 | + | |
| 381 | + | |
| 382 | + | |
| 383 | + | |
| 384 | + | |
| 385 | + | |
| 386 | + | |
| 387 | + | |
| 388 | + | |
| 389 | + | |
| 390 | + | |
| 391 | + | |
| 392 | + | |
| 393 | + | |
| 394 | + | |
| 395 | + | |
| 396 | + | |
| 397 | + | |
| 398 | + | |
| 399 | + | |
| 400 | + | |
| 401 | + | |
| 402 | + | |
| 403 | + | |
| 404 | + | |
| 405 | + | |
| 406 | + | |
| 407 | + | |
| 408 | + | |
| 409 | + | |
| 410 | + | |
| 411 | + | |
| 412 | + | |
| 413 | + | |
| 414 | + | |
| 415 | + | |
| 416 | + | |
| 417 | + | |
| 418 | + | |
| 419 | + | |
| 420 | + | |
| 421 | + | |
| 422 | + | |
| 423 | + | |
| 424 | + | |
| 425 | + | |
| 426 | + | |
| 427 | + | |
| 428 | + | |
| 429 | + | |
| 430 | + | |
| 431 | + | |
| 432 | + | |
| 433 | + | |
| 434 | + | |
| 435 | + | |
| 436 | + | |
| 437 | + | |
| 438 | + | |
| 439 | + | |
| 440 | + | |
| 441 | + | |
| 442 | + | |
| 443 | + | |
| 444 | + | |
| 445 | + | |
| 446 | + | |
| 447 | + | |
| 448 | + | |
| 449 | + | |
| 450 | + | |
| 451 | + | |
| 452 | + | |
| 453 | + | |
| 454 | + | |
| 455 | + | |
| 456 | + | |
| 457 | + | |
| 458 | + | |
| 459 | + | |
| 460 | + | |
| 461 | + | |
| 462 | + | |
| 463 | + | |
| 464 | + | |
| 465 | + | |
| 466 | + | |
| 467 | + | |
| 468 | + | |
| 469 | + | |
324 | 470 | | |
325 | 471 | | |
326 | 472 | | |
| |||
0 commit comments