/// <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; } }