StringEnumConverter in CosmosSerializer is ignored when making a LINQ query against an enum
See original GitHub issueDescribe the bug When working with enum serialized as string in simple where clause, the expression is properly translated in SQL using the string value of the enum when StringEnumConverter is set as a C# attribute. But it doesn’t work when working with Fluent interface and inheriting from CosmosSerializer.
To Reproduce
public enum Status
{
StatusA,
StatusB
}
public class CosmosNewtonsoftJsonSerializer : CosmosSerializer
{
private readonly JsonSerializerSettings _jsonSerializerSettings;
public CosmosNewtonsoftJsonSerializer()
{
_jsonSerializerSettings = new JsonSerializerSettings
{
Converters = new List<JsonConverter>
{
new StringEnumConverter()
}
};
_jsonSerializer = JsonSerializer.Create(_jsonSerializerSettings);
}
...
}
Expected behavior
var query = container.GetItemLinqQueryable<Foo>();
query = query.Where(foo => foo.Status == Status.StatusA);
Console.WriteLine(query.ToQueryDefinition().QueryText);
SELECT VALUE root FROM root WHERE (root["Status"] = "StatusA")
Actual behavior
SELECT VALUE root FROM root WHERE (root["Status"] = 0)
Environment summary SDK Version: 3.17.1 Windows 10 Working with Cosmos Emulator
Additional context
public class Foo
{
[JsonConverter(typeof(StringEnumConverter))]
public FooStatus Status { get; set; }
}
This configuration works as intended.
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:7 (1 by maintainers)
Top Results From Across the Web
Enum cannot be serialized as string in CosmosDb #1063
I have an object that I wan't to save as a document in a CosmosDb container. This object contains some Enum properties.
Read more >JObject ToString with StringEnumConverter not working
1 Answer. Your problem is that you are applying StringEnumConverter too late in the process. Json.NET converts the enum to a string or...
Read more >Enum Support - Code First - EF6
This video shows how to use enum types with Entity Framework Code First. It also demonstrates how to use enums in a LINQ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@j82w, @sboshra, please reopen this issue.
@HMoen but that is what I don’t want the developers to do. Most of the time the dev is going to decorate the Enum in the end instead of always doing this. It becomes almost meaningless that I have centralized a serialization strategy. (without considering if I want to do something completely strange in the serializer, this wouldn’t solve my problem)