Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

LINQ: Enums convertor using System.Text.Json is not working #4388

Closed
v-mghabin opened this issue Apr 3, 2024 · 6 comments
Closed

LINQ: Enums convertor using System.Text.Json is not working #4388

v-mghabin opened this issue Apr 3, 2024 · 6 comments
Assignees
Labels
customer-reported Issue created by a customer QUERY

Comments

@v-mghabin
Copy link

I've followed this #3250 but sounds it didn't solve the Enums conversation to strings.
The latest solution from that issue is to use a custom serializer CosmosSystemTextJsonSerializer that inherits CosmosSerializer and CosmosLinqSerializer but that won't work because CosmosLinqSerializer is internal class and can't be inherited due to protection level.

My main issue that when trying to use LINQ and filter items based on Enum that has been converted to string, it is not returning any result because it compares the converted string with int.

Record in DB

"State": "Finish"

Enum in C# converted using System.Text.Json Convertor:

[JsonConverter(typeof(JsonStringEnumConverter))]
public enum State
{
    None = 0,
    InProgress = 1,
    Finish = 2,
    Failed = 3,
}
@microsoft-github-policy-service microsoft-github-policy-service bot added the customer-reported Issue created by a customer label Apr 3, 2024
@Maya-Painter Maya-Painter self-assigned this Apr 4, 2024
@Maya-Painter
Copy link
Contributor

Maya-Painter commented Apr 4, 2024

@v-mghabin The code proposed in #3250 is incorrect. I had a discussion with the user here- #2685 (comment)

Also please note that you need to be using the preview version of the newest release. This feature is not yet available in the public version.

Here is an example of how you could implement CosmosLinqSerializer for your scenario. Please give it a try and let me know how it goes:

 class SystemTextJsonLinqSerializer : CosmosLinqSerializer
  {
      private readonly JsonObjectSerializer systemTextJsonSerializer;

      public SystemTextJsonLinqSerializer(JsonSerializerOptions jsonSerializerOptions)
      {
          this.systemTextJsonSerializer = new JsonObjectSerializer(jsonSerializerOptions);
      }

      public override T FromStream<T>(Stream stream)
      {
          if (stream == null)
              throw new ArgumentNullException(nameof(stream));

          using (stream)
          {
              if (stream.CanSeek && stream.Length == 0)
              {
                  return default;
              }

              if (typeof(Stream).IsAssignableFrom(typeof(T)))
              {
                  return (T)(object)stream;
              }

              return (T)this.systemTextJsonSerializer.Deserialize(stream, typeof(T), default);
          }
      }

      public override Stream ToStream<T>(T input)
      {
          MemoryStream streamPayload = new MemoryStream();
          this.systemTextJsonSerializer.Serialize(streamPayload, input, input.GetType(), default);
          streamPayload.Position = 0;
          return streamPayload;
      }

      public override string SerializeMemberName(MemberInfo memberInfo)
      {
          System.Text.Json.Serialization.JsonExtensionDataAttribute jsonExtensionDataAttribute =
              memberInfo.GetCustomAttribute<System.Text.Json.Serialization.JsonExtensionDataAttribute>(true);
          if (jsonExtensionDataAttribute != null)
          {
              return null;
          }

          JsonPropertyNameAttribute jsonPropertyNameAttribute = memberInfo.GetCustomAttribute<JsonPropertyNameAttribute>(true);

          string memberName = !string.IsNullOrEmpty(jsonPropertyNameAttribute?.Name)
              ? jsonPropertyNameAttribute.Name
              : memberInfo.Name;

          return memberName;
      }
  }

@v-mghabin
Copy link
Author

v-mghabin commented Apr 4, 2024

@Maya-Painter
Can u pls elaborate when this change will be released as our team can't use a preview package in production?

@Maya-Painter
Copy link
Contributor

@v-mghabin it will be part of the next public package, which we are hoping to release in the next couple weeks

@v-mghabin
Copy link
Author

@Maya-Painter
I noticed the new release is published but still it is not reflected in NuGet which is weird!
Can u pls elaborate on this?

image
image

@Maya-Painter
Copy link
Contributor

@v-mghabin Apologies for the delay, there are some failing gates blocking the release. More discussion here - #4412

@Maya-Painter
Copy link
Contributor

3.39 has been released, thanks everyone for your patience. CosmosLinqSerializer is included as part of this release and allows for enum conversion in LINQ queries. Closing this issue as addressed. Please feel free to tag me with any follow up questions or concerns.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
customer-reported Issue created by a customer QUERY
Projects
None yet
Development

No branches or pull requests

3 participants