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(), }); }
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(), }); }
/// <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; } }
/// <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; } }