Exemplo n.º 1
0
        /// <summary>
        /// Discover all the api endpoints in an assembly.
        /// </summary>
        /// <returns></returns>
        public ApiControllersViewModel Discover()
        {
            ApiControllersViewModel result = new ApiControllersViewModel();

            System.Type[] assemblyTypes = this.Assembly.GetTypes();

            for (long i = 0; i < assemblyTypes.LongLength; i++)
            {
                System.Type assemblyType = assemblyTypes[i];

                // get the controller base types
                if (assemblyType.BaseType == typeof(Microsoft.AspNetCore.Mvc.Controller) || assemblyType.BaseType?.BaseType == typeof(Microsoft.AspNetCore.Mvc.Controller))
                {
                    string controllerName  = string.Empty;
                    string controllerRoute = string.Empty;
                    bool   addController   = false;

                    addController = this.HasApiAttrs(assemblyType, out controllerName, out controllerRoute);

                    if (addController == true)
                    {
                        ApiControllerModel apiController = new ApiControllerModel()
                        {
                            Name = controllerName, Route = controllerRoute
                        };
                        MethodInfo[] methods = assemblyType.GetMethods();

                        // get the attributes
                        foreach (MethodInfo method in methods)
                        {
                            ApiActionModel apiActionModel = this.GetApiActionModel(method, controllerRoute);

                            if (apiActionModel != null)
                            {
                                apiController.Actions.Add(apiActionModel);
                            }
                        }

                        // Remove the route info from the route its not needed anymore.
                        if (apiController.Route.Contains("[action]"))
                        {
                            apiController.Route = apiController.Route.Replace("[action]", string.Empty);

                            if (apiController.Route.EndsWith("/"))
                            {
                                apiController.Route = apiController.Route.Substring(0, apiController.Route.Length - 1);
                            }
                        }

                        result.Controllers.Add(apiController);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Discover all the api endpoints in an assembly.
        /// </summary>
        /// <returns></returns>
        public ApiControllersViewModel Discover()
        {
            ApiControllersViewModel result = new ApiControllersViewModel();

            System.Type[] assemblyTypes = this.Assembly.GetTypes();

            for (long i = 0; i < assemblyTypes.LongLength; i++)
            {
                System.Type assemblyType = assemblyTypes[i];

                // get the controller base types
                if (assemblyType.BaseType == typeof(Microsoft.AspNetCore.Mvc.Controller))
                {
                    string controllerName  = string.Empty;
                    string controllerRoute = string.Empty;
                    bool   addController   = false;

                    addController = this.HasApiAttrs(assemblyType, out controllerName, out controllerRoute);

                    if (addController == true)
                    {
                        ApiControllerModel apiController = new ApiControllerModel()
                        {
                            Name = controllerName, Route = controllerRoute
                        };
                        MethodInfo[] methods = assemblyType.GetMethods();

                        // get the attributes
                        foreach (MethodInfo method in methods)
                        {
                            ApiActionModel apiActionModel = this.GetApiActionModel(method);

                            if (apiActionModel != null)
                            {
                                apiController.Actions.Add(apiActionModel);
                            }
                        }

                        result.Controllers.Add(apiController);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Get the action details for a controller method.
        /// </summary>
        /// <param name="method"></param>
        /// <returns></returns>
        private ApiActionModel GetApiActionModel(MethodInfo method)
        {
            ApiActionModel result = new ApiActionModel()
            {
                Signature = method.ToString(), Name = method.Name
            };
            bool addAction = false;

            // determing the method
            ApiMethod apiMethod = ApiMethod.GET; // default
            string    routeInfo = string.Empty;

            addAction = this.FindApiMethod(method, out apiMethod, out routeInfo);

            if (addAction == false)
            {
                result = null;
            }
            else
            {
                result.Method = apiMethod;
                result.Route  = routeInfo;

                // See if there is a custom default response
                Attribute methodAttr = null;
                methodAttr = method.GetCustomAttribute(typeof(ProducesResponseTypeAttribute));

                if (methodAttr != null)
                {
                    result.SucessResponseCode = Convert.ToInt32(this.GetDefaultCustomAttributeValue(method.CustomAttributes, typeof(ProducesResponseTypeAttribute)));
                }

                // Get the input parameters
                ParameterInfo[] parameters = method.GetParameters();

                foreach (ParameterInfo p in parameters)
                {
                    ApiActionParameterType parameterType = this.FindActionParameterType(p);

                    if (parameterType != ApiActionParameterType.indeterminate)
                    {
                        ApiActionParameterModel parameter = new ApiActionParameterModel()
                        {
                            Name          = p.Name,
                            Type          = p.ParameterType,
                            IsNullable    = p.IsOptional || p.ParameterType.ToString().Contains("Nullable"),
                            ParameterType = parameterType
                        };

                        if (parameterType == ApiActionParameterType.jsonBody)
                        {
                            parameter.DefaultValue = p.ParameterType.JSONExample().UnMinify();
                        }
                        else
                        {
                            parameter.DefaultValue = p.DefaultValue.ToString();
                        }

                        result.Parameters.Add(parameter);
                    }
                }

                // determine the output type.
                if (method.ReturnType.IsGenericType)
                {
                    // assuming the return type is always a single type.
                    result.ReturnType = method.ReturnType.GenericTypeArguments[0];
                }
                else
                {
                    result.ReturnType = method.ReturnType;
                }

                result.ReturnBody = result.ReturnType.JSONExample().UnMinify();
            }

            return(result);
        }