question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IndexingPolicy.ToString() changes state of IncludePaths collection (possibly others)

See original GitHub issue

Describe 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:closed
  • Created 3 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
Diana960commented, Sep 24, 2020

sounds good, I was told to post the issue on both repos just in case. Thanks!

0reactions
MehaKaushikcommented, Sep 24, 2020

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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Manage indexing policies in Azure Cosmos DB
In Azure Cosmos DB, data is indexed following indexing policies that are defined for each container. The default indexing policy for newly ...
Read more >
Introduction To Azure Cosmos DB PDF
Set variables for the new SQL API account, database, and container ... The ToString() method is used to format the output, it's used...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found