IndexingPolicy.ToString() changes state of IncludePaths collection (possibly others)
See original GitHub issueDescribe the bug
When setting up the indexing policies on CosmosDB, if we call ToString()
on the IndexingPolicy
object, this changes the state of the IncludePaths
collection associated to the IndexingPolicy
object to include a DefaultPath
of "/*"
. This cause issues for us that result in double-setting if we include the default path explicitly. ToString()
should not change the state of an object and in this case it does, which we determined by reviewing the disassembly of the SDK for the NuGet https://www.nuget.org/packages/Microsoft.Azure.DocumentDB.Core/. Purely viewing the state of the IndexingPolicy
object in the debugger (watching variables, mousing over it, calling ToString() in the immediate window) induces the state change.
To Reproduce We reproduced this by running the following code in VS 2019’s Immediate Window:
var test = new IndexingPolicy();
>> Expression has been evaluated and has no value
test.IncludedPaths.Count
>> 0
test.ToString()
>> "{\r\n \"automatic\": true,\r\n \"indexingMode\": \"Consistent\",\r\n \"includedPaths\": [\r\n {\r\n \"path\": \"/*\",\r\n \"indexes\": []\r\n }\r\n ],\r\n \"excludedPaths\": [],\r\n \"compositeIndexes\": [],\r\n \"spatialIndexes\": []\r\n}"
test.IncludedPaths.Count
>> 1
Expected behavior
We expect that after calling IndexingPolicy.ToString()
that the count remains at 0
and does not change.
Actual behavior
State changes when we call IndexingPolicy.ToString()
Additional context
We looking into disassembling the SDK to determine this, where we found that IndexingPolicy
extends JsonSerializable
. JsonSerializable.ToString()
is implemented as follows:
/// <summary>
/// Returns the string representation of the object in the Azure Cosmos DB service.
/// </summary>
/// <returns>The string representation of the object.</returns>
public override string ToString()
{
this.OnSave();
return this.propertyBag.ToString();
}
Note that if this.OnSave()
changes state at all for any class that extends JsonSerializable
, then calling ToString()
will induce a change in state (so this might be an issue for other classes as well). In the case of IndexingPolicy.OnSave()
, we see that state does change when calling OnSave()
:
internal override void OnSave()
{
if (this.IndexingMode != IndexingMode.None && this.IncludedPaths.get_Count() == 0 && this.ExcludedPaths.get_Count() == 0)
{
this.IncludedPaths.Add(new IncludedPath()
{
Path = IndexingPolicy.DefaultPath
});
}
foreach (IncludedPath includedPath in this.IncludedPaths)
{
includedPath.OnSave();
}
base.SetValue("includedPaths", this.IncludedPaths);
foreach (ExcludedPath excludedPath in this.ExcludedPaths)
{
excludedPath.OnSave();
}
base.SetValue("excludedPaths", this.ExcludedPaths);
foreach (Collection<CompositePath> compositeIndex in this.CompositeIndexes)
{
foreach (CompositePath compositePath in compositeIndex)
{
compositePath.OnSave();
}
}
base.SetValue("compositeIndexes", this.CompositeIndexes);
foreach (SpatialSpec spatialIndex in this.SpatialIndexes)
{
spatialIndex.OnSave();
}
base.SetValue("spatialIndexes", this.spatialIndexes);
}
For reference, this is the class definition for JsonSerializable
:
namespace Microsoft.Azure.Documents
{
/// <summary>
/// Represents the base class for Azure Cosmos DB database objects and provides methods for serializing and deserializing from JSON.
/// </summary>
public abstract class JsonSerializable
And the class definition for IndexingPolicy
:
namespace Microsoft.Azure.Documents
{
/// <summary>
/// Represents the indexing policy configuration for a collection in the Azure Cosmos DB service.
/// </summary>
/// <remarks>
/// Indexing policies can used to configure which properties (JSON paths) are included/excluded, whether the index is updated consistently
/// or offline (lazy), automatic vs. opt-in per-document, as well as the precision and type of index per path.
/// <para>
/// Refer to <see>http://azure.microsoft.com/documentation/articles/documentdb-indexing-policies/</see> for additional information on how to specify
/// indexing policies.
/// </para>
/// </remarks>
/// <seealso cref="T:Microsoft.Azure.Documents.DocumentCollection" />
public sealed class IndexingPolicy : JsonSerializable, ICloneable
Environment SDK Version: https://www.nuget.org/packages/Microsoft.Azure.DocumentDB.Core/ Version 2.11.6 OS Version (e.g. Windows, Linux, MacOSX): Windows, Linux IDE: Visual Studio 2019
Related Issue can be found here,: https://github.com/Azure/azure-cosmos-dotnet-v2/issues/819
Issue Analytics
- State:
- Created 3 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
sounds good, I was told to post the issue on both repos just in case. Thanks!
Hey, @Diana960 the correct repository for this issue is https://github.com/Azure/azure-cosmos-dotnet-v2. I noticed you have already opened the issue there. To avoid duplication, I will close this issue.