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.

StringEnumConverter in CosmosSerializer is ignored when making a LINQ query against an enum

See original GitHub issue

Describe 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:open
  • Created 2 years ago
  • Reactions:1
  • Comments:7 (1 by maintainers)

github_iconTop GitHub Comments

2reactions
ivancavlekcommented, Apr 21, 2022

@j82w, @sboshra, please reopen this issue.

1reaction
Ettores88commented, Apr 20, 2022

@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)

Read more comments on GitHub >

github_iconTop 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 >

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