Merge pull request #2751 from abpframework/maliming/proxyscript

Make proxy script support [FromForm] model binding.
pull/2766/head
Halil İbrahim Kalkan 5 years ago committed by GitHub
commit b27f1f36e3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -175,7 +175,10 @@ namespace Volo.Abp.AspNetCore.Mvc
parameterDescription.RouteInfo?.IsOptional ?? false,
parameterDescription.RouteInfo?.DefaultValue,
parameterDescription.RouteInfo?.Constraints?.Select(c => c.GetType().Name).ToArray(),
parameterDescription.Source.Id
parameterDescription.Source.Id,
parameterDescription.ModelMetadata.ContainerType != null
? parameterDescription.ParameterDescriptor.Name
: string.Empty
)
);
}

@ -1,4 +1,4 @@
using System;
using System;
namespace Volo.Abp.Http.Modeling
{
@ -19,12 +19,14 @@ namespace Volo.Abp.Http.Modeling
public string BindingSourceId { get; set; }
public string DescriptorName { get; set; }
private ParameterApiDescriptionModel()
{
}
public static ParameterApiDescriptionModel Create(string name, string nameOnMethod, Type type, bool isOptional = false, object defaultValue = null, string[] constraintTypes = null, string bindingSourceId = null)
public static ParameterApiDescriptionModel Create(string name, string nameOnMethod, Type type, bool isOptional = false, object defaultValue = null, string[] constraintTypes = null, string bindingSourceId = null, string descriptorName = null)
{
return new ParameterApiDescriptionModel
{
@ -34,7 +36,8 @@ namespace Volo.Abp.Http.Modeling
IsOptional = isOptional,
DefaultValue = defaultValue,
ConstraintTypes = constraintTypes,
BindingSourceId = bindingSourceId
BindingSourceId = bindingSourceId,
DescriptorName = descriptorName
};
}
}

@ -96,7 +96,11 @@ namespace Volo.Abp.Http.ProxyScripting.Generators.JQuery
AddAjaxCallParameters(script, action);
script.AppendLine(" }, ajaxParams));;");
var ajaxParamsIsFromForm = action.Parameters.Any(x => x.BindingSourceId == ParameterBindingSources.Form);
script.AppendLine(ajaxParamsIsFromForm
? " }, $.extend(true, {}, ajaxParams, { contentType: 'application/x-www-form-urlencoded; charset=UTF-8' })));"
: " }, ajaxParams));");
script.AppendLine(" };");
}

@ -66,7 +66,7 @@ namespace Volo.Abp.Http.ProxyScripting.Generators
return null;
}
return ProxyScriptingJsFuncHelper.CreateJsObjectLiteral(parameters, indent);
return ProxyScriptingJsFuncHelper.CreateJsFormPostData(parameters, indent);
}
private static string ReplacePathVariables(string url, IList<ParameterApiDescriptionModel> actionParameters)

@ -111,8 +111,8 @@ namespace Volo.Abp.Http.ProxyScripting.Generators
public static string GetParamNameInJsFunc(ParameterApiDescriptionModel parameterInfo)
{
return parameterInfo.Name == parameterInfo.NameOnMethod
? NormalizeJsVariableName(parameterInfo.Name.ToCamelCase(), ".")
: NormalizeJsVariableName(parameterInfo.NameOnMethod.ToCamelCase()) + "." + NormalizeJsVariableName(parameterInfo.Name.ToCamelCase(), ".");
? NormalizeJsVariableName(parameterInfo.Name.ToCamelCase(), ".")
: NormalizeJsVariableName(parameterInfo.NameOnMethod.ToCamelCase()) + "." + NormalizeJsVariableName(parameterInfo.Name.ToCamelCase(), ".");
}
public static string CreateJsObjectLiteral(ParameterApiDescriptionModel[] parameters, int indent = 0)
@ -131,9 +131,37 @@ namespace Volo.Abp.Http.ProxyScripting.Generators
return sb.ToString();
}
public static string GetFormPostParamNameInJsFunc(ParameterApiDescriptionModel parameterInfo)
{
return parameterInfo.Name == parameterInfo.NameOnMethod
? NormalizeJsVariableName((parameterInfo.DescriptorName + parameterInfo.Name).ToCamelCase(), ".")
: NormalizeJsVariableName(parameterInfo.NameOnMethod.ToCamelCase()) + "." + NormalizeJsVariableName((parameterInfo.DescriptorName + parameterInfo.Name).ToCamelCase(), ".");
}
public static string CreateJsFormPostData(ParameterApiDescriptionModel[] parameters, int indent)
{
var sb = new StringBuilder();
for (var i = 0; i < parameters.Length; i++)
{
var and = i < parameters.Length - 1 ? " + '&' + " : string.Empty;
var parameterName = parameters[i].DescriptorName.IsNullOrWhiteSpace()
? parameters[i].Name
: $"{parameters[i].DescriptorName}.{parameters[i].Name}";
sb.Append($"'{parameterName}=' + {GetFormPostParamNameInJsFunc(parameters[i])}{and}");
}
return sb.ToString();
}
public static string GenerateJsFuncParameterList(ActionApiDescriptionModel action, string ajaxParametersName)
{
var methodParamNames = action.ParametersOnMethod.Select(p => p.Name).Distinct().ToList();
var paramsIsFromForm = action.Parameters.Any(x => x.BindingSourceId == ParameterBindingSources.Form);
var methodParamNames = paramsIsFromForm
? action.Parameters.Select(p => p.DescriptorName + p.Name).Distinct().ToList()
: action.ParametersOnMethod.Select(p => p.Name).Distinct().ToList();
methodParamNames.Add(ajaxParametersName);
return methodParamNames.Select(prmName => NormalizeJsVariableName(prmName.ToCamelCase())).JoinAsString(", ");
}

Loading…
Cancel
Save