Details
-
Bug
-
Resolution: Done
-
Minor - P4
-
2.2
Description
In a usage scenario that worked perfectly on 1.x, I am getting System.InvalidOperationException : The serializer for field 'EnabledForProduct' must implement IBsonArraySerializer and provide item serialization info.
Where EnabledForProduct is mapped like this:
BsonClassMap.RegisterClassMap<TestClass>(cm =>
|
{
|
cm.AutoMap();
|
cm.MapMember(c => c.EnabledForProduct)
|
.SetSerializer(new DictionaryInterfaceImplementerSerializer<Dictionary<TestProduct, bool>>(
|
DictionaryRepresentation.ArrayOfDocuments, BsonSerializer.LookupSerializer<int>(),
|
BsonSerializer.LookupSerializer<bool>())
|
);
|
});
|
A Full NUnit test that reproduces the error follows:
using System;
|
using System.Collections.Generic;
|
using MongoDB.Bson.Serialization;
|
using MongoDB.Bson.Serialization.Options;
|
using MongoDB.Bson.Serialization.Serializers;
|
using MongoDB.Driver;
|
using MongoDB.Driver.Builders;
|
using NUnit.Framework;
|
|
|
namespace CredMarket.Tests
|
{
|
[TestFixture]
|
public class ElemMatchTest
|
{
|
[Test]
|
public void TestElemMatch()
|
{
|
BsonClassMap.RegisterClassMap<TestClass>(cm =>
|
{
|
cm.AutoMap();
|
cm.MapMember(c => c.EnabledForProduct)
|
.SetSerializer(new DictionaryInterfaceImplementerSerializer<Dictionary<TestProduct, bool>>(
|
DictionaryRepresentation.ArrayOfDocuments, BsonSerializer.LookupSerializer<int>(),
|
BsonSerializer.LookupSerializer<bool>())
|
);
|
});
|
var client = new MongoClient("mongodb://localhost");
|
var database = client.GetDatabase("test");
|
var collection = database.GetCollection<TestClass>("testcollection");
|
collection.DeleteMany(FilterDefinition<TestClass>.Empty);
|
|
|
//Creating Document
|
var doc = new TestClass
|
{
|
Id = Guid.NewGuid().ToString(),
|
EnabledForProduct = new Dictionary<TestProduct, bool>
|
{
|
{TestProduct.Product1, true}
|
}
|
};
|
|
|
collection.InsertOne(doc);
|
|
|
var foundDoc = collection.Find(d=>d.Id == doc.Id).Limit(1).FirstOrDefault();
|
AssertDoc(foundDoc);
|
|
|
var filter = Builders<TestClass>.Filter.ElemMatch(d => d.EnabledForProduct,
|
k => k.Key == TestProduct.Product1 && k.Value);
|
foundDoc = collection.Find(filter).Limit(1).FirstOrDefault();
|
AssertDoc(foundDoc);
|
}
|
|
|
private static void AssertDoc(TestClass foundDoc)
|
{
|
Assert.IsNotNull(foundDoc);
|
Assert.IsNotNull(foundDoc.EnabledForProduct);
|
Assert.IsTrue(foundDoc.EnabledForProduct[TestProduct.Product1]);
|
}
|
}
|
|
|
public class TestClass
|
{
|
public string Id { get; set; }
|
public Dictionary<TestProduct,bool> EnabledForProduct { get; set; }
|
}
|
|
|
public enum TestProduct
|
{
|
Product1,Product2
|
}
|
}
|
Attachments
Issue Links
- related to
-
CSHARP-2991 Regression of CSHARP-1521. ElemMatch requires IBsonArraySerializer for DictionaryInterfaceImplementerSerializer
-
- Closed
-