From 2c362246003045ba532d4ae55169672d9c94cf99 Mon Sep 17 00:00:00 2001 From: Thomas Edward Kingstone Date: Tue, 18 Feb 2025 16:53:45 +0000 Subject: [PATCH] fixed problem with querying if a value can be set to property where the original value was not null --- Serialiser_Engine/Compute/Deserialise/IObject.cs | 6 +++--- Serialiser_Engine/Compute/Deserialise/Immutable.cs | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Serialiser_Engine/Compute/Deserialise/IObject.cs b/Serialiser_Engine/Compute/Deserialise/IObject.cs index 918b2dfff..92eb1e592 100644 --- a/Serialiser_Engine/Compute/Deserialise/IObject.cs +++ b/Serialiser_Engine/Compute/Deserialise/IObject.cs @@ -117,7 +117,7 @@ private static IObject SetProperties(this BsonDocument doc, Type type, IObject v { object propertyValue = item.Value.IDeserialise(prop.PropertyType, prop.GetValue(value), version, isUpgraded); - if (CanSetValueToProperty(prop.PropertyType, propertyValue)) + if (CanSetValueToProperty(prop.PropertyType, propertyValue, item.Value)) { prop.SetValue(value, propertyValue); } @@ -152,10 +152,10 @@ private static IObject SetProperties(this BsonDocument doc, Type type, IObject v /*******************************************/ - private static bool CanSetValueToProperty(Type propType, object value) + private static bool CanSetValueToProperty(Type propType, object value, BsonValue bsonValue) { if (value == null) - return !propType.IsValueType || Nullable.GetUnderlyingType(propType) != null; + return (!propType.IsValueType || Nullable.GetUnderlyingType(propType) != null) && (bsonValue == null || bsonValue.IsBsonNull); else return propType.IsAssignableFrom(value.GetType()); } diff --git a/Serialiser_Engine/Compute/Deserialise/Immutable.cs b/Serialiser_Engine/Compute/Deserialise/Immutable.cs index f3b77012d..0708a62a4 100644 --- a/Serialiser_Engine/Compute/Deserialise/Immutable.cs +++ b/Serialiser_Engine/Compute/Deserialise/Immutable.cs @@ -70,8 +70,10 @@ private static IObject DeserialiseImmutable(this BsonValue bson, Type targetType List arguments = new List(); foreach (var match in matches) { - object propertyValue = IDeserialise(match.Properties.First().Value, match.Parameter.ParameterType, null, version, isUpgraded); - if (CanSetValueToProperty(match.Parameter.ParameterType, propertyValue)) + BsonValue bsonValue = match.Properties.First().Value; + object propertyValue = IDeserialise(bsonValue, match.Parameter.ParameterType, null, version, isUpgraded); + + if (CanSetValueToProperty(match.Parameter.ParameterType, propertyValue, bsonValue)) arguments.Add(propertyValue); else {