Example #1
0
        private void SetTemplateData_ReturnType(ApiMethodObj methodItem, MethodXmlElement xmlMethodObj, ref StringBuilder sbr_mdFileForMethod)
        {
            string returnText = TemplateConsts.TEXT_NONE;

            if (xmlMethodObj != null && !string.IsNullOrEmpty(xmlMethodObj.Returns))
            {
                returnText = xmlMethodObj.Returns;
            }
            else if (methodItem.ReturnType != null)
            {
                returnText = methodItem.ReturnType.Name;
            }

            sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_RETURN_GENERIC_RESPONSE, returnText);


            if (xmlMethodObj != null && !string.IsNullOrEmpty(xmlMethodObj.Returns_WithSuccess))
            {
                sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_SUCCESS_RESPONSE, xmlMethodObj.Returns_WithSuccess);
            }
            else
            {
                sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_SUCCESS_RESPONSE, TemplateConsts.TEXT_NONE);
            }


            if (xmlMethodObj != null && !string.IsNullOrEmpty(xmlMethodObj.Returns_WithFail))
            {
                sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_FAIL_RESPONSE, xmlMethodObj.Returns_WithFail);
            }
            else
            {
                sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_FAIL_RESPONSE, TemplateConsts.TEXT_NONE);
            }
        }
Example #2
0
 private void SetTemplateData_Example(ApiMethodObj methodItem, MethodXmlElement xmlMethodObj, ref StringBuilder sbr_mdFileForMethod)
 {
     if (xmlMethodObj != null && !string.IsNullOrEmpty(xmlMethodObj.Example))
     {
         sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_EXAMPLE, xmlMethodObj.Example);
     }
     else
     {
         sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_EXAMPLE, TemplateConsts.TEXT_NONE);
     }
 }
Example #3
0
        private void SetTemplateData_MethodParameters(ApiMethodObj methodItem, MethodXmlElement xmlMethodObj, ref StringBuilder sbr_mdFileForMethod)
        {
            StringBuilder sbr_RequiredParams = new StringBuilder();
            StringBuilder sbr_OptionalParams = new StringBuilder();
            string        fromBodyParam      = TemplateConsts.TEXT_NONE;

            foreach (var paramItem in methodItem.ParameterArray)
            {
                string paramDesc = xmlMethodObj?.ParamList.FirstOrDefault(t => t.Name == paramItem.ParamName)?.Value ?? "";

                string appendingString = $"* **{paramItem.ParamName}** [{paramItem.GetCorrectedParamTypeName()}]";

                if (paramItem.IsFromBody)
                {
                    StringBuilder sbr_FromBody = new StringBuilder();

                    sbr_FromBody.AppendLine(appendingString);
                    sbr_FromBody.AppendLine(PADDING_PARAM + paramDesc);
                    sbr_FromBody.AppendLine();

                    fromBodyParam = sbr_FromBody.ToString();

                    if (xmlMethodObj != null)
                    {
                        xmlMethodObj.DataParamFromBody = fromBodyParam;
                    }
                }
                else
                {
                    var refSbr = (paramItem.IsOptional) ? sbr_OptionalParams : sbr_RequiredParams;

                    refSbr.AppendLine(appendingString);
                    refSbr.AppendLine(PADDING_PARAM + paramDesc);
                    refSbr.AppendLine();
                }
            }

            if (sbr_OptionalParams.Length == 0)
            {
                sbr_OptionalParams.AppendLine(CONST_NotApplicable);
            }

            if (sbr_RequiredParams.Length == 0)
            {
                sbr_RequiredParams.AppendLine(CONST_NotApplicable);
            }

            sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_PARAM_LIST_REQUIRED, sbr_RequiredParams.ToString());

            sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_PARAM_LIST_OPTIONAL, sbr_OptionalParams.ToString());

            sbr_mdFileForMethod.Replace(TemplateConsts.PLACEHOLDER_PARAM_FROM_BODY, fromBodyParam);
        }
