Skip to content

Commit

Permalink
PartitionKey: Fixes NullRef in toString handling for None for Partiti…
Browse files Browse the repository at this point in the history
…onKey.ToString() (#3498)

* Added toString handling for None in addition to existing null

* Added const string representation of None

* Unnecessary using added - removed

* Consistent handling of None for both references to PartitionKey.None.ToString()

Co-authored-by: Adam Holliday <adamho@microsoft.com>
  • Loading branch information
dochollidayxx and Adam Holliday authored Oct 11, 2022
1 parent 1eaae65 commit f679c3b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public override string ToString()
{
if (this.PartitionKey.IsNone)
{
return "None";
return PartitionKey.NoneString;
}

return this.PartitionKey.InternalKey.ToJsonString();
Expand Down
10 changes: 10 additions & 0 deletions Microsoft.Azure.Cosmos/src/PartitionKey.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ namespace Microsoft.Azure.Cosmos
/// </summary>
internal bool IsNone { get; }

/// <summary>
/// String representation of None for use when representing Partition Key as none.
/// </summary>
internal const string NoneString = "None";

/// <summary>
/// Creates a new partition key value.
/// </summary>
Expand Down Expand Up @@ -173,6 +178,11 @@ public bool Equals(PartitionKey other)
/// <returns>The string representation of the partition key value</returns>
public override string ToString()
{
if (this.IsNone)
{
return NoneString;
}

if (this.InternalKey == null)
{
return PartitionKey.NullPartitionKeyInternal.ToJsonString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,15 @@ public void TestPartitionKeyDefinitionAreEquivalent()
Assert.IsTrue(PartitionKeyDefinition.AreEquivalent(definition1, definition2));
}

[TestMethod]
public void NoneToStringNotNullRef()
{
const string noneString = "None";
Cosmos.PartitionKey noneKey = Cosmos.PartitionKey.None;

Assert.AreEqual(noneString, noneKey.ToString());
}

[TestMethod]
public void RoundTripTests()
{
Expand Down

0 comments on commit f679c3b

Please sign in to comment.