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("};");
            }
        }
Exemple #2
0
    public ModuleApiDescriptionModel AddModule(ModuleApiDescriptionModel module)
    {
        if (Modules.ContainsKey(module.RootPath))
        {
            throw new AbpException("There is already a module with same root path: " + module.RootPath);
        }

        return(Modules[module.RootPath] = module);
    }
        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("    };");
        }
        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("    };");
        }
 private void AddModuleScript(StringBuilder script, ModuleApiDescriptionModel module)
 {
     script.AppendLine($"// module '{module.Name.ToCamelCase()}'");
     script.AppendLine("(function(){");
     script.AppendLine();
     script.AppendLine($"abp.schemas.{module.Name.ToCamelCase()} = abp.schemas.{module.Name.ToCamelCase()} || {{}};");
     foreach (var controller in module.Controllers.Values)
     {
         script.AppendLine();
         AddControllerScript(script, module, controller);
     }
     script.AppendLine();
     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.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} = abp.schemas.{module.Name.ToCamelCase()}.{controller.Name.ToCamelCase()} || {{}};");

            foreach (var action in controller.Actions.Values)
            {
                AddActionScript(script, module, controller, action);
            }

            script.AppendLine();
            script.AppendLine("  })();");
        }
        private static void AddModuleScript(StringBuilder script, ModuleApiDescriptionModel module)
        {
            script.AppendLine($"// module '{module.Name.ToCamelCase()}'");
            script.AppendLine("(function(){");
            script.AppendLine();
            script.AppendLine($"  infrastructure.services.{module.Name.ToCamelCase()} = infrastructure.services.{module.Name.ToCamelCase()} || {{}};");

            foreach (var controller in module.Controllers.Values)
            {
                script.AppendLine();
                AddControllerScript(script, module, controller);
            }

            script.AppendLine();
            script.AppendLine("})();");
        }
        private static void AddModuleScript(StringBuilder script, ModuleApiDescriptionModel module)
        {
            //TODO: Eleminate repeating module.RootPath.Replace("/", ".").ToCamelCase() !
            //TODO: Remove illegal chars (like '-') from module/controller names!

            script.AppendLine($"// module {module.RootPath.ToCamelCase()}");
            script.AppendLine();
            script.AppendLine("(function(){");

            foreach (var controller in module.Controllers.Values)
            {
                script.AppendLine();
                AddControllerScript(script, controller);
            }

            script.AppendLine();
            script.AppendLine("})();");
        }
        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("    };");
        }
Exemple #10
0
 public ModuleApiDescriptionModel GetOrAddModule(string rootPath, string remoteServiceName)
 {
     return(Modules.GetOrAdd(rootPath, () => ModuleApiDescriptionModel.Create(rootPath, remoteServiceName)));
 }
        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);
            }
        }