Description
I have the following case:
I have a couple of classes that inherit from a base class. This is because the only difference between the classes are the property names in Cosmos. The business logic is done by using the base class object.
The following reproduces the issue
using Microsoft.Azure.Cosmos;
using Microsoft.Azure.Cosmos.Linq;
using Newtonsoft.Json;
public class CosmosSerializerTests
{
[Fact]
public void TestInheritance()
{
var cosmosClient = new CosmosClient(Settings.CosmosConnectionString);
var container = cosmosClient.GetDatabase("mydb").GetContainer("myContainer");
var query = container.GetItemLinqQueryable<ChildClass>()
.Where(x => x.Name == "test" && x.Name2 == "test2");
var sql = query.ToQueryDefinition().QueryText;
Assert.Equal("SELECT VALUE root FROM root WHERE ((root[\"my_custom_child_name\"] = \"test\") " +
"AND (root[\"my_custom_child_name2\"] = \"test2\"))", sql);
//Actual: "SELECT VALUE root FROM root WHERE ((root[\"my_custom_parent_name\"] = \"test\")
// AND (root[\"my_custom_child_name2\"] = \"test2\"
}
public class ChildClass : ParentClass
{
[JsonProperty("my_custom_child_name")]
public override string? Name { get; set; }
[JsonProperty("my_custom_child_name2")]
public string? Name2 { get; set; }
}
public abstract class ParentClass
{
[JsonProperty("my_custom_parent_name")]
public abstract string? Name { get; set; }
}
}
I believe the JsonProperty attributes should be read from the ChildClass in the scenario above.
Instead, it's using the parent's JsonProperty value and translates it to my_custom_parent_name
instead of my_custom_child_name
. The Name2 property that is not overriding anything works as expected
The above is using Newtonsoft.Json
. Switching it to use System.Text.Json.Serialization
:
using System.Text.Json.Serialization;
...
public class ChildClass : ParentClass
{
[JsonPropertyName("my_custom_child_name")]
public override string? Name { get; set; }
[JsonPropertyName("my_custom_child_name2")]
public string? Name2 { get; set; }
}
public abstract class ParentClass
{
[JsonPropertyName("my_custom_parent_name")]
public abstract string? Name { get; set; }
}
This results in no translation at all - the test result shows
SELECT VALUE root FROM root WHERE ((root["Name"] = "test") AND (root["Name2"] = "test2"))
I tried Microsoft.Azure.Cosmos
3.37.1 and 3.41.0
The alternative I have right now is to execute direct SQL text instead