Skip to content
Open
  •  
  •  
  •  
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ public override TESTID Parse(object value)
return value switch
{
string stringValue when !string.IsNullOrEmpty(stringValue) => new TESTID(new MongoDB.Bson.ObjectId(stringValue)),
string stringValue when string.IsNullOrEmpty(stringValue) => TESTID.Empty,
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to TESTID"),
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ public class EfCoreValueConverter : Microsoft.EntityFrameworkCore.Storage.ValueC
public EfCoreValueConverter() : this(null) { }
public EfCoreValueConverter(Microsoft.EntityFrameworkCore.Storage.ValueConversion.ConverterMappingHints mappingHints = null)
: base(
id => id.Value == MongoDB.Bson.ObjectId.Empty ? null : id.Value.ToString(),
value => string.IsNullOrEmpty(value) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(value)),
id => id.Value.ToString(),
value => new TESTID(new MongoDB.Bson.ObjectId(value)),
mappingHints
) { }
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,11 @@
{
public override TESTID Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
{
if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId)
{
return new TESTID(context.Reader.ReadObjectId());
}
else
{
context.Reader.SkipValue();
return TESTID.Empty;
}
return new TESTID(context.Reader.ReadObjectId());
}

public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, TESTID value)
{
if (value.Value == MongoDB.Bson.ObjectId.Empty)
{
context.Writer.WriteNull();
}
else
{
context.Writer.WriteObjectId(value.Value);
}
context.Writer.WriteObjectId(value.Value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,13 @@ public override bool CanConvert(System.Type objectType)

public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
if (value is TESTID id && id.Value != MongoDB.Bson.ObjectId.Empty)
{
serializer.Serialize(writer, id.Value.ToString());
}
else
{
serializer.Serialize(writer, null);
}
var id = (TESTID)value;
serializer.Serialize(writer, id.Value.ToString());
}

public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var result = serializer.Deserialize<string>(reader);
return string.IsNullOrEmpty(result) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(result));
return new TESTID(new MongoDB.Bson.ObjectId(result));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ class TESTIDSystemTextJsonConverter : System.Text.Json.Serialization.JsonConvert
{
public override TESTID Read(ref System.Text.Json.Utf8JsonReader reader, System.Type typeToConvert, System.Text.Json.JsonSerializerOptions options)
{
var result = reader.GetString();
return string.IsNullOrEmpty(result) ? TESTID.Empty : new TESTID(new MongoDB.Bson.ObjectId(result));
return new TESTID(new MongoDB.Bson.ObjectId(reader.GetString()));
}

public override void Write(System.Text.Json.Utf8JsonWriter writer, TESTID value, System.Text.Json.JsonSerializerOptions options)
Expand Down
23 changes: 3 additions & 20 deletions test/StronglyTypedIds.IntegrationTests/ObjectIdTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public ObjectIdTests(MongoDbFixture mongoDbFixture)
{
_mongoDbFixture = mongoDbFixture;
}

[Fact]
public void SameValuesAreEqual()
{
Expand Down Expand Up @@ -95,18 +95,6 @@ public void CanSerializeToObjectId_WithTypeConverter()

Assert.Equal(serializedFoo, serializedObjectId);
}

[Fact]
public void CanSerializeToNullable_WithNewtonsoftJsonProvider()
{
var entity = new EntityWithNullableId { Id = null };

var json = NewtonsoftJsonSerializer.SerializeObject(entity);
var deserialize = NewtonsoftJsonSerializer.DeserializeObject<EntityWithNullableId>(json);

Assert.NotNull(deserialize);
Assert.Equal(deserialize.Id, NewtonsoftJsonObjectIdId.Empty);
}
Copy link
Owner

Choose a reason for hiding this comment

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

I think this should still be something we support shouldn't it? 🤔


[Fact]
public void CanSerializeToObjectId_WithSystemTextJsonProvider()
Expand Down Expand Up @@ -231,7 +219,7 @@ public async Task WhenDapperValueConverterUsesValueConverter()
var value = Assert.Single(results);
Assert.Equal(new DapperObjectIdId(new ObjectId("62758c6ee39f6ef4751bb831")), value);
}

[Fact]
public async Task WhenMongoSerializerUsesSerializer()
{
Expand All @@ -241,7 +229,7 @@ public async Task WhenMongoSerializerUsesSerializer()
var original = new TestDocument { Id = new MongoObjectIdId(objectIdValue) };
await collection.InsertOneAsync(original);
var retrieved = await collection.Find(x => x.Id == new MongoObjectIdId(objectIdValue)).FirstAsync();

Assert.Equal(new MongoObjectIdId(objectIdValue), retrieved.Id);
}

Expand Down Expand Up @@ -375,11 +363,6 @@ public class TestEntity
{
public EfCoreObjectIdId Id { get; set; }
}

public class EntityWithNullableId
{
public NewtonsoftJsonObjectIdId? Id { get; set; }
}

