Skip to content
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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

papafe
Copy link
Contributor

@papafe papafe commented Nov 5, 2024

@papafe papafe requested a review from a team as a code owner November 5, 2024 08:32
@papafe papafe requested review from JamesKovacs and rstam and removed request for a team and JamesKovacs November 5, 2024 08:32
@@ -25,7 +25,6 @@ namespace MongoDB.Bson.Serialization.Serializers
public class SerializerHelper
{
// private fields
private readonly long _allMemberFlags;
Copy link
Contributor Author

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)
Copy link
Contributor

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?

Copy link
Contributor Author

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 the DateOnlyDocumentFormat .HumanReadable format, while now they're serialized by default in the DateOnlyDocumentFormat.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?

Copy link
Contributor

Choose a reason for hiding this comment

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

Developers should ensure that their documents are all serialized in the same way in the database.

However, we almost always are willing to deserialize whatever we find. I don't see why deserializing whatever document format we find is any different than deserializing Double vs DateTime vs Document etc...

Maybe we need other people to chime in also.

new SerializerHelper.Member("DateTime", Flags.DateTime),
new SerializerHelper.Member("Ticks", Flags.Ticks)
);
if (_documentFormat is DateOnlyDocumentFormat.Classic)
Copy link
Contributor

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 ==

Copy link
Contributor Author

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);
Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Definitely

@papafe papafe requested a review from rstam November 25, 2024 17:01
@@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

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

Developers should ensure that their documents are all serialized in the same way in the database.

However, we almost always are willing to deserialize whatever we find. I don't see why deserializing whatever document format we find is any different than deserializing Double vs DateTime vs Document etc...

Maybe we need other people to chime in also.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants