Skip to content

Serialiser_Engine: Added condition that the serialised value must be null if the deserialised result was null. #3463

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

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions Serialiser_Engine/Compute/Deserialise/IObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,67 +71,67 @@

private static IObject SetProperties(this BsonDocument doc, Type type, IObject value, string version, bool isUpgraded)
{
try
{
foreach (BsonElement item in doc)
{
if (item.Name.StartsWith("_"))
continue;

PropertyInfo prop = type.GetProperty(item.Name, BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

if (prop == null)
{
if (!isUpgraded)
{
if (TryUpgrade(doc, version, out IObject upgraded))
{
return upgraded;
}
}

if (value is IBHoMObject)
{
BH.Engine.Base.Compute.RecordWarning($"Unable to find a property named {item.Name}. Data stored in CustomData of the {type.Name}.");
((IBHoMObject)value).CustomData[item.Name] = item.Value.IDeserialise(version, isUpgraded);
}
else
{
BH.Engine.Base.Compute.RecordError($"Unable to find a property named {item.Name} on object of type {type.Name}. CustomObject returned in its place.");
return DeserialiseDeprecatedCustomObject(doc, version, false);
}

Check warning on line 102 in Serialiser_Engine/Compute/Deserialise/IObject.cs

View check run for this annotation

BHoMBot-CI / code-compliance

Serialiser_Engine/Compute/Deserialise/IObject.cs#L93-L102

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData

}
else if (item.Name == "CustomData" && value is IBHoMObject)
((IBHoMObject)value).CustomData = item.Value.DeserialiseDictionary(((IBHoMObject)value).CustomData, version, isUpgraded);
else
{
if (!prop.CanWrite)
{
if (!(value is IImmutable))
{
Base.Compute.RecordError("Property is not settable.");
}
}
else
{
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);
}
else if (!isUpgraded)
{
return DeserialiseDeprecate(doc, version, isUpgraded) as IObject;
}
else
{
Base.Compute.RecordError($"Unable to set property {item.Name} to object of type {value?.GetType().Name ?? "uknown type"} due to a type missmatch. Expected {prop.PropertyType.Name} but serialised value was {propertyValue?.GetType().Name ?? "null"}.");
return DeserialiseDeprecatedCustomObject(doc, version, false);
}
}
}

Check warning on line 134 in Serialiser_Engine/Compute/Deserialise/IObject.cs

View check run for this annotation

BHoMBot-CI / code-compliance

Serialiser_Engine/Compute/Deserialise/IObject.cs#L83-L134

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData

Check warning on line 134 in Serialiser_Engine/Compute/Deserialise/IObject.cs

View check run for this annotation

BHoMBot-CI / code-compliance

Serialiser_Engine/Compute/Deserialise/IObject.cs#L105-L134

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData
}

Check warning on line 135 in Serialiser_Engine/Compute/Deserialise/IObject.cs

View check run for this annotation

BHoMBot-CI / code-compliance

Serialiser_Engine/Compute/Deserialise/IObject.cs#L76-L135

The use of CustomData within the code is discouraged except in circumstances where volatile data is being used. - For more information see https://bhom.xyz/documentation/DevOps/Code%20Compliance%20and%20CI/Compliance%20Checks/IsUsingCustomData

return value;
Expand All @@ -152,10 +152,10 @@

/*******************************************/

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());
}
Expand Down
6 changes: 4 additions & 2 deletions Serialiser_Engine/Compute/Deserialise/Immutable.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,10 @@ private static IObject DeserialiseImmutable(this BsonValue bson, Type targetType
List<object> arguments = new List<object>();
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
{
Expand Down
Loading