From 048e6f17f74d8aae13cd9e921f9bf8ebae9d5bb0 Mon Sep 17 00:00:00 2001 From: Daniele Teti Date: Sun, 22 Dec 2024 18:27:17 +0100 Subject: [PATCH] https://github.com/danieleteti/delphimvcframework/issues/792 --- ...VCFramework.Serializer.JsonDataObjects.pas | 5 +++ .../Serializers.JsonDataObjectsTestU.pas | 39 +++++++++++++++++++ 2 files changed, 44 insertions(+) diff --git a/sources/MVCFramework.Serializer.JsonDataObjects.pas b/sources/MVCFramework.Serializer.JsonDataObjects.pas index acac02de..0031a65f 100644 --- a/sources/MVCFramework.Serializer.JsonDataObjects.pas +++ b/sources/MVCFramework.Serializer.JsonDataObjects.pas @@ -1510,6 +1510,11 @@ procedure TMVCJsonDataObjectsSerializer.JSONObjectPropertyToTValue(AJSONObject: LClazz: TClass; lValueTypeInfo: PTypeInfo; begin + if AJSONObject = nil then + begin + Exit; + end; + case AJSONObject[APropertyName].Typ of jdtNone: Exit; diff --git a/unittests/general/Several/Serializers.JsonDataObjectsTestU.pas b/unittests/general/Several/Serializers.JsonDataObjectsTestU.pas index 44f07a12..6d36b033 100644 --- a/unittests/general/Several/Serializers.JsonDataObjectsTestU.pas +++ b/unittests/general/Several/Serializers.JsonDataObjectsTestU.pas @@ -175,6 +175,10 @@ TMVCTestSerializerJsonDataObjects = class(TObject) [Test] [Category('serializers')] procedure TestDeserializeOwnedProperty_WithPropertyUnassigned_JSONExists_Polimorphic; + + [Test] + [Category('issues')] + procedure TestIssue792; end; TMVCEntityCustomSerializerJsonDataObjects = class(TInterfacedObject, IMVCTypeSerializer) @@ -216,6 +220,15 @@ TMVCNullableIntegerSerializerJsonDataObjects = class(TInterfacedObject, IMVCTy /// TNestedGenericEntity = TGenericEntity>; + TMyObj = class + private + fName: string; + fNumber: integer; + public + property Name: string read FName write FName; + property Number: integer read FNumber write FNumber; + end; + implementation uses @@ -917,6 +930,32 @@ procedure TMVCTestSerializerJsonDataObjects.TestDoNotSerializeDoNotDeSerialize; end; +procedure TMVCTestSerializerJsonDataObjects.TestIssue792; +var + lMyObj: TMyObj; + lSer: IMVCSerializer; +begin + lMyObj := TMyObj.Create; + try + lMyObj.Name := 'will be changed'; + lSer := TMVCJsonDataObjectsSerializer.Create(); + lSer.DeserializeObject('{ "dataobject" : { "name" : "Daniele", "number" : 123 }}', lMyObj, stDefault, nil, 'dataobject'); + Assert.Contains(lSer.SerializeObject(lMyObj), 'Daniele'); + finally + lMyObj.Free; + end; + + lMyObj := TMyObj.Create; + try + lSer := TMVCJsonDataObjectsSerializer.Create(); + lMyObj.Name := 'the untouchable'; + lSer.DeserializeObject('{ "dataobject" : null}', lMyObj, stDefault, nil, 'dataobject'); + Assert.Contains(lSer.SerializeObject(lMyObj), 'the untouchable'); + finally + lMyObj.Free; + end; +end; + procedure TMVCTestSerializerJsonDataObjects.TestSerializeAllNullableTypes; var lObj1, lObj2: BusinessObjectsU.TNullablesTest;