Skip to content

Instantly share code, notes, and snippets.

@onionhammer
Created May 1, 2023 19:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onionhammer/24f6f148324a4ecae6cc1bb8a89ea9a8 to your computer and use it in GitHub Desktop.
Save onionhammer/24f6f148324a4ecae6cc1bb8a89ea9a8 to your computer and use it in GitHub Desktop.
diff --git a/Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs b/Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs
index 476774e13..1593fc081 100644
--- a/Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs
+++ b/Microsoft.Azure.Cosmos/src/Linq/ExpressionToSQL.cs
@@ -742,6 +742,12 @@ namespace Microsoft.Azure.Cosmos.Linq
SqlScalarExpression memberExpression = ExpressionToSql.VisitScalarExpression(inputExpression.Expression, context);
string memberName = inputExpression.Member.GetMemberName(context.linqSerializerOptions);
+ // If the resulting memberName is empty, then the indexer should be on the root of the object.
+ if (memberName == string.Empty)
+ {
+ return memberExpression;
+ }
+
// if expression is nullable
if (inputExpression.Expression.Type.IsNullable())
{
diff --git a/Microsoft.Azure.Cosmos/src/Linq/TypeSystem.cs b/Microsoft.Azure.Cosmos/src/Linq/TypeSystem.cs
index 34d771bf5..84f2e13f5 100644
--- a/Microsoft.Azure.Cosmos/src/Linq/TypeSystem.cs
+++ b/Microsoft.Azure.Cosmos/src/Linq/TypeSystem.cs
@@ -25,6 +25,14 @@ namespace Microsoft.Azure.Cosmos.Linq
public static string GetMemberName(this MemberInfo memberInfo, CosmosLinqSerializerOptions linqSerializerOptions = null)
{
string memberName = null;
+
+ // Check if Newtonsoft JsonExtensionDataAttribute is present on the member, if so, return empty member name.
+ JsonExtensionDataAttribute jsonExtensionDataAttribute = memberInfo.GetCustomAttribute<JsonExtensionDataAttribute>(true);
+ if (jsonExtensionDataAttribute != null)
+ {
+ return string.Empty;
+ }
+
// Json.Net honors JsonPropertyAttribute more than DataMemberAttribute
// So we check for JsonPropertyAttribute first.
JsonPropertyAttribute jsonPropertyAttribute = memberInfo.GetCustomAttribute<JsonPropertyAttribute>(true);
diff --git a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Linq/CosmosLinqJsonConverterTests.cs b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Linq/CosmosLinqJsonConverterTests.cs
index e4f90f195..8b0556cef 100644
--- a/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Linq/CosmosLinqJsonConverterTests.cs
+++ b/Microsoft.Azure.Cosmos/tests/Microsoft.Azure.Cosmos.Tests/Linq/CosmosLinqJsonConverterTests.cs
@@ -5,6 +5,7 @@
namespace Microsoft.Azure.Cosmos.Linq
{
using System;
+ using System.Collections.Generic;
using System.Globalization;
using System.Linq.Expressions;
using Microsoft.VisualStudio.TestTools.UnitTesting;
@@ -50,5 +51,33 @@ namespace Microsoft.Azure.Cosmos.Linq
}
}
}
+
+ [TestMethod]
+ public void TestNewtonsoftExtensionDataQuery()
+ {
+ Expression<Func<DocumentWithExtensionData, bool>> expr = a => (string)a.NewtonsoftExtensionData["foo"] == "bar";
+ string sql = SqlTranslator.TranslateExpression(expr.Body);
+
+ Assert.AreEqual("(a[\"foo\"] = \"bar\")", sql);
+ }
+
+ [TestMethod]
+ public void TestSystemTextJsonExtensionDataQuery()
+ {
+ Expression<Func<DocumentWithExtensionData, bool>> expr = a => ((object)a.NetExtensionData["foo"]) == "bar";
+ string sql = SqlTranslator.TranslateExpression(expr.Body);
+
+ Assert.AreEqual("(a[\"foo\"] = \"bar\")", sql);
+ }
+
+ class DocumentWithExtensionData
+ {
+ [Newtonsoft.Json.JsonExtensionData(ReadData = true, WriteData = true)]
+ public Dictionary<string, object> NewtonsoftExtensionData { get; set; }
+
+ [System.Text.Json.Serialization.JsonExtensionData()]
+ public Dictionary<string, System.Text.Json.JsonElement> NetExtensionData { get; set; }
+
+ }
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment