Skip to content

Commit 80c9ad3

Browse files
committed
Small corrections
1 parent f4f705c commit 80c9ad3

File tree

3 files changed

+24
-21
lines changed

3 files changed

+24
-21
lines changed

src/MongoDB.Bson/Serialization/IBsonSerializerExtensions.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ serializer is IHasDiscriminatorConvention hasDiscriminatorConvention
7070
/// <param name="shouldReconfigure">
7171
/// An optional predicate to determine if the reconfiguration should be applied to the current serializer.
7272
/// </param>
73-
/// <typeparam name="T">The specific type of serializer to be reconfigured.</typeparam>
73+
/// <typeparam name="T">The input type for the reconfigure method.</typeparam>
7474
/// <returns>
7575
/// The reconfigured serializer, or <c>null</c> if no leaf serializer could be reconfigured.
7676
/// </returns>

src/MongoDB.Bson/Serialization/Serializers/DateOnlySerializer.cs

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -117,15 +117,13 @@ public DateOnlySerializer(BsonType representation, DateOnlyDocumentFormat docume
117117
public override DateOnly Deserialize(BsonDeserializationContext context, BsonDeserializationArgs args)
118118
{
119119
var bsonReader = context.Reader;
120-
DateOnly value;
121120

122121
var bsonType = bsonReader.GetCurrentBsonType();
123122

124123
switch (bsonType)
125124
{
126125
case BsonType.DateTime:
127-
value = VerifyAndMakeDateOnly(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()));
128-
break;
126+
return VerifyAndMakeDateOnly(BsonUtils.ToDateTimeFromMillisecondsSinceEpoch(bsonReader.ReadDateTime()));
129127

130128
case BsonType.Document:
131129
var ticks = 0L;
@@ -153,30 +151,24 @@ public override DateOnly Deserialize(BsonDeserializationContext context, BsonDes
153151
};
154152

155153
case BsonType.Decimal128:
156-
value = VerifyAndMakeDateOnly(new DateTime(_converter.ToInt64(bsonReader.ReadDecimal128()), DateTimeKind.Utc));
157-
break;
154+
return VerifyAndMakeDateOnly(new DateTime(_converter.ToInt64(bsonReader.ReadDecimal128()), DateTimeKind.Utc));
158155

159156
case BsonType.Double:
160-
value = VerifyAndMakeDateOnly(new DateTime(_converter.ToInt64(bsonReader.ReadDouble()), DateTimeKind.Utc));
161-
break;
157+
return VerifyAndMakeDateOnly(new DateTime(_converter.ToInt64(bsonReader.ReadDouble()), DateTimeKind.Utc));
162158

163159
case BsonType.Int32:
164-
value = VerifyAndMakeDateOnly(new DateTime(bsonReader.ReadInt32(), DateTimeKind.Utc));
165-
break;
160+
return VerifyAndMakeDateOnly(new DateTime(bsonReader.ReadInt32(), DateTimeKind.Utc));
166161

167162
case BsonType.Int64:
168-
value = VerifyAndMakeDateOnly(new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc));
169-
break;
163+
return VerifyAndMakeDateOnly(new DateTime(bsonReader.ReadInt64(), DateTimeKind.Utc));
170164

171165
case BsonType.String:
172-
value = DateOnly.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd");
173-
break;
166+
return DateOnly.ParseExact(bsonReader.ReadString(), "yyyy-MM-dd");
174167

175168
default:
176169
throw CreateCannotDeserializeFromBsonTypeException(bsonType);
177170
}
178171

179-
return value;
180172

181173
DateOnly VerifyAndMakeDateOnly(DateTime dt)
182174
{

tests/MongoDB.Bson.Tests/Serialization/Serializers/DateOnlySerializerTests.cs

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -232,17 +232,28 @@ public void Equals_with_equal_fields_should_return_true()
232232
}
233233

234234
[Theory]
235-
[InlineData(BsonType.String)]
236-
[InlineData(BsonType.Int64)]
237-
[InlineData(BsonType.Document)]
238-
public void Equals_with_not_equal_fields_should_return_true(BsonType representation)
235+
[ParameterAttributeData]
236+
public void Equals_with_different_representation_and_format_should_return_correct(
237+
[Values(BsonType.DateTime, BsonType.String, BsonType.Int64, BsonType.Document)]
238+
BsonType representation,
239+
[Values(DateOnlyDocumentFormat.DateTimeTicks, DateOnlyDocumentFormat.YearMonthDay)]
240+
DateOnlyDocumentFormat format)
239241
{
240242
var x = new DateOnlySerializer();
241-
var y = new DateOnlySerializer(representation);
243+
var y = new DateOnlySerializer(representation, format);
242244

243245
var result = x.Equals(y);
246+
var result2 = y.Equals(x);
247+
result.Should().Be(result2);
244248

245-
result.Should().Be(false);
249+
if (representation == BsonType.DateTime && format == DateOnlyDocumentFormat.DateTimeTicks)
250+
{
251+
result.Should().Be(true);
252+
}
253+
else
254+
{
255+
result.Should().Be(false);
256+
}
246257
}
247258

248259
[Fact]

0 commit comments

Comments
 (0)