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

PartitionKey: Fixes NullRef in toString handling for None for PartitionKey.ToString() #3498

Merged
merged 4 commits into from
Oct 11, 2022
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
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