From 3dbc90406cd10481b84e78f73a379fa112c847f5 Mon Sep 17 00:00:00 2001 From: Gian Maria Ricci Date: Fri, 14 Aug 2020 16:55:28 +0200 Subject: [PATCH 1/3] Fix: Better error when no constructor found --- .../Serialization/Serializers/BsonClassMapSerializer.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs b/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs index e2c7d6dfe6b..4e9df5081e4 100644 --- a/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs +++ b/src/MongoDB.Bson/Serialization/Serializers/BsonClassMapSerializer.cs @@ -414,7 +414,7 @@ private BsonCreatorMap ChooseBestCreator(Dictionary values) if (creatorMap == null) { - throw new BsonSerializationException("No matching creator found."); + throw new BsonSerializationException($"No matching creator found for class {_classMap.ClassType.FullName}."); } return creatorMap; From 53854aaf4669e8c5affca7c7ccc42f3ec20014ee Mon Sep 17 00:00:00 2001 From: Oleksandr Poliakov Date: Thu, 14 Mar 2024 11:30:45 -0700 Subject: [PATCH 2/3] Add unit test --- .../BsonClassMapSerializerTests.cs | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs index f04507eabdf..4b6a7c55cee 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs @@ -39,10 +39,42 @@ public void Deserialize_should_throw_invalidOperationException_when_creator_retu exception.Should().BeOfType(); } + [Fact] + public void Deserialize_should_throw_when_no_creators_found() + { + var bsonClassMap = new BsonClassMap(); + bsonClassMap.AutoMap(); + bsonClassMap.Freeze(); + + var subject = new BsonClassMapSerializer(bsonClassMap); + + using var reader = new JsonReader("{ \"_id\": \"just_an_id\" }"); + var context = BsonDeserializationContext.CreateRoot(reader); + + var exception = Record.Exception(() => subject.Deserialize(context)); + exception.Should().BeOfType() + .Subject.Message.Should().Be($"No matching creator found for class {typeof(ModelWithCtor).FullName}"); + } + // nested classes private class MyModel { public string Id { get; set; } } + + private class ModelWithCtor + { + private readonly string _myId; + private readonly int _i; + + public ModelWithCtor(string id, int i) + { + _myId = id; + _i = i; + } + + public string Id => _myId; + public int I => _i; + } } } From ea77ff21850ce820e2a284c4819f6b004c45a76c Mon Sep 17 00:00:00 2001 From: Oleksandr Poliakov <31327136+sanych-sun@users.noreply.github.com> Date: Thu, 14 Mar 2024 12:03:45 -0700 Subject: [PATCH 3/3] Update BsonClassMapSerializerTests.cs --- .../Serialization/BsonClassMapSerializerTests.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs index 4b6a7c55cee..a9b836302a1 100644 --- a/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs +++ b/tests/MongoDB.Bson.Tests/Serialization/BsonClassMapSerializerTests.cs @@ -53,7 +53,7 @@ public void Deserialize_should_throw_when_no_creators_found() var exception = Record.Exception(() => subject.Deserialize(context)); exception.Should().BeOfType() - .Subject.Message.Should().Be($"No matching creator found for class {typeof(ModelWithCtor).FullName}"); + .Subject.Message.Should().Be($"No matching creator found for class {typeof(ModelWithCtor).FullName}."); } // nested classes