Skip to content

CSHARP-2150 Add check that the serializer's ValueType matches the type when registering the serializer#1929

Merged
kyra-rk merged 8 commits intomongodb:mainfrom
kyra-rk:csharp2150
Apr 8, 2026
Merged

CSHARP-2150 Add check that the serializer's ValueType matches the type when registering the serializer#1929
kyra-rk merged 8 commits intomongodb:mainfrom
kyra-rk:csharp2150

Conversation

@kyra-rk
Copy link
Copy Markdown
Contributor

@kyra-rk kyra-rk commented Mar 27, 2026

RegisterSerializer and TryRegisterSerializer now throw an ArgumentException if the serializer's ValueType is not compatible with the registered type (e.g. registering an Int32Serializer for typeof(long)). Serializers registered for a base type remain valid for derived types.

@kyra-rk kyra-rk added the improvement Optimizations or refactoring (no new features or fixes). label Mar 27, 2026
@kyra-rk kyra-rk requested a review from papafe March 27, 2026 20:10
@kyra-rk kyra-rk marked this pull request as ready for review March 30, 2026 15:22
@kyra-rk kyra-rk requested a review from a team as a code owner March 30, 2026 15:22
@kyra-rk kyra-rk requested a review from papafe March 31, 2026 15:29
@kyra-rk
Copy link
Copy Markdown
Contributor Author

kyra-rk commented Mar 31, 2026

After our standup discussion, I checked whether a serializer registered for a base type works correctly for a derived type — it does (see new test added), so I changed the type equality check to IsAssignableFrom. This relaxes the validation to reflect the actual behavior of serialize/deserialize.

@kyra-rk kyra-rk added the breaking change PR contains some breaking changes. label Mar 31, 2026
Copy link
Copy Markdown
Contributor

@papafe papafe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall it looks good, some minor comments.

{
public override void Serialize(BsonSerializationContext context, BsonSerializationArgs args, IEnumerable<Person> value)
{
context.Writer.WriteStartArray();
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We're not actually using this implementation, just throw a NotImplementedException.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice catch! Will fix

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

tryException.Should().BeOfType<BsonSerializationException>();
tryException.Message.Should().Contain("A serializer for Int32 cannot be registered for type Int64.");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you use the full string comparison it should use Be instead of Contain

Copy link
Copy Markdown
Contributor

@papafe papafe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Just one minor thing

subject.GetSerializer(typeof(List<Person>)).Should().BeSameAs(serializer);
}

private class Person
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for noticing before, usually we keep utility class and other utility methods after all the test methods, so at the end of the whole test class.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No worries, will change it!

@kyra-rk kyra-rk merged commit 1778ee6 into mongodb:main Apr 8, 2026
33 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking change PR contains some breaking changes. improvement Optimizations or refactoring (no new features or fixes).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants