From 416b154a7f90d35bf572d05ff69c70c5737cfaf0 Mon Sep 17 00:00:00 2001 From: Matias Quaranta Date: Mon, 9 Jan 2023 14:18:54 -0800 Subject: [PATCH] [Internal] ContainerProperties: Fixes version reset when setting PartitionKeyPath (#3637) * Remember previous value * test --- .../src/Resource/Settings/ContainerProperties.cs | 7 +++++++ .../CosmosContainerSettingsTests.cs | 11 +++++++++++ 2 files changed, 18 insertions(+) diff --git a/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs b/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs index e1eaa6ca96..b95efb99c8 100644 --- a/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs +++ b/Microsoft.Azure.Cosmos/src/Resource/Settings/ContainerProperties.cs @@ -362,10 +362,17 @@ public string PartitionKeyPath throw new ArgumentNullException(nameof(this.PartitionKeyPath)); } + PartitionKeyDefinitionVersion? currentDefinitionVersion = this.PartitionKeyDefinitionVersion; + this.PartitionKey = new PartitionKeyDefinition { Paths = new Collection() { value } }; + + if (currentDefinitionVersion.HasValue) + { + this.PartitionKeyDefinitionVersion = currentDefinitionVersion.Value; + } } } diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs index 84618705de..865bb71d18 100644 --- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs +++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/CosmosContainerSettingsTests.cs @@ -165,6 +165,17 @@ public void ValidateIncludedPathSerialization() } } + [TestMethod] + public void SettingPKShouldNotResetVersion() + { + ContainerProperties containerProperties = new(); + containerProperties.Id = "test"; + containerProperties.PartitionKeyDefinitionVersion = Cosmos.PartitionKeyDefinitionVersion.V2; + containerProperties.PartitionKeyPath = "/id"; + + Assert.AreEqual(Cosmos.PartitionKeyDefinitionVersion.V2, containerProperties.PartitionKeyDefinitionVersion); + } + private static string SerializeDocumentCollection(DocumentCollection collection) { using (MemoryStream ms = new MemoryStream())