Skip to content

Commit e0ccc15

Browse files
DateTime deserialization in REST ignored the client’s timezone (#1118)
* Add Localize method to SDTs to convert UTC DateTimes to the context’s time zone after deserialization * Add LoadCollection method * Set Context for InternalModel * Fix LoadCollection definition * FromStringCollection was not working for DateTimes. * Fix FromStringCollection * Log badrequest errors. * Avoid type conversion for ToStringCollection of a GxSimpleCollection<string> * Add CToT2 and TToC2 for datetime collections * Rename LoadCollection to ToInternalModel * The type arguments for method 'GxRestService.ToInternalModel<T>(GxGenericCollectionItem<T>)' cannot be inferred from the usage. * FromStringCollection with context can be deleted now (httpcontext current is accessible in.net core) * Remove unused Localize method and rename ToInternalModel to LoadSdt for consistency with LoadCollection. * Add missing closing curly brace.
1 parent 2b5adc3 commit e0ccc15

File tree

6 files changed

+115
-6
lines changed

6 files changed

+115
-6
lines changed

dotnet/src/dotnetcore/GxClasses.Web/Middleware/GXRestServices.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,38 @@ internal void Initialize()
8686
context.HttpContext.NewSessionCheck();
8787
ServiceHeaders();
8888
}
89+
//Renamed to LoadSdt
90+
protected T ToInternalModel<T>(GxGenericCollectionItem<T> restSDT) where T : GxUserType, new()
91+
{
92+
return LoadSdt<T>(restSDT);
93+
}
94+
protected T LoadSdt<T>(GxGenericCollectionItem<T> restSDT) where T : GxUserType, new()
95+
{
96+
if (restSDT != null)
97+
{
98+
restSDT.Sdt.context = context;
99+
return restSDT.InternalSdt;
100+
}
101+
else
102+
{
103+
T internalSDT = new T();
104+
internalSDT.context = context;
105+
return internalSDT;
106+
}
107+
}
89108

109+
protected void LoadCollection<X, T>(GxGenericCollection<X> restModel, GXBaseCollection<T> internalModel) where T : GxUserType, new()
110+
where X : new()
111+
{
112+
if (restModel != null)
113+
{
114+
restModel.LoadCollection(internalModel);
115+
foreach (GxUserType item in internalModel)
116+
{
117+
item.context = context;
118+
}
119+
}
120+
}
90121
protected void Cleanup()
91122
{
92123
if (runAsMain)

dotnet/src/dotnetcore/GxNetCoreStartup/Startup.cs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
using Microsoft.AspNetCore.Http.Features;
2323
using Microsoft.AspNetCore.Mvc;
2424
using Microsoft.AspNetCore.Mvc.ApplicationModels;
25+
using Microsoft.AspNetCore.Mvc.ModelBinding;
2526
using Microsoft.AspNetCore.Mvc.Routing;
2627
using Microsoft.AspNetCore.Rewrite;
2728
using Microsoft.AspNetCore.Routing;
@@ -124,11 +125,29 @@ public static IApplicationBuilder MapWebSocketManager(this IApplicationBuilder a
124125
}
125126
public class CustomBadRequestObjectResult : ObjectResult
126127
{
128+
static readonly IGXLogger log = GXLoggerFactory.GetLogger(typeof(CustomBadRequestObjectResult).FullName);
127129
public CustomBadRequestObjectResult(ActionContext context)
128130
: base(HttpHelper.GetJsonError(StatusCodes.Status400BadRequest.ToString(), HttpHelper.StatusCodeToTitle(HttpStatusCode.BadRequest)))
129131
{
132+
LogErrorResponse(context);
130133
StatusCode = StatusCodes.Status400BadRequest;
131134
}
135+
static void LogErrorResponse(ActionContext context)
136+
{
137+
if (log.IsErrorEnabled)
138+
{
139+
foreach (KeyValuePair<string, ModelStateEntry> entry in context.ModelState)
140+
{
141+
if (entry.Value.Errors.Count > 0)
142+
{
143+
foreach (ModelError error in entry.Value.Errors)
144+
{
145+
GXLogging.Error(log, "Field ", entry.Key, "Errors:", error.ErrorMessage);
146+
}
147+
}
148+
}
149+
}
150+
}
132151
}
133152

134153
public class Startup

dotnet/src/dotnetframework/GxClasses/Core/GXUtilsCommon.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,6 +2850,15 @@ internal static DateTime CToDT2(string jsonDate, IGxContext context) {
28502850
return CToD2(jsonDate);
28512851
}
28522852
}
2853+
public static GxSimpleCollection<DateTime> CToT2(GxSimpleCollection<string> stCollection, IGxContext context)
2854+
{
2855+
GxSimpleCollection<DateTime> dtCollection = new GxSimpleCollection<DateTime>();
2856+
foreach (string st in stCollection)
2857+
{
2858+
dtCollection.Add(CToT2(st, context));
2859+
}
2860+
return dtCollection;
2861+
}
28532862
public static DateTime CToH2(string value)
28542863
{
28552864
if (isNullJsonDate(value))
@@ -2916,10 +2925,21 @@ public static string TToC2(DateTime dt, IGxContext context)
29162925
{
29172926
return TToC2(dt, true, context);
29182927
}
2928+
public static GxSimpleCollection<string> TToC2(GxSimpleCollection<DateTime> dtCollection, IGxContext context)
2929+
{
2930+
GxSimpleCollection<string> stCollection = new GxSimpleCollection<string>();
2931+
foreach(DateTime dt in dtCollection)
2932+
{
2933+
stCollection.Add(TToC2(dt, true, context));
2934+
}
2935+
return stCollection;
2936+
}
2937+
29192938
public static string HToC2(DateTime dt)
29202939
{
29212940
return TToC2(dt, false);
29222941
}
2942+
29232943
//[Obsolete("TToC2 is deprecated, use TToC2(DateTime, bool, IGxContext) instead", false)]
29242944
public static string TToC2(DateTime dt, bool toUTC)
29252945
{

dotnet/src/dotnetframework/GxClasses/Domain/GxCollections.cs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -787,20 +787,51 @@ public virtual void FromJSONObject(dynamic obj)
787787
public GxSimpleCollection<string> ToStringCollection(int digits, int decimals)
788788
{
789789
GxSimpleCollection<string> result = new GxSimpleCollection<string>();
790-
foreach (T item in this)
790+
if (typeof(T) == typeof(string))
791791
{
792-
decimal value = (decimal)Convert.ChangeType(item, typeof(decimal));
793-
result.Add(StringUtil.LTrim(StringUtil.Str(value, digits, decimals)));
792+
foreach (T item in this)
793+
{
794+
result.Add(item as string);
795+
}
796+
}
797+
else
798+
{
799+
foreach (T item in this)
800+
{
801+
decimal value = (decimal)Convert.ChangeType(item, typeof(decimal));
802+
result.Add(StringUtil.LTrim(StringUtil.Str(value, digits, decimals)));
803+
}
794804
}
795805
return result;
796806
}
797807
public void FromStringCollection(GxSimpleCollection<string> value)
798808
{
799-
foreach (string item in value)
809+
if (typeof(T) == typeof(DateTime))
810+
{
811+
foreach (string item in value)
812+
{
813+
Add(DateTimeUtil.CToT2(item));
814+
}
815+
}
816+
else if(typeof(T) == typeof(string))
800817
{
801-
Add(Convert.ChangeType(NumberUtil.Val(item.ToString()), typeof(T)));
818+
foreach (string item in value)
819+
{
820+
Add(item);
821+
}
822+
}else {
823+
foreach (string item in value)
824+
{
825+
Add(Convert.ChangeType(NumberUtil.Val(item.ToString()), typeof(T)));
826+
}
802827
}
803828
}
829+
//To delete
830+
public void FromStringCollection(GxSimpleCollection<string> value, IGxContext context)
831+
{
832+
FromStringCollection(value);
833+
}
834+
804835

805836
}
806837
#if !NETCORE

dotnet/src/dotnetframework/GxClasses/Domain/GxGenericCollections.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ public GXBaseCollection(GXBaseCollection<T> value)
122122
Add(item);
123123
}
124124
}
125-
126125
protected CollectionBase jsonArr
127126
{
128127
get

dotnet/src/dotnetframework/GxClasses/Model/GXSilentTrn.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -731,6 +731,15 @@ public GxUserType Sdt
731731
get { return sdt1; }
732732
set { sdt1 = (T)value; }
733733
}
734+
#if NETCORE
735+
[JsonIgnore]
736+
public T InternalSdt
737+
{
738+
get { return sdt1; }
739+
set { sdt1 = value; }
740+
}
741+
#endif
742+
734743
#if NETCORE
735744
[JsonIgnore]
736745
#endif

0 commit comments

Comments
 (0)