From cd162c59778ab963ac69f19294fe4356880642f1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=A3o=20Ant=C3=B4nio=20Duarte?= Date: Fri, 13 Mar 2020 11:58:04 -0300 Subject: [PATCH] Exception handling when receiving value that does not exist in the mapped values. Search for mapped values case insensitive --- sources/MVCFramework.Serializer.Commons.pas | 10 ++++++++-- sources/MVCFramework.Serializer.JsonDataObjects.pas | 3 +++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/sources/MVCFramework.Serializer.Commons.pas b/sources/MVCFramework.Serializer.Commons.pas index f78d3b1f..81dd7f1a 100644 --- a/sources/MVCFramework.Serializer.Commons.pas +++ b/sources/MVCFramework.Serializer.Commons.pas @@ -314,7 +314,8 @@ uses MVCFramework.Serializer.JsonDataObjects, MVCFramework.Serializer.Intf, Data.FmtBcd, - MVCFramework.Nullables; + MVCFramework.Nullables, + System.Generics.Defaults; function NewObjectHolder(const AObject: TObject; const AMetaFiller: TProc = nil; const AOwns: boolean = false): TMVCObjectResponse; @@ -732,7 +733,12 @@ end; constructor MVCEnumSerializationAttribute.Create(const ASerializationType: TMVCEnumSerializationType; const AMappedValues: string); begin - FMappedValues := TList.Create; + FMappedValues := TList.Create(TDelegatedComparer.Create( + function(const Left, Right: string): Integer + begin + Result := CompareText(Left, Right); + end)); + FSerializationType := ASerializationType; if (FSerializationType = estEnumMappedValues) then diff --git a/sources/MVCFramework.Serializer.JsonDataObjects.pas b/sources/MVCFramework.Serializer.JsonDataObjects.pas index 330c00b2..b6cdfd8c 100644 --- a/sources/MVCFramework.Serializer.JsonDataObjects.pas +++ b/sources/MVCFramework.Serializer.JsonDataObjects.pas @@ -936,6 +936,9 @@ begin else begin LMappedValueIndex := LEnumMappedValues.IndexOf(AJsonObject[AName].Value); + if LMappedValueIndex < 0 then + raise EMVCSerializationException.CreateFmt('Cannot deserialize property %s from mapped values', [AName]); + TValue.Make(GetEnumValue(AValue.TypeInfo, GetEnumName(AValue.TypeInfo, LMappedValueIndex)), AValue.TypeInfo, AValue) end;