Example #1
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.IsNullablePrimitive(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(),
            });
        }
Example #2
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(),
            });
        }