private void AddAjaxCallParameters(StringBuilder script, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action) { var httpMethod = action.HttpMethod?.ToUpperInvariant() ?? "POST"; script.AppendLine(" url: abp.appPath + '" + ProxyScriptingHelper.GenerateUrlWithParameters(action) + "',"); script.Append(" type: '" + httpMethod + "'"); var headers = ProxyScriptingHelper.GenerateHeaders(action, 8); if (headers != null) { script.AppendLine(","); script.Append(" headers: " + headers); } var body = ProxyScriptingHelper.GenerateBody(action); if (!body.IsNullOrEmpty()) { script.AppendLine(","); script.Append(" data: JSON.stringify(" + body + ")"); } else { var formData = ProxyScriptingHelper.GenerateFormPostData(action, 8); if (!formData.IsNullOrEmpty()) { script.AppendLine(","); script.Append(" data: " + formData); } } script.AppendLine(); }
public ControllerApiDescriptionModel AddController(ControllerApiDescriptionModel controller) { if (Controllers.ContainsKey(controller.Type)) { throw new AbpException($"There is already a controller with type: {controller.Type} in module: {RootPath}"); } return(Controllers[controller.Type] = controller); }
private static void AddActionScript(StringBuilder script, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action) { var actionName = ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(action.Name.ToCamelCase()); var parameterList = ProxyScriptingJsFuncHelper.GenerateJsFuncParameterList(action, "ajaxParams"); script.AppendLine($" serviceNamespace{actionName} = function({parameterList}) {{"); script.AppendLine(" return site.ajax(site.utils.extend({"); AddAjaxCallParameters(script, controller, action); script.AppendLine(" }, ajaxParams));"); script.AppendLine(" };"); }
private string GetNormalizedMethodName(ControllerApiDescriptionModel controllerModel, MethodInfo method) { if (!IsAsync(method)) { return(method.Name); } var normalizedName = Regex.Replace(method.Name, "Async$", ""); if (controllerModel.Actions.ContainsKey(normalizedName)) { return(method.Name); } return(normalizedName); }
public ControllerApiDescriptionModel CreateSubModel(string[] actions) { var subModel = new ControllerApiDescriptionModel { Type = Type, Interfaces = Interfaces, ControllerName = ControllerName, Actions = new Dictionary <string, ActionApiDescriptionModel>() }; foreach (var action in Actions) { if (actions == null || actions.Contains(action.Key)) { subModel.AddAction(action.Key, action.Value); } } return(subModel); }
private string GetNormalizedMethodName(ControllerApiDescriptionModel controllerModel, MethodInfo method) { if (!_apiProxyScriptingConfiguration.RemoveAsyncPostfixOnProxyGeneration) { return(method.Name); } if (!method.IsAsync()) { return(method.Name); } var normalizedName = method.Name.RemovePostFix("Async"); if (controllerModel.Actions.ContainsKey(normalizedName)) { return(method.Name); } return(normalizedName); }
private static void AddAjaxCallParameters(StringBuilder script, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action) { var httpMethod = action.HttpMethod?.ToUpperInvariant() ?? "POST"; script.AppendLine(" url: site.Config.webapiDomain + '" + ProxyScriptingHelper.GenerateUrlWithParameters(action) + "',"); script.Append(" type: '" + httpMethod + "'"); if (action.ReturnValue.Type == typeof(void)) { script.AppendLine(","); script.Append(" dataType: null"); } var headers = ProxyScriptingHelper.GenerateHeaders(action, 8); if (headers != null) { script.AppendLine(","); script.Append(" headers: " + headers); } var body = ProxyScriptingHelper.GenerateBody(action); if (!body.IsNullOrEmpty()) { script.AppendLine(","); script.Append(" data: JSON.stringify(input)"); } else { var formData = ProxyScriptingHelper.GenerateFormPostData(action, 8); if (!formData.IsNullOrEmpty()) { script.AppendLine(","); script.Append(" data: " + formData); } } script.AppendLine(); }
private static void AddControllerScript(StringBuilder script, ControllerApiDescriptionModel controller) { var controllerName = GetNormalizedTypeName(controller.Type); script.AppendLine($" // controller {controllerName}"); script.AppendLine(); script.AppendLine(" (function(){"); script.AppendLine(); script.AppendLine($" Plus.utils.createNamespace(window, '{controllerName}');"); var normalizedActionNames = CalculateNormalizedActionNames(controller.Actions); foreach (var action in controller.Actions.Values) { script.AppendLine(); AddActionScript(script, controllerName, action, normalizedActionNames[action]); } script.AppendLine(); script.AppendLine(" })();"); }
private static void AddAjaxCallParameters(StringBuilder script, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action) { //var httpMethod = action.HttpMethod?.ToUpperInvariant() ?? "POST"; //script.AppendLine(" url: abp.appPath + '" + Abp.Web.Api.ProxyScripting.Generators.ProxyScriptingHelper.GenerateUrlWithParameters(action) + "',"); //script.Append(" type: '" + httpMethod + "'"); //if (action.ReturnValue.Type == typeof(void)) //{ // script.AppendLine(","); // script.Append(" dataType: null"); //} //var headers = ProxyScriptingHelper.GenerateHeaders(action, 8); //if (headers != null) //{ // script.AppendLine(","); // script.Append(" headers: " + headers); //} //var body = ProxyScriptingHelper.GenerateBody(action); //if (!body.IsNullOrEmpty()) //{ // script.AppendLine(","); // script.Append(" data: JSON.stringify(" + body + ")"); //} //else //{ // var formData = ProxyScriptingHelper.GenerateFormPostData(action, 8); // if (!formData.IsNullOrEmpty()) // { // script.AppendLine(","); // script.Append(" data: " + formData); // } //} //script.AppendLine(); }
private static void AddControllerScript(StringBuilder script, ModuleApiDescriptionModel module, ControllerApiDescriptionModel controller) { script.AppendLine($" // controller '{controller.Name.ToCamelCase()}'"); script.AppendLine(" (function(){"); script.AppendLine(); script.AppendLine($" abp.services.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} = abp.services.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} || {{}};"); foreach (var action in controller.Actions.Values) { script.AppendLine(); AddActionScript(script, module, controller, action); } script.AppendLine(); script.AppendLine(" })();"); }
public ControllerApiDescriptionModel GetOrAddController(string name, string groupName, Type type, [CanBeNull] HashSet <Type> ignoredInterfaces = null) { return(Controllers.GetOrAdd(type.FullName, () => ControllerApiDescriptionModel.Create(name, groupName, type, ignoredInterfaces))); }
private static void AddActionScript(StringBuilder script, ModuleApiDescriptionModel module, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action) { script.AppendLine($" // action '{action.Name.ToCamelCase()}'"); var settings = new JsonSchemaGeneratorSettings(); settings.FlattenInheritanceHierarchy = true; settings.DefaultPropertyNameHandling = NJsonSchema.PropertyNameHandling.CamelCase; settings.DefaultReferenceTypeNullHandling = NJsonSchema.ReferenceTypeNullHandling.NotNull; var generator = new JsonSchemaGenerator(settings); script.AppendLine($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()} = abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()} || {{}};"); var type = action.ReturnValue.Type; if (type != typeof(Task)) { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task <>)) { type = type.GetGenericArguments()[0]; // use this... } //var schema = await generator.GenerateAsync<JsonSchema4>(typeof(Person)); var schema = generator.GenerateAsync(type).GetAwaiter().GetResult(); var schemaData = schema.ToJson(); //var js = new Newtonsoft.Json.Schema.Generation.JSchemaGenerator(); //var schema = js.Generate(action.ReturnValue.Type); //schema.Title = action.ReturnValue.Type.Name; script.Append($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.returnValue = "); script.AppendLine(schemaData); } if (action.Parameters.Any()) { script.Append($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.parameters = "); script.AppendLine("{"); foreach (var parameter in action.Parameters) { type = parameter.Type; var schema = generator.GenerateAsync(type).GetAwaiter().GetResult(); if (parameter.BindingSourceId.IsIn(ParameterBindingSources.ModelBinding, ParameterBindingSources.Query)) { schema.Title = parameter.Name; schema.Default = parameter.DefaultValue; //schema.re = parameter.IsOptional } var schemaData = schema.ToJson(); //script.Append($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.{parameter.Name.ToCamelCase()} = "); script.Append($" {parameter.Name.ToCamelCase()} : "); script.Append(schemaData); script.AppendLine(","); } script.AppendLine(" };"); } //script.AppendLine(" url: abp.appPath + '" + ProxyScriptingHelper.GenerateUrlWithParameters(action) + "',"); //var body = GenerateBody(action); //if (!body.IsNullOrEmpty()) //{ // script.AppendLine(","); // script.Append(" data: JSON.stringify(" + body + ")"); //} //else //{ // var formData = GenerateFormPostData(action, 8); // if (!formData.IsNullOrEmpty()) // { // script.AppendLine(","); // script.Append(" data: " + formData); // } //} //var parameterList = ProxyScriptingJsFuncHelper.GenerateJsFuncParameterList(action, "ajaxParams"); //script.AppendLine($" // action '{action.Name.ToCamelCase()}'"); //script.AppendLine($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}{ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(action.Name.ToCamelCase())} = function({parameterList}) {{"); //script.AppendLine(" return abp.ajax($.extend(true, {"); //AddAjaxCallParameters(script, controller, action); //script.AppendLine(" }, ajaxParams));;"); //script.AppendLine(" };"); }
private void AddControllerScript(StringBuilder script, ModuleApiDescriptionModel module, ControllerApiDescriptionModel controller) { script.AppendLine($"// controller '{controller.Name.ToCamelCase()}'"); script.AppendLine($"abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} = abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} || {{}};"); /* * var get = controller.Actions.Values.FirstOrDefault(a => a.Name.Equals("get", StringComparison.InvariantCultureIgnoreCase)); * if (get != null) * { * script.AppendLine($" // get return type"); * string varStr = $"abp.schemas.{ module.Name.ToCamelCase()}.{ controller.Name.ToCamelCase()}.get"; * script.AppendLine($" {varStr} = {varStr} || {{}};"); * var type = get.ReturnValue.Type; * if (type != typeof(Task)) * { * if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task<>)) * { * type = type.GetGenericArguments()[0]; // use this... * } * var schema = generator.GenerateAsync(type).GetAwaiter().GetResult(); * var schemaData = schema.ToJson(); * //schema.Title = action.ReturnValue.Type.Name; * script.Append($" {varStr} = "); * script.AppendLine(schemaData); * } * } * // getall * var getall = controller.Actions.Values.FirstOrDefault(a => a.Name.Equals("getall", StringComparison.InvariantCultureIgnoreCase)); * if (getall != null) * { * script.AppendLine($" // getall parameters"); * string varStr = $"abp.schemas.{ module.Name.ToCamelCase()}.{ controller.Name.ToCamelCase()}.filter"; * //script.AppendLine($" {varStr} = {varStr} || {{}};"); * * * if (getall.Parameters.Count == 1) * { * script.Append($" {varStr} = "); * //script.AppendLine("{"); * foreach (var parameter in getall.Parameters) * { * var type = parameter.Type; * var schema = generator.GenerateAsync(type).GetAwaiter().GetResult(); * if (parameter.BindingSourceId.IsIn(ParameterBindingSources.ModelBinding, ParameterBindingSources.Query)) * { * schema.Title = parameter.Name; * schema.Default = parameter.DefaultValue; * //schema.re = parameter.IsOptional * } * var schemaData = schema.ToJson(); * //script.Append($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.{parameter.Name.ToCamelCase()} = "); * // script.Append($" {parameter.Name.ToCamelCase()} : "); * script.Append(schemaData).Append(";") ; * //script.AppendLine(","); * } * //script.AppendLine(" };"); * } * } */ foreach (var action in controller.Actions.Values) { AddActionScript(script, module, controller, action); } }
private void AddActionScript(StringBuilder script, ModuleApiDescriptionModel module, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action) { script.AppendLine($"// action '{action.Name.ToCamelCase()}'"); script.AppendLine($"abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()} = abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()} || {{}};"); var type = action.ReturnValue.Type; if (type != typeof(Task)) { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task <>)) { type = type.GetGenericArguments()[0]; // use this... } var schema = generator.GenerateAsync(type).GetAwaiter().GetResult(); try { schema = CleanUpSchema(schema); var schemaData = schema.ToJson(); //schema.Title = action.ReturnValue.Type.Name; script.Append($"abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.returnValue = "); script.AppendLine(schemaData); } catch (Exception ex) { var pathError = $"abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.returnValue"; Logger.Error("Error generating schema for " + pathError); script.AppendLine("// Error generating schema for " + pathError); } } if (action.Parameters.Any()) { script.Append($"abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.parameters = "); script.AppendLine("{"); foreach (var parameter in action.Parameters) { type = parameter.Type; var schema = generator.GenerateAsync(type).GetAwaiter().GetResult(); if (parameter.BindingSourceId.IsIn(ParameterBindingSources.ModelBinding, ParameterBindingSources.Query)) { schema.Title = parameter.Name; schema.Default = parameter.DefaultValue; //schema.re = parameter.IsOptional } try { schema = CleanUpSchema(schema); } catch (Exception ex) { throw new Exception($"{module.Name} {controller.Name} {action.Name} {parameter.Name} : {ex.Message}", ex); } var schemaData = schema.ToJson(); script.Append($" {parameter.Name.ToCamelCase()} : "); script.Append(schemaData); script.AppendLine(","); } script.AppendLine("};"); } }
private static void AddActionScript(StringBuilder script, ModuleApiDescriptionModel module, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action) { var parameterList = ProxyScriptingJsFuncHelper.GenerateJsFuncParameterList(action, "ajaxParams"); script.AppendLine($" // action '{action.Name.ToCamelCase()}'"); script.AppendLine($" abp.services.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}{ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(action.Name.ToCamelCase())} = function({parameterList}) {{"); script.AppendLine(" return abp.ajax($.extend(true, {"); AddAjaxCallParameters(script, controller, action); script.AppendLine(" }, ajaxParams));;"); script.AppendLine(" };"); }
public ControllerApiDescriptionModel GetOrAddController(string name, string groupName, bool isRemoteService, string apiVersion, Type type, [CanBeNull] HashSet <Type> ignoredInterfaces = null) { var key = apiVersion.IsNullOrWhiteSpace() ? type.FullName : $"{apiVersion + "."}{type.FullName}"; return(Controllers.GetOrAdd(key, () => ControllerApiDescriptionModel.Create(name, groupName, isRemoteService, apiVersion, type, ignoredInterfaces))); }
private static void AddActionScript(StringBuilder script, ModuleApiDescriptionModel module, ControllerApiDescriptionModel controller, ActionApiDescriptionModel action) { script.AppendLine($" // action '{action.Name.ToCamelCase()}'"); var settings = new JsonSchemaGeneratorSettings(); settings.FlattenInheritanceHierarchy = true; settings.DefaultPropertyNameHandling = NJsonSchema.PropertyNameHandling.CamelCase; settings.DefaultReferenceTypeNullHandling = NJsonSchema.ReferenceTypeNullHandling.NotNull; var generator = new JsonSchemaGenerator(settings); script.AppendLine($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()} = abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()} || {{}};"); var type = action.ReturnValue.Type; if (type != typeof(Task)) { if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Task <>)) { type = type.GetGenericArguments()[0]; // use this... } //var schema = await generator.GenerateAsync<JsonSchema4>(typeof(Person)); var schema = generator.GenerateAsync(type).GetAwaiter().GetResult(); var schemaData = schema.ToJson(); //var js = new Newtonsoft.Json.Schema.Generation.JSchemaGenerator(); //var schema = js.Generate(action.ReturnValue.Type); //schema.Title = action.ReturnValue.Type.Name; script.Append($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.returnValue = "); script.AppendLine(schemaData); } foreach (var parameter in action.Parameters) { type = parameter.Type; var schema = generator.GenerateAsync(type).GetAwaiter().GetResult(); var schemaData = schema.ToJson(); script.Append($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}.{action.Name.ToCamelCase()}.{parameter.Name.ToCamelCase()} = "); script.AppendLine(schemaData); } //var parameterList = ProxyScriptingJsFuncHelper.GenerateJsFuncParameterList(action, "ajaxParams"); //script.AppendLine($" // action '{action.Name.ToCamelCase()}'"); //script.AppendLine($" abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()}{ProxyScriptingJsFuncHelper.WrapWithBracketsOrWithDotPrefix(action.Name.ToCamelCase())} = function({parameterList}) {{"); //script.AppendLine(" return abp.ajax($.extend(true, {"); //AddAjaxCallParameters(script, controller, action); //script.AppendLine(" }, ajaxParams));;"); //script.AppendLine(" };"); }