コード例 #1
0
        public void CreateCodeDom(Assembly assembly, CherryPickingMethods methods, DocCommentLookup docLookup)
        {
            this.docLookup = docLookup;
            var cherryTypes = PodGenHelper.GetCherryTypes(assembly, methods);

            CreateCodeDom(cherryTypes, methods);
        }
コード例 #2
0
        public static WebApiDescription GetWebApiDescription(ApiDescription description)
        {
            if (description == null)
            {
                throw new ArgumentNullException(nameof(description));
            }

            var xmlFilePath    = DocCommentLookup.GetXmlPath(description.ActionDescriptor.ControllerDescriptor.ControllerType.Assembly);
            var docLookup      = DocCommentLookup.Create(xmlFilePath);
            var methodComments = docLookup == null ? null : GetMethodDocComment(docLookup, description.ActionDescriptor);
            var actionName     = description.ActionDescriptor.ActionName;
            var controllerName = description.ActionDescriptor.ControllerDescriptor.ControllerName;

            return(new WebApiDescription(description.ID)
            {
                ActionDescriptor = new ActionDescriptor()
                {
                    ActionName = actionName,
                    ReturnType = description.ResponseDescription?.ResponseType ?? description.ActionDescriptor.ReturnType,                    //for complex types
                    ControllerDescriptor = new ControllerDescriptor()
                    {
                        ControllerName = controllerName,
                        ControllerType = description.ActionDescriptor.ControllerDescriptor.ControllerType,
                    }
                },

                HttpMethod = description.HttpMethod.Method,
                Documentation = DocCommentHelper.GetSummary(methodComments),
                RelativePath = description.RelativePath,
                ResponseDescription = new ResponseDescription()
                {
                    Documentation = DocCommentHelper.GetReturnComment(methodComments),
                    ResponseType = description.ResponseDescription.ResponseType,
                    DeclaredType = description.ResponseDescription.DeclaredType,
                },

                ParameterDescriptions = description.ParameterDescriptions.Select(d =>
                {
                    var parameterBinder = GetParameterBinder(d.ParameterDescriptor.ParameterBinderAttribute);
                    var parameterType = d.ParameterDescriptor.ParameterType;
                    var parameterName = d.ParameterDescriptor.ParameterName;
                    if ((parameterBinder == ParameterBinder.FromQuery || parameterBinder == ParameterBinder.FromUri) && !TypeHelper.IsValueType(parameterType) && !TypeHelper.IsNullablePremitive(parameterType))
                    {
                        throw new ArgumentException($"Not support ParameterBinder {parameterBinder} with a class parameter {parameterName}:{parameterType.ToString()} in {controllerName}/{actionName}.");
                    }

                    return new ParameterDescription()
                    {
                        Documentation = DocCommentHelper.GetParameterComment(methodComments, d.Name),
                        Name = d.Name,
                        ParameterDescriptor = new ParameterDescriptor()
                        {
                            ParameterName = parameterName,
                            ParameterType = parameterType,
                            ParameterBinder = parameterBinder,
                        }
                    };
                }).ToArray(),
            });
        }
コード例 #3
0
ファイル: Poco2CsGen.cs プロジェクト: imatary/webapiclientgen
        public void CreateCodeDom(Assembly assembly, CherryPickingMethods methods, DocCommentLookup docLookup, ModelGenOutputs codeGenOutputs)
        {
            this.docLookup = docLookup;
            this.settings  = codeGenOutputs;
            var cherryTypes = PodGenHelper.GetCherryTypes(assembly, methods);

            CreateCodeDom(cherryTypes, methods, settings.CSClientNamespaceSuffix);
        }
コード例 #4
0
        public void CreateCodeDom(Assembly assembly, CherryPickingMethods methods, DocCommentLookup docLookup, string clientNamespaceSuffix)
        {
            this.docLookup             = docLookup;
            this.clientNamespaceSuffix = clientNamespaceSuffix;
            var cherryTypes = PodGenHelper.GetCherryTypes(assembly, methods);

            CreateCodeDom(cherryTypes, methods, clientNamespaceSuffix);
        }
コード例 #5
0
        public void CreateCodeDom(Assembly assembly, CherryPickingMethods methods, DocCommentLookup docLookup, string clientNamespaceSuffix, bool dataAnnotationsEnabled, bool dataAnnotationsToComments)
        {
            this.docLookup                 = docLookup;
            this.clientNamespaceSuffix     = clientNamespaceSuffix;
            this.dataAnnotationsEnabled    = dataAnnotationsEnabled;
            this.dataAnnotationsToComments = dataAnnotationsToComments;
            var cherryTypes = PodGenHelper.GetCherryTypes(assembly, methods);

            CreateCodeDom(cherryTypes, methods, clientNamespaceSuffix);
        }
コード例 #6
0
        public void TestReadDocComment()
        {
            var d = DocCommentLookup.Create(@"C:\VsProjects\webapiclientgen\DemoWebApi\App_Data\xmlDocument.xml");

            Assert.NotNull(d);
            Assert.Equal("DemoWebApi", d.XmlDoc.assembly.name);
            var summary = d.GetMember("T:DemoWebApi.Areas.HelpPage.HelpPageConfig").summary;

            const string expected = "\n            Use this class to customize the Help Page.\n            For example you can set a custom ";

            Assert.Equal(expected, summary.Text[0]);
        }
コード例 #7
0
        static docMember GetMethodDocComment(DocCommentLookup lookup, System.Web.Http.Controllers.HttpActionDescriptor descriptor)
        {
            var methodFullName = descriptor.ControllerDescriptor.ControllerType.FullName + "." + descriptor.ActionName;
            var parameters     = descriptor.GetParameters();

            if (parameters.Count > 0)
            {
                methodFullName += "(" + parameters.Select(d => d.ParameterType.FullName).Aggregate((c, n) => c + "," + n) + ")";
            }

            return(lookup.GetMember("M:" + methodFullName));
        }
コード例 #8
0
        static docMember GetMethodDocComment(DocCommentLookup lookup, Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor descriptor)
        {
            var methodInfo     = descriptor.MethodInfo;
            var methodFullName = methodInfo.DeclaringType.FullName + "." + methodInfo.Name;

            if (descriptor.Parameters.Count > 0)
            {
                methodFullName += "(" + descriptor.Parameters.Select(d => d.ParameterType.FullName).Aggregate((c, n) => c + "," + n) + ")";
            }

            return(lookup.GetMember("M:" + methodFullName));
        }
コード例 #9
0
        public void TestReadDocComment()
        {
            var d = DocCommentLookup.Create(@"C:\VsProjects\OpenSource\webapiclientgen\DemoCoreWeb\bin\Debug\net5.0\DemoCoreWeb.xml");

            Assert.NotNull(d);
            Assert.Equal("DemoCoreWeb", d.XmlDoc.assembly.name);
            var summary = d.GetMember("T:DemoWebApi.Controllers.HeroesController").summary;

            const string expected = "\n            Heroes operations\n            ";

            Assert.Equal(expected, summary.Text[0]);
        }
コード例 #10
0
        public static WebApiDescription GetWebApiDescription(ApiDescription description)
        {
            var xmlFilePath    = DocCommentLookup.GetXmlPath(description.ActionDescriptor.ControllerDescriptor.ControllerType.Assembly);
            var docLookup      = DocCommentLookup.Create(xmlFilePath);
            var methodComments = docLookup == null ? null : GetMethodDocComment(docLookup, description.ActionDescriptor);

            return(new WebApiDescription(description.ID)
            {
                ActionDescriptor = new ActionDescriptor()
                {
                    ActionName = description.ActionDescriptor.ActionName,
                    ReturnType = description.ResponseDescription?.ResponseType ?? description.ActionDescriptor.ReturnType,//for complex types
                    ControllerDescriptor = new ControllerDescriptor()
                    {
                        ControllerName = description.ActionDescriptor.ControllerDescriptor.ControllerName,
                        ControllerType = description.ActionDescriptor.ControllerDescriptor.ControllerType,
                    }
                },

                HttpMethod = description.HttpMethod.Method,
                Documentation = DocCommentHelper.GetSummary(methodComments),
                RelativePath = description.RelativePath,
                ResponseDescription = new ResponseDescription()
                {
                    Documentation = DocCommentHelper.GetReturnComment(methodComments),
                    ResponseType = description.ResponseDescription.ResponseType,
                    DeclaredType = description.ResponseDescription.DeclaredType,
                },

                ParameterDescriptions = description.ParameterDescriptions.Select(d => new ParameterDescription()
                {
                    Documentation = DocCommentHelper.GetParameterComment(methodComments, d.Name),
                    Name = d.Name,
                    ParameterDescriptor = new ParameterDescriptor()
                    {
                        ParameterName = d.ParameterDescriptor.ParameterName,
                        ParameterType = d.ParameterDescriptor.ParameterType,
                        ParameterBinder = GetParameterBinder(d.ParameterDescriptor.ParameterBinderAttribute),
                    }
                }).ToArray(),
            });
        }
コード例 #11
0
        public void TestReadNotExist()
        {
            var r = DocCommentLookup.Create(@"C:\VsProjects\webapiclientgen\NotExist\App_Data\xmlDocument.xml");

            Assert.Null(r);
        }
コード例 #12
0
        /// <summary>
        /// Translate ApiDescription of the Framework to my own WebApiDescription
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        public static WebApiDescription GetWebApiDescription(ApiDescription description)
        {
            var controllerActionDescriptor = description.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;

            if (controllerActionDescriptor == null)
            {
                return(null);
            }

            try
            {
                Type responseType;
                if (description.SupportedResponseTypes.Count > 0)
                {
                    if (description.SupportedResponseTypes[0].Type.Equals(typeof(void)))
                    {
                        responseType = null;
                    }
                    else
                    {
                        responseType = description.SupportedResponseTypes[0].Type;
                    }
                }
                else
                {
                    Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor actionDescriptor = description.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;
                    Debug.Assert(actionDescriptor != null);
                    responseType = actionDescriptor.MethodInfo.ReturnType;// in .net core 2.1, IActionResult is not in SupportedResponseTypes anymore, so I have to get it here.
                    if (responseType.Equals(typeof(void)))
                    {
                        responseType = null;
                    }
                }

                var xmlFilePath    = DocComment.DocCommentLookup.GetXmlPath(controllerActionDescriptor.MethodInfo.DeclaringType.Assembly);
                var docLookup      = DocCommentLookup.Create(xmlFilePath);
                var methodComments = docLookup == null ? null : GetMethodDocComment(docLookup, controllerActionDescriptor);

                var dr = new WebApiDescription(description.ActionDescriptor.Id)
                {
                    ActionDescriptor = new ActionDescriptor()
                    {
                        ActionName           = controllerActionDescriptor.ActionName,
                        ReturnType           = responseType,
                        ControllerDescriptor = new ControllerDescriptor()
                        {
                            ControllerName = controllerActionDescriptor.ControllerName,
                            ControllerType = controllerActionDescriptor.ControllerTypeInfo.AsType()
                        }
                    },

                    HttpMethod          = description.HttpMethod,
                    Documentation       = DocCommentHelper.GetSummary(methodComments),
                    RelativePath        = description.RelativePath + BuildQuery(description.ParameterDescriptions),
                    ResponseDescription = new ResponseDescription()
                    {
                        Documentation = DocCommentHelper.GetReturnComment(methodComments),
                        ResponseType  = responseType,
                    },

                    ParameterDescriptions = description.ParameterDescriptions.Select(d =>
                    {
                        var parameterBinder = GetParameterBinder(d.Source);
                        var parameterType   = d.ParameterDescriptor.ParameterType;
                        if ((parameterBinder == ParameterBinder.FromQuery || parameterBinder == ParameterBinder.FromUri) && !TypeHelper.IsValueType(parameterType) && !TypeHelper.IsNullablePremitive(parameterType))
                        {
                            throw new ArgumentException($"Not support ParameterBinder {parameterBinder} with a class parameter {parameterType.ToString()}.");
                        }

                        return(new ParameterDescription()
                        {
                            Documentation = DocCommentHelper.GetParameterComment(methodComments, d.Name),
                            Name = d.Name,
                            ParameterDescriptor = new ParameterDescriptor()
                            {
                                ParameterName = d.ParameterDescriptor.Name,
                                ParameterType = parameterType,
                                ParameterBinder = parameterBinder,
                            }
                        });
                    }).ToArray(),
                };

                return(dr);
            }
            catch (ArgumentException ex)//Expected to be thrown from GetParameterBinder()
            {
                var msg      = ex.Message;
                var errorMsg = $"Web API {controllerActionDescriptor.ControllerName}/{controllerActionDescriptor.ActionName} is defined with invalid parameters: {msg}";
                Trace.TraceError(errorMsg);
                throw new ArgumentException(errorMsg);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                throw;
            }
        }
コード例 #13
0
        /// <summary>
        /// Translate ApiDescription of the Framework to my own WebApiDescription
        /// </summary>
        /// <param name="description"></param>
        /// <returns></returns>
        public static WebApiDescription GetWebApiDescription(ApiDescription description)
        {
            var controllerActionDescriptor = description.ActionDescriptor as Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor;

            if (controllerActionDescriptor == null)
            {
                return(null);
            }

            try
            {
                Type responseType;
                if (description.SupportedResponseTypes.Count > 0)
                {
                    if (description.SupportedResponseTypes[0].Type.Equals(typeof(void)))
                    {
                        responseType = null;
                    }
                    else
                    {
                        responseType = description.SupportedResponseTypes[0].Type;
                    }
                }
                else
                {
                    responseType = null;
                }

                var xmlFilePath    = DocComment.DocCommentLookup.GetXmlPath(controllerActionDescriptor.MethodInfo.DeclaringType.Assembly);
                var docLookup      = DocCommentLookup.Create(xmlFilePath);
                var methodComments = docLookup == null ? null : GetMethodDocComment(docLookup, controllerActionDescriptor);

                var dr = new WebApiDescription(description.ActionDescriptor.Id)
                {
                    ActionDescriptor = new ActionDescriptor()
                    {
                        ActionName           = controllerActionDescriptor.ActionName,
                        ReturnType           = responseType,
                        ControllerDescriptor = new ControllerDescriptor()
                        {
                            ControllerName = controllerActionDescriptor.ControllerName,
                            ControllerType = controllerActionDescriptor.ControllerTypeInfo.AsType()
                        }
                    },

                    HttpMethod          = description.HttpMethod,
                    Documentation       = DocCommentHelper.GetSummary(methodComments),
                    RelativePath        = description.RelativePath + BuildQuery(description.ParameterDescriptions),
                    ResponseDescription = new ResponseDescription()
                    {
                        Documentation = DocCommentHelper.GetReturnComment(methodComments),
                        ResponseType  = responseType,
                        // DeclaredType = description.ResponseDescription.DeclaredType,
                    },

                    ParameterDescriptions = description.ParameterDescriptions.Select(d => new ParameterDescription()
                    {
                        Documentation       = DocCommentHelper.GetParameterComment(methodComments, d.Name),
                        Name                = d.Name,
                        ParameterDescriptor = new ParameterDescriptor()
                        {
                            ParameterName   = d.ParameterDescriptor.Name,
                            ParameterType   = d.ParameterDescriptor.ParameterType,
                            ParameterBinder = GetParameterBinder(d.Source),                            //.ParameterBinderAttribute),
                        }
                    }).ToArray(),
                };

                return(dr);
            }
            catch (Exception ex)
            {
                Trace.TraceError(ex.ToString());
                throw;
            }
        }