Skip to content

Commit c817496

Browse files
committed
change exception types and move check to its own method
1 parent 332cfca commit c817496

File tree

2 files changed

+15
-19
lines changed

2 files changed

+15
-19
lines changed

src/MongoDB.Bson/Serialization/BsonSerializerRegistry.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,8 @@ public void RegisterSerializer(Type type, IBsonSerializer serializer)
9393
{
9494
throw new ArgumentNullException("serializer");
9595
}
96-
EnsureRegisteringASerializerForThisTypeIsAllowed(serializer, type);
96+
EnsureRegisteringASerializerForThisTypeIsAllowed(type);
97+
EnsureSerializerIsCompatibleWithType(serializer, type);
9798

9899
if (!_cache.TryAdd(type, serializer))
99100
{
@@ -133,7 +134,8 @@ public bool TryRegisterSerializer(Type type, IBsonSerializer serializer)
133134
{
134135
throw new ArgumentNullException(nameof(serializer));
135136
}
136-
EnsureRegisteringASerializerForThisTypeIsAllowed(serializer, type);
137+
EnsureRegisteringASerializerForThisTypeIsAllowed(type);
138+
EnsureSerializerIsCompatibleWithType(serializer, type);
137139

138140
if (_cache.TryAdd(type, serializer))
139141
{
@@ -178,7 +180,7 @@ private IBsonSerializer CreateSerializer(Type type)
178180
throw new BsonSerializationException(message);
179181
}
180182

181-
private void EnsureRegisteringASerializerForThisTypeIsAllowed(IBsonSerializer serializer, Type type)
183+
private void EnsureRegisteringASerializerForThisTypeIsAllowed(Type type)
182184
{
183185
var typeInfo = type.GetTypeInfo();
184186
if (typeof(BsonValue).GetTypeInfo().IsAssignableFrom(type))
@@ -191,9 +193,13 @@ private void EnsureRegisteringASerializerForThisTypeIsAllowed(IBsonSerializer se
191193
var message = string.Format("Generic type {0} has unassigned type parameters.", BsonUtils.GetFriendlyTypeName(type));
192194
throw new ArgumentException(message, "type");
193195
}
196+
}
197+
198+
private void EnsureSerializerIsCompatibleWithType(IBsonSerializer serializer, Type type)
199+
{
194200
if (!serializer.ValueType.IsAssignableFrom(type))
195201
{
196-
throw new BsonSerializationException($"A serializer for {BsonUtils.GetFriendlyTypeName(serializer.ValueType)} cannot be registered for type {BsonUtils.GetFriendlyTypeName(type)}.");
202+
throw new ArgumentException($"A serializer for {BsonUtils.GetFriendlyTypeName(serializer.ValueType)} cannot be registered for type {BsonUtils.GetFriendlyTypeName(type)}.");
197203
}
198204
}
199205
}

tests/MongoDB.Bson.Tests/Serialization/BsonSerializerRegistryTests.cs

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,8 @@ public void RegisterSerializer_should_throw_when_type_and_serializer_do_not_matc
105105

106106
var exception = Record.Exception(() => subject.RegisterSerializer(typeof(long), intSerializer));
107107

108-
exception.Should().BeOfType<BsonSerializationException>();
109-
exception.Message.Should().Contain("A serializer for Int32 cannot be registered for type Int64.");
108+
exception.Should().BeOfType<ArgumentException>();
109+
exception.Message.Should().Be("A serializer for Int32 cannot be registered for type Int64.");
110110
}
111111

112112
[Fact]
@@ -117,8 +117,8 @@ public void TryRegisterSerializer_should_throw_when_type_and_serializer_do_not_m
117117

118118
var tryException = Record.Exception(() => subject.TryRegisterSerializer(typeof(long), intSerializer));
119119

120-
tryException.Should().BeOfType<BsonSerializationException>();
121-
tryException.Message.Should().Contain("A serializer for Int32 cannot be registered for type Int64.");
120+
tryException.Should().BeOfType<ArgumentException>();
121+
tryException.Message.Should().Be("A serializer for Int32 cannot be registered for type Int64.");
122122
}
123123

124124
[Fact]
@@ -153,17 +153,7 @@ private class Person
153153
private class PeopleSerializer : SerializerBase<IEnumerable<Person>>
154154
{
155155
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IEnumerable<Person> value)
156-
{
157-
context.Writer.WriteStartArray();
158-
foreach (var person in value)
159-
{
160-
context.Writer.WriteStartDocument();
161-
context.Writer.WriteName("Name");
162-
context.Writer.WriteString(person.Name);
163-
context.Writer.WriteEndDocument();
164-
}
165-
context.Writer.WriteEndArray();
166-
}
156+
=> throw new NotImplementedException();
167157

168158
public override IEnumerable<Person> Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
169159
=> throw new NotImplementedException();

0 commit comments

Comments
 (0)