Uploaded image for project: 'C# Driver'
  1. C# Driver
  2. CSHARP-5314

ExpressionFieldDefinition.Render() fails with ImmutableArrays

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Unknown Unknown
    • None
    • Affects Version/s: 2.29.0
    • Component/s: None
    • None
    • Dotnet Drivers
    • Hide

      1. What would you like to communicate to the user about this feature?
      2. Would you like the user to see examples of the syntax and/or executable code and its output?
      3. Which versions of the driver/connector does this apply to?

      Show
      1. What would you like to communicate to the user about this feature? 2. Would you like the user to see examples of the syntax and/or executable code and its output? 3. Which versions of the driver/connector does this apply to?

      Summary

      The following filter fails in an FindOneAndUpdateAsync() call (LinqV3):

      var update = Update
          .PullFilter(d => d.CustomAttributes, a => a.Id == attributeId)
      await Collection.FindOneAndUpdateAsync(filter,...);
       
      // CustomAttributes: ImmutableArray<AttributeConfiguration> { get; init; }
      // Id: Guid
      color: Color value is invalid

      Exception:

      MongoDB.Driver.Linq.ExpressionNotSupportedException
      Expression not supported: Convert(d.CustomAttributes, IEnumerable`1).
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ConvertExpressionToFilterFieldTranslator.Translate(TranslationContext context, UnaryExpression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.Translators.ExpressionToFilterTranslators.ToFilterFieldTranslators.ExpressionToFilterFieldTranslator.Translate(TranslationContext context, Expression expression)
         at MongoDB.Driver.Linq.Linq3Implementation.LinqProviderAdapterV3.TranslateExpressionToField[TDocument](LambdaExpression expression, IBsonSerializer`1 documentSerializer, IBsonSerializerRegistry serializerRegistry)
         at MongoDB.Driver.ExpressionFieldDefinition`1.Render(RenderArgs`1 args)
         at MongoDB.Driver.PullUpdateDefinition`2.Render(RenderArgs`1 args)
         at MongoDB.Driver.CombinedUpdateDefinition`1.Render(RenderArgs`1 args)
         at MongoDB.Driver.MongoCollectionImpl`1.CreateFindOneAndUpdateOperation[TProjection](FilterDefinition`1 filter, UpdateDefinition`1 update, FindOneAndUpdateOptions`2 options)
         at MongoDB.Driver.MongoCollectionImpl`1.FindOneAndUpdateAsync[TProjection](IClientSessionHandle session, FilterDefinition`1 filter, UpdateDefinition`1 update, FindOneAndUpdateOptions`2 options, CancellationToken cancellationToken)
         at Heavendata.Core.Persistence.CustomizableEntities.EntityConfigurationRepositoryBase`1.DeleteAttribute(Guid attributeId)

      How to Reproduce

      I tried to isolate the issue and while it's not exactly the same error message,...

          [Fact]
          public void Driver_should_create_field_definition_from_property_of_type_ImmutableArray()
          {
              Expression<Func<TestModel, IEnumerable<string>>> expression = m => m.Arr;
              var fieldDefinition = new ExpressionFieldDefinition<TestModel>(expression);
              var serializerRegistry = BsonSerializer.SerializerRegistry;
              var documentSerializer = serializerRegistry.GetSerializer<TestModel>();        // This line will throw an exception
              fieldDefinition.Render(new(documentSerializer, serializerRegistry));
          }
      
          public record TestModel(ImmutableArray<string> Arr);

      Fails with:

      System.ArgumentException
      GenericArguments[0], 'System.Collections.Immutable.ImmutableArray`1[System.String]', on 'MongoDB.Bson.Serialization.Serializers.EnumerableInterfaceImplementerSerializer`2[TValue,TItem]' violates the constraint of type 'TValue'.

      Additional Background

      The test above works if you replace ImmutableArray with ImmutableList, or List.

      Changing ImmutableArray<string> to ImmutableArray<MyClass> doesn't change anything.

            Assignee:
            robert@mongodb.com Robert Stam
            Reporter:
            info@christoph-luetjen.de Christoph Lütjen
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved: