-
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-5345: Unable to deserialize C# DateOnly objects #1534
base: main
Are you sure you want to change the base?
Conversation
@@ -25,7 +25,6 @@ namespace MongoDB.Bson.Serialization.Serializers | |||
public class SerializerHelper | |||
{ | |||
// private fields | |||
private readonly long _allMemberFlags; |
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 was assigned but not used
@@ -102,17 +140,37 @@ public override DateOnly Deserialize(BsonDeserializationContext context, BsonDes | |||
break; | |||
|
|||
case BsonType.Document: | |||
value = default; | |||
_helper.DeserializeMembers(context, (_, flag) => | |||
if (_documentFormat is DateOnlyDocumentFormat.Classic) |
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.
Do we want to be more forgiving and deserialize whatever we find instead of requiring that the stored documents match the configured format?
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.
I was also thinking about that and I'm not super sure of this either. My thought on this was:
- I suppose developers would prefer to have their
DateOnly
in one format in the database - If they were serializing
DateOnly
before, then they probably are in theDateOnlyDocumentFormat .HumanReadable
format, while now they're serialized by default in theDateOnlyDocumentFormat.Classic
format. - I think it would be better to raise an error as soon as possible, to make developers aware of these inconsistencies, before they realize it further down the road
What do you think?
new SerializerHelper.Member("DateTime", Flags.DateTime), | ||
new SerializerHelper.Member("Ticks", Flags.Ticks) | ||
); | ||
if (_documentFormat is DateOnlyDocumentFormat.Classic) |
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.
It really throws me off to see is
instead of ==
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.
I started doing that mostly because I think it is convenient to use this form when one must check if the enum is one of multiple values:
v is Enum.A or Enum.B
vs v == Enum.A || v == Enum.B
And from there I started using also when there is only value, for consistency, even though there is no particular advantage here.
Of course I can put ==
if we prefer.
return this; | ||
} | ||
|
||
return new DateOnlySerializer(representation, documentFormat); |
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.
Probably should include _documentFormat
in line 291 below.
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.
Definitely
JIRA ticket