mirror of
https://gitee.com/dotnetchina/OpenAuth.Net.git
synced 2025-04-05 17:38:01 +08:00
fix 动态API,多层级反序列化
This commit is contained in:
parent
2d768d3cc7
commit
8d031a701d
@ -151,7 +151,7 @@ namespace OpenAuth.App
|
||||
}
|
||||
|
||||
// 将对象转换为字典
|
||||
var dict = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(req.Obj);
|
||||
var dict = JsonHelper.Instance.Deserialize<Dictionary<string, Object>>(req.Obj);
|
||||
|
||||
// 设置ID
|
||||
if (!dict.ContainsKey("Id"))
|
||||
@ -219,7 +219,7 @@ namespace OpenAuth.App
|
||||
}
|
||||
|
||||
// 将对象转换为字典
|
||||
var dict = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(req.Obj);
|
||||
var dict = JsonHelper.Instance.Deserialize<Dictionary<string, Object>>(req.Obj);
|
||||
|
||||
// 检查ID是否存在
|
||||
if (!dict.ContainsKey("Id"))
|
||||
@ -370,10 +370,6 @@ namespace OpenAuth.App
|
||||
throw new Exception($"未找到方法:{req.MethodName}");
|
||||
}
|
||||
|
||||
// 调用方法
|
||||
// 将对象转换为字典
|
||||
var dict = JsonHelper.Instance.Deserialize<Dictionary<string, string>>(req.Parameters);
|
||||
|
||||
// 获取方法参数信息
|
||||
var parameters = method.GetParameters();
|
||||
var paramValues = new object[parameters.Length];
|
||||
@ -382,14 +378,44 @@ namespace OpenAuth.App
|
||||
for (int i = 0; i < parameters.Length; i++)
|
||||
{
|
||||
var param = parameters[i];
|
||||
if (dict.ContainsKey(param.Name))
|
||||
|
||||
// 尝试从JSON中获取参数值
|
||||
try
|
||||
{
|
||||
// 将字典中的值转换为参数类型
|
||||
paramValues[i] = Convert.ChangeType(dict[param.Name], param.ParameterType);
|
||||
var json = req.Parameters;
|
||||
// 检查参数名是否存在于JSON中
|
||||
if (json.Contains($"\"{param.Name}\""))
|
||||
{
|
||||
// 解析完整的JSON对象
|
||||
var jsonObj = JsonHelper.Instance.Deserialize<Dictionary<string, object>>(json);
|
||||
|
||||
// 从JSON对象中获取参数值
|
||||
if (jsonObj.ContainsKey(param.Name))
|
||||
{
|
||||
// 获取参数对应的JSON值并序列化为字符串
|
||||
var paramValue = JsonHelper.Instance.Serialize(jsonObj[param.Name]);
|
||||
|
||||
// 将JSON字符串反序列化为目标类型
|
||||
var deserializeMethod = typeof(JsonHelper).GetMethod("Deserialize").MakeGenericMethod(param.ParameterType);
|
||||
paramValues[i] = deserializeMethod.Invoke(JsonHelper.Instance, new object[] { paramValue });
|
||||
}
|
||||
else
|
||||
{
|
||||
// 参数名存在但获取不到,使用默认值
|
||||
paramValues[i] = param.HasDefaultValue ? param.DefaultValue : null;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
// 如果JSON中没有对应的属性,使用默认值
|
||||
paramValues[i] = param.HasDefaultValue ? param.DefaultValue : null;
|
||||
}
|
||||
}
|
||||
else
|
||||
catch (Exception ex)
|
||||
{
|
||||
// 如果字典中没有对应的参数,使用默认值
|
||||
// 记录错误日志
|
||||
Console.WriteLine($"反序列化参数 {param.Name} 失败: {ex.Message}");
|
||||
// 反序列化失败,使用默认值
|
||||
paramValues[i] = param.HasDefaultValue ? param.DefaultValue : null;
|
||||
}
|
||||
}
|
||||
|
@ -28,6 +28,14 @@ namespace OpenAuth.App.Test
|
||||
|
||||
return services;
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestAdd()
|
||||
{
|
||||
var app = _autofacServiceProvider.GetService<DynamicApiApp>();
|
||||
var obj = await app.Add(new AddOrUpdateDynamicEntityReq { TableName = "noentity", Obj = "{\"P1\":\"测试\",\"Id\":\"12\"}" });
|
||||
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public async Task TestGet()
|
||||
@ -65,25 +73,10 @@ namespace OpenAuth.App.Test
|
||||
var obj = app.Invoke(new InvokeDynamicReq {
|
||||
ServiceName = "UserManagerApp",
|
||||
MethodName = "Load",
|
||||
Parameters = "{\"request\":{\"page\":1,\"limit\":10,\"key\":\"\"}}"
|
||||
Parameters = "{\"request\":{\"page\":1,\"limit\":10,\"key\":\"dddd\"}}"
|
||||
});
|
||||
|
||||
// 如果返回的是Task,需要等待它完成并获取结果
|
||||
if (obj is Task task)
|
||||
{
|
||||
await task;
|
||||
// 使用反射获取Task的Result属性
|
||||
var resultProperty = task.GetType().GetProperty("Result");
|
||||
if (resultProperty != null)
|
||||
{
|
||||
var result = resultProperty.GetValue(task);
|
||||
Console.WriteLine(JsonHelper.Instance.Serialize(result));
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Console.WriteLine(JsonHelper.Instance.Serialize(obj));
|
||||
}
|
||||
Console.WriteLine(obj.ToString());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user