Using linq3 driver and custom serializer with IQueryable on nested properties result in error 'does not represent members as fields'

Currently we have a specification object which contain a property like “TrimName”. TrimName is another object typed “NormalizedString” which contains two properties like below.

public class NormalizedString
{
    public string Value { get; init; }
    public string NormalizedValue { get; init; } 
}

We are trying to filter with the “Value” property by using IQueryable like so →

Collection.AsQueryable().Where(x => x.TrimName.Value == "something")

We receive this error as a result →

System.NotSupportedException: Serializer for Domain.ValueObjects.NormalizedString does not represent members as fields.
   at MongoDB.Driver.Linq.Linq3Implementation.Misc.DocumentSerializerHelper.GetMemberSerializationInfo(IBsonSerializer serializer, String memberName)

Note that we are also using a custom serializer for our NormalizedString class. The serialize or Deserialize method doesn’t get trigger at this stage but it is correctly registered using IBsonSerializer? GetSerializer(Type type) method

Is this some kind of limitation or we are missing something?
Thank you!

Hi, @pierre-luc_des,

When writing a custom serializer that stores types as subdocuments, then you must implement IBsonDocumentSerializer as a well as IBsonDocument<T>. IBsonDocumentSerializer provides a single method TryGetMemberSerializationInfo, which allows the lookup of a serializer from a property name. In this case, it is responsible for returning the StringSerializer for NormalizedString.Value.

Note that if you never accessed NormalizedString.Value and treated NormalizedString as a scalar type (e.g. x => x.TrimValue == new NormalizedString("something"), then your serializer would not need to implement IBsonDocumentSerializer. It is only because you are accessing properties of NormalizedString within your lambda expression that the driver requires a way to locate BsonSerializationInfo for its properties.

Hope that helps.

Sincerely,
James

Thank you! Implementing IBsonDocumentSerializer with TryGetMemberSerializationInfo resolve our issue

This topic was automatically closed 5 days after the last reply. New replies are no longer allowed.