-
Notifications
You must be signed in to change notification settings - Fork 1.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CSHARP-2096: Make EnumRepresentationConvention also affect collection… #1535
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,8 +14,7 @@ | |
*/ | ||
|
||
using System; | ||
using System.Reflection; | ||
using MongoDB.Bson.Serialization.Options; | ||
using MongoDB.Bson.Serialization.Serializers; | ||
|
||
namespace MongoDB.Bson.Serialization.Conventions | ||
{ | ||
|
@@ -50,60 +49,46 @@ public EnumRepresentationConvention(BsonType representation) | |
/// <param name="memberMap">The member map.</param> | ||
public void Apply(BsonMemberMap memberMap) | ||
{ | ||
var memberType = memberMap.MemberType; | ||
var memberTypeInfo = memberType.GetTypeInfo(); | ||
var serializer = memberMap.GetSerializer(); | ||
|
||
if (memberTypeInfo.IsEnum) | ||
if (memberMap.MemberType.IsEnum && serializer is IRepresentationConfigurable representationConfigurableSerializer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This part here of the code could be removed, as it is a special case of I've removed the "nullable enum" case though, but we can decide it needs to have the same treatment as this. |
||
{ | ||
var serializer = memberMap.GetSerializer(); | ||
var representationConfigurableSerializer = serializer as IRepresentationConfigurable; | ||
if (representationConfigurableSerializer != null) | ||
{ | ||
var reconfiguredSerializer = representationConfigurableSerializer.WithRepresentation(_representation); | ||
memberMap.SetSerializer(reconfiguredSerializer); | ||
} | ||
memberMap.SetSerializer(representationConfigurableSerializer.WithRepresentation(_representation)); | ||
return; | ||
} | ||
|
||
if (IsNullableEnum(memberType)) | ||
var reconfiguredSerializer = Reconfigure(serializer); | ||
if (reconfiguredSerializer is not null) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you prefer There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think sometimes it reads a little bit better, but in this case it probably does not make much of a difference. So I can definitely use |
||
{ | ||
var serializer = memberMap.GetSerializer(); | ||
var childSerializerConfigurableSerializer = serializer as IChildSerializerConfigurable; | ||
if (childSerializerConfigurableSerializer != null) | ||
{ | ||
var childSerializer = childSerializerConfigurableSerializer.ChildSerializer; | ||
var representationConfigurableChildSerializer = childSerializer as IRepresentationConfigurable; | ||
if (representationConfigurableChildSerializer != null) | ||
{ | ||
var reconfiguredChildSerializer = representationConfigurableChildSerializer.WithRepresentation(_representation); | ||
var reconfiguredSerializer = childSerializerConfigurableSerializer.WithChildSerializer(reconfiguredChildSerializer); | ||
memberMap.SetSerializer(reconfiguredSerializer); | ||
} | ||
} | ||
return; | ||
memberMap.SetSerializer(reconfiguredSerializer); | ||
} | ||
} | ||
|
||
// private methods | ||
private bool IsNullableEnum(Type type) | ||
private IBsonSerializer Reconfigure(IBsonSerializer serializer) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This method here is recursively trying to fin an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Searching for an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Exactly, and this is also why I think it would make sense to do this for next major version, as this is effectively a behaviour breaking change. What do you think? |
||
{ | ||
return | ||
type.GetTypeInfo().IsGenericType && | ||
type.GetGenericTypeDefinition() == typeof(Nullable<>) && | ||
Nullable.GetUnderlyingType(type).GetTypeInfo().IsEnum; | ||
if (serializer is IChildSerializerConfigurable childSerializerConfigurable) | ||
{ | ||
var childSerializer = childSerializerConfigurable.ChildSerializer; | ||
var reconfiguredChildSerializer = Reconfigure(childSerializer); | ||
return reconfiguredChildSerializer is null ? null : childSerializerConfigurable.WithChildSerializer(reconfiguredChildSerializer); | ||
} | ||
|
||
if (serializer.ValueType.IsEnum && serializer is IRepresentationConfigurable representationConfigurable) | ||
{ | ||
return representationConfigurable.WithRepresentation(_representation); | ||
} | ||
|
||
return null; | ||
} | ||
|
||
// private methods | ||
private void EnsureRepresentationIsValidForEnums(BsonType representation) | ||
{ | ||
if ( | ||
representation == 0 || | ||
representation == BsonType.String || | ||
representation == BsonType.Int32 || | ||
representation == BsonType.Int64) | ||
if (representation is 0 or BsonType.String or BsonType.Int32 or BsonType.Int64) | ||
{ | ||
return; | ||
} | ||
throw new ArgumentException("Enums can only be represented as String, Int32, Int64 or the type of the enum", "representation"); | ||
throw new ArgumentException("Enums can only be represented as String, Int32, Int64 or the type of the enum", nameof(representation)); | ||
} | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is effectively a beahviour breaking change. Should we keep this for next major version?