public class TestDocument
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the StronglyTypedId source generator
//
Expand Down Expand Up @@ -50,7 +50,6 @@ namespace Some.Namespace
return value switch
{
string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty,
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
};
}
Expand All @@ -65,47 +64,26 @@ namespace Some.Namespace

public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty)
{
serializer.Serialize(writer, id.Value.ToString());
}
else
{
serializer.Serialize(writer, null);
}
var id = (MyTestId)value;
serializer.Serialize(writer, id.Value.ToString());
}

public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var result = serializer.Deserialize<string>(reader);
return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result));
return new MyTestId(new MongoDB.Bson.ObjectId(result));
}
}
class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<MyTestId>
{
public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
{
if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId)
{
return new MyTestId(context.Reader.ReadObjectId());
}
else
{
context.Reader.SkipValue();
return MyTestId.Empty;
}
return new MyTestId(context.Reader.ReadObjectId());
}

public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
{
if (value.Value == MongoDB.Bson.ObjectId.Empty)
{
context.Writer.WriteNull();
}
else
{
context.Writer.WriteObjectId(value.Value);
}
context.Writer.WriteObjectId(value.Value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the StronglyTypedId source generator
//
Expand Down Expand Up @@ -50,7 +50,6 @@ namespace Some.Namespace
return value switch
{
string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty,
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
};
}
Expand All @@ -65,47 +64,26 @@ namespace Some.Namespace

public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty)
{
serializer.Serialize(writer, id.Value.ToString());
}
else
{
serializer.Serialize(writer, null);
}
var id = (MyTestId)value;
serializer.Serialize(writer, id.Value.ToString());
}

public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var result = serializer.Deserialize<string>(reader);
return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result));
return new MyTestId(new MongoDB.Bson.ObjectId(result));
}
}
class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<MyTestId>
{
public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
{
if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId)
{
return new MyTestId(context.Reader.ReadObjectId());
}
else
{
context.Reader.SkipValue();
return MyTestId.Empty;
}
return new MyTestId(context.Reader.ReadObjectId());
}

public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
{
if (value.Value == MongoDB.Bson.ObjectId.Empty)
{
context.Writer.WriteNull();
}
else
{
context.Writer.WriteObjectId(value.Value);
}
context.Writer.WriteObjectId(value.Value);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by the StronglyTypedId source generator
//
Expand Down Expand Up @@ -51,7 +51,6 @@ namespace Some.Namespace
return value switch
{
string stringValue when !string.IsNullOrEmpty(stringValue) => new MyTestId(new MongoDB.Bson.ObjectId(stringValue)),
string stringValue when string.IsNullOrEmpty(stringValue) => MyTestId.Empty,
_ => throw new System.InvalidCastException($"Unable to cast object of type {value.GetType()} to MyTestId"),
};
}
Expand All @@ -66,47 +65,26 @@ namespace Some.Namespace

public override void WriteJson(Newtonsoft.Json.JsonWriter writer, object value, Newtonsoft.Json.JsonSerializer serializer)
{
if (value is MyTestId id && id.Value != MongoDB.Bson.ObjectId.Empty)
{
serializer.Serialize(writer, id.Value.ToString());
}
else
{
serializer.Serialize(writer, null);
}
var id = (MyTestId)value;
serializer.Serialize(writer, id.Value.ToString());
}

public override object ReadJson(Newtonsoft.Json.JsonReader reader, System.Type objectType, object existingValue, Newtonsoft.Json.JsonSerializer serializer)
{
var result = serializer.Deserialize<string>(reader);
return string.IsNullOrEmpty(result) ? MyTestId.Empty : new MyTestId(new MongoDB.Bson.ObjectId(result));
return new MyTestId(new MongoDB.Bson.ObjectId(result));
}
}
class MyTestIdMongoSerializer : MongoDB.Bson.Serialization.Serializers.SerializerBase<MyTestId>
{
public override MyTestId Deserialize(MongoDB.Bson.Serialization.BsonDeserializationContext context, MongoDB.Bson.Serialization.BsonDeserializationArgs args)
{
if (context.Reader.GetCurrentBsonType() == MongoDB.Bson.BsonType.ObjectId)
{
return new MyTestId(context.Reader.ReadObjectId());
}
else
{
context.Reader.SkipValue();
return MyTestId.Empty;
}
return new MyTestId(context.Reader.ReadObjectId());
}

public override void Serialize(MongoDB.Bson.Serialization.BsonSerializationContext context, MongoDB.Bson.Serialization.BsonSerializationArgs args, MyTestId value)
{
if (value.Value == MongoDB.Bson.ObjectId.Empty)
{
context.Writer.WriteNull();
}
else
{
context.Writer.WriteObjectId(value.Value);
}
context.Writer.WriteObjectId(value.Value);
}
}
}
Expand Down
Loading