Example #4
0
        private List <ApiMethodObj> ProcessApiMethods(Type controllerItem)
        {
            List <ApiMethodObj> apiMethods = new List <ApiMethodObj>();

            //src: https://stackoverflow.com/questions/21583278/getting-all-controllers-and-actions-names-in-c-sharp

            List <MethodInfo> allPublicMethods = controllerItem.GetMethods(BindingFlags.Instance | BindingFlags.DeclaredOnly | BindingFlags.Public)
                                                 .Where(m => !m.GetCustomAttributes(typeof(System.Runtime.CompilerServices.CompilerGeneratedAttribute), true).Any()).ToList();

            foreach (MethodInfo mInfo in allPublicMethods)
            {
                try
                {
                    var m = new ApiMethodObj()
                    {
                        ControllerName = mInfo.DeclaringType.Name
                    };

                    apiMethods.Add(m);
                    try
                    { m.ReturnType = MSMethods.GetReturnType(mInfo); }
                    catch (Exception ex)
                    {
                        Console.Write(ex);
                    }

                    var _attributeCache = mInfo.GetCustomAttributes(inherit: true);

                    m.MethodName = MSMethods.GetActionName(mInfo, _attributeCache);

                    m.FullMethodName = controllerItem.FullName + "." + m.MethodName;

                    var supportedMethods = MSMethods.GetSupportedHttpMethods(mInfo, _attributeCache);
                    if (supportedMethods.Any())
                    {
                        m.SupportedHttpMethodArray.AddRange(supportedMethods);
                    }

                    Process_MethodParams(mInfo, ref m);
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }

            return(apiMethods);
        }
Example #5
0
        private void Process_MethodParams(MethodInfo mInfo, ref ApiMethodObj m)
        {
            //m.Attributes = attribs.Select(a => a.GetType().Name.Replace("Attribute", "")).ToList();

            var allParams = mInfo.GetParameters();

            if (allParams.Length == 1)
            {
                var p = GenerateApiMethodParamObj(allParams.First());

                FromBodyAttribute fromBodyAttr = (FromBodyAttribute)Attribute.GetCustomAttribute(allParams.First(), typeof(FromBodyAttribute));

                //if it is exactly one param && it has the attribute or it is complex type param then by default it is FromBody
                if (fromBodyAttr != null || !IsSimpleType(allParams.First().ParameterType))
                {
                    p.IsFromBody = true;
                }

                m.ParameterArray.Add(p);
            }
            else
            {
                //only one FromBody will receive the body params even if the attribute is put for multiple params.
                //src: https://stackoverflow.com/questions/24874490/pass-multiple-complex-objects-to-a-post-put-web-api-method
                bool isFromBodyAlreadySet = false;

                foreach (var pInfo in mInfo.GetParameters().OrderBy(p => p.Position))
                {
                    var p = GenerateApiMethodParamObj(pInfo);

                    FromBodyAttribute fromBodyAttr = (FromBodyAttribute)Attribute.GetCustomAttribute(pInfo, typeof(FromBodyAttribute));

                    if (!isFromBodyAlreadySet && fromBodyAttr != null)
                    {
                        p.IsFromBody = true;
                    }

                    m.ParameterArray.Add(p);
                }
            }
        }
Example #6
0
        public MethodXmlElement GetMemberDefinition(ApiMethodObj methodObj)
        {
            string parametersSignature = "(";

            foreach (var p in methodObj.ParameterArray)
            {
                parametersSignature += p.ParamTypeName + ",";
            }

            if (methodObj.ParameterArray.Count > 0)
            {
                parametersSignature = parametersSignature.Remove(parametersSignature.Length - 1, 1);
            }

            parametersSignature += ")";

            if (methodObj.ParameterArray.Count == 0)
            {
                parametersSignature = "";
            }

            var keyToLookfor = string.Format("M:{0}{1}", methodObj.FullMethodName, parametersSignature);

            if (!MemberXmlElementLookup.ContainsKey(keyToLookfor))
            {
                return(null);
            }

            XmlElement ele = MemberXmlElementLookup[keyToLookfor];

            MethodXmlElement memDef = new MethodXmlElement()
            {
                Name             = methodObj.MethodName,
                SourceXmlElement = ele
            };

            foreach (var c in ele.ChildNodes.Cast <XmlElement>())
            {
                try
                {
                    if (c.Name == "summary")
                    {
                        memDef.Summary = this.GetXml_Summary(c);
                    }
                    else if (c.Name == "example")
                    {
                        memDef.Example = this.HandleXml_Example(c);
                    }
                    else if (c.Name == "returns")
                    {
                        this.HandleXml_Returns(c, ref memDef);
                    }
                    else if (c.Name == "param")
                    {
                        memDef.ParamList.Add(this.GetXml_Param(c));
                    }
                }
                catch (Exception ex)
                {
                    Console.Write(ex);
                }
            }

            if (string.IsNullOrWhiteSpace(memDef.Summary))
            {
                memDef.Summary = "No information found.";
            }

            if (string.IsNullOrWhiteSpace(memDef.DataParamFromBody))
            {
                memDef.DataParamFromBody = "N/A";
            }

            return(memDef);
        }