From 699843f5e4efa27e4d8f9e4cfda983d9f577b823 Mon Sep 17 00:00:00 2001 From: AlejandroP Date: Wed, 6 Apr 2022 11:07:50 -0300 Subject: [PATCH] - Variable external name for API Objects Issues 95409 90822 --- .../GxClasses/Middleware/GXHttpModules.cs | 2 +- .../GxClasses/Services/GxRestWrapper.cs | 43 +++++++++++++++++-- .../GxClasses/Services/ReflectionHelper.cs | 29 +++++++++++++ 3 files changed, 69 insertions(+), 5 deletions(-) diff --git a/dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpModules.cs b/dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpModules.cs index a95c5ff5d..ea847a093 100644 --- a/dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpModules.cs +++ b/dotnet/src/dotnetframework/GxClasses/Middleware/GXHttpModules.cs @@ -131,7 +131,7 @@ public void ServicesGroupSetting(string webPath) object p = JSONHelper.Deserialize(File.ReadAllText(grp)); #pragma warning restore SCS0018 MapGroup m = p as MapGroup; - if (m != null) + if (m != null && m.Name != null && m.Mappings != null ) { if (String.IsNullOrEmpty(m.BasePath)) diff --git a/dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs b/dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs index 4a6046603..294e876fa 100644 --- a/dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs +++ b/dotnet/src/dotnetframework/GxClasses/Services/GxRestWrapper.cs @@ -104,7 +104,7 @@ public virtual Task MethodBodyExecute(object key) if (IsCoreEventReplicator(_procWorker)) { bodyParameters = ReadBodyParameters(); - string synchronizer = PreProcessReplicatorParameteres(_procWorker, innerMethod, bodyParameters); + string synchronizer = PreProcessReplicatorParameteres( _procWorker, innerMethod, bodyParameters); if (!IsAuthenticated(synchronizer)) return Task.CompletedTask; } @@ -133,6 +133,7 @@ public virtual Task MethodBodyExecute(object key) if (!String.IsNullOrEmpty(this._serviceMethod)) { innerMethod = this._serviceMethod; + bodyParameters = PreProcessApiSdtParameter(_procWorker, innerMethod, bodyParameters, this._variableAlias); } Dictionary outputParameters = ReflectionHelper.CallMethod(_procWorker, innerMethod, bodyParameters, _gxContext); Dictionary formatParameters = ReflectionHelper.ParametersFormat(_procWorker, innerMethod); @@ -150,9 +151,7 @@ public virtual Task MethodBodyExecute(object key) finally { Cleanup(); - - } - + } } public virtual Task Post() @@ -186,6 +185,42 @@ private Dictionary ReadBodyParameters() return ReadRequestParameters(_httpContext.Request.GetInputStream()); #endif } + + + private Dictionary SetAlias(Dictionary bodyParameters, Dictionary varAlias) + { + Dictionary parameters = new Dictionary(); + foreach (string k in bodyParameters.Keys) + { + if (k != null) + { + string keyLowercase = k.ToLower(); + if (varAlias == null) + parameters[keyLowercase] = bodyParameters[k]; + else + { + if (varAlias.ContainsKey(keyLowercase)) + { + string alias = varAlias[keyLowercase].ToLower(); + parameters[alias] = bodyParameters[k]; + } + else if (!varAlias.ContainsValue(keyLowercase)) + { + parameters[keyLowercase] = bodyParameters[k]; + } + } + } + } + return parameters; + } + + private Dictionary PreProcessApiSdtParameter(GXBaseObject procWorker, string innerMethod, + Dictionary bodyParameters, Dictionary varAlias) + { + Dictionary bP = SetAlias(bodyParameters, varAlias); + return ReflectionHelper.GetWrappedParameter(procWorker, innerMethod, bP); + } + private string PreProcessReplicatorParameteres(GXBaseObject procWorker, string innerMethod, Dictionary bodyParameters) { var methodInfo = procWorker.GetType().GetMethod(innerMethod); diff --git a/dotnet/src/dotnetframework/GxClasses/Services/ReflectionHelper.cs b/dotnet/src/dotnetframework/GxClasses/Services/ReflectionHelper.cs index c500e40b6..751a046b7 100644 --- a/dotnet/src/dotnetframework/GxClasses/Services/ReflectionHelper.cs +++ b/dotnet/src/dotnetframework/GxClasses/Services/ReflectionHelper.cs @@ -7,6 +7,7 @@ using System.Text.RegularExpressions; using GeneXus.Utils; using System.Linq; +using Jayrock.Json; using Type = System.Type; @@ -67,6 +68,34 @@ public static bool MethodHasInputParameters(object instance, String methodName) return false; } + public static Dictionary GetWrappedParameter(object instance, String methodName, Dictionary bodyParameters) + { + System.Diagnostics.Debugger.Launch(); + MethodInfo methodInfo = instance.GetType().GetMethod(methodName); + var methodParameters = methodInfo.GetParameters(); + List inputParameters = new List(); + foreach (var methodParameter in methodParameters) + { + if (!methodParameter.IsOut) + { + inputParameters.Add(methodParameter); + } + } + if (inputParameters.Count == 1 && bodyParameters.Count>1) + { + ParameterInfo pInfo = inputParameters[0]; + if (pInfo.ParameterType.IsSubclassOf(typeof(GxUserType))) + { + var gxParameterName = GxParameterName(pInfo.Name).ToLower(); + Dictionary parameters = new Dictionary(); + JObject jparms = new JObject(bodyParameters); + parameters.Add(gxParameterName, jparms); + return parameters; + } + } + return bodyParameters; + } + private static object ConvertSingleJsonItem(object value, Type newType, IGxContext context) { if (value!= null && value.GetType() == newType)