public static CreateResourceApiOperation ( System.Web.Http.Description.ApiDescription api, |
||
api | System.Web.Http.Description.ApiDescription | Description of the api via the ApiExplorer |
docProvider | Access to the XML docs written in code | |
Результат |
private ResourceListing getDocs(HttpActionContext actionContext) { var assemblyType = (actionContext.ActionDescriptor as ReflectedHttpActionDescriptor).MethodInfo.DeclaringType; var docProvider = new XmlCommentDocumentationProvider(); //(XmlCommentDocumentationProvider)GlobalConfiguration.Configuration.Services.GetDocumentationProvider(); ResourceListing r = SwaggerGen.CreateResourceListing(actionContext); foreach (var api in GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions) { if (api.ActionDescriptor.ActionName.EndsWith("API"))//Ignore each Default API action { continue; } string apiControllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName; if (api.Route.Defaults.ContainsKey(SwaggerGen.SWAGGER) || apiControllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper())) { continue; } // Make sure we only report the current controller docs if (!apiControllerName.Equals(actionContext.ControllerContext.ControllerDescriptor.ControllerName)) { continue; } ResourceApi rApi = SwaggerGen.CreateResourceApi(api); r.apis.Add(rApi); ResourceApiOperation rApiOperation = SwaggerGen.CreateResourceApiOperation(r, api, docProvider); rApi.operations.Add(rApiOperation); foreach (var param in api.ParameterDescriptions) { ResourceApiOperationParameter parameter = SwaggerGen.CreateResourceApiOperationParameter(r, api, param, docProvider); rApiOperation.parameters.Add(parameter); } if (System.Configuration.ConfigurationManager.AppSettings["swagger:APITOKEN"] != null && System.Configuration.ConfigurationManager.AppSettings["swagger:APITOKEN"].Equals("true") && !api.ActionDescriptor.ActionName.EndsWith("API")) { //添加Token ResourceApiOperationParameter p = new ResourceApiOperationParameter(); p.name = "ApiToken"; p.description = "Api Token"; p.paramType = "path"; p.required = true; p.dataType = "String"; rApiOperation.parameters.Insert(0, p); } SwaggerGen.CreateModel(r, api, docProvider); //r.models = new ResourceApiModel(); } return(r); }
private ResourceListing getDocs(HttpActionContext actionContext) { var docProvider = (XmlCommentDocumentationProvider)GlobalConfiguration.Configuration.Services.GetDocumentationProvider(); var r = SwaggerGen.CreateResourceListing(actionContext); foreach (var grp in GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions.GroupBy(api => api.HttpMethod)) { var existing = new List <ResourceApi>(); foreach (var api in grp.OrderBy(api => api.RelativePath.Length)) { string apiControllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName; if (api.Route.Defaults.ContainsKey(SwaggerGen.SWAGGER) || apiControllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper())) { continue; } // Make sure we only report the current controller docs if (!apiControllerName.Equals(actionContext.ControllerContext.ControllerDescriptor.ControllerName)) { continue; } ResourceApi rApi = SwaggerGen.CreateResourceApi(api); if (IsDuplicate(api, rApi, existing)) { continue; } existing.Add(rApi); r.apis.Add(rApi); var rApiOperation = SwaggerGen.CreateResourceApiOperation(api, docProvider); rApi.operations.Add(rApiOperation); if (api.ActionDescriptor as ReflectedHttpActionDescriptor != null) { r.models.Add((api.ActionDescriptor as ReflectedHttpActionDescriptor).MethodInfo.ReturnType); } foreach (var param in api.ParameterDescriptions) { ResourceApiOperationParameter parameter = SwaggerGen.CreateResourceApiOperationParameter(api, param, docProvider); rApiOperation.parameters.Add(parameter); r.models.Add(param.ParameterDescriptor.ParameterType); } } } return(r); }
private ResourceListing getDocs(HttpActionContext actionContext) { var docProvider = (XmlCommentDocumentationProvider)GlobalConfiguration.Configuration.Services.GetDocumentationProvider(); ResourceListing r = SwaggerGen.CreateResourceListing(actionContext); foreach (var api in GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions) { string apiControllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName; if (api.Route.Defaults.ContainsKey(SwaggerGen.SWAGGER) || apiControllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper())) { continue; } // Make sure we only report the current controller docs if (!apiControllerName.Equals(actionContext.ControllerContext.ControllerDescriptor.ControllerName)) { continue; } if (api.ActionDescriptor.GetCustomAttributes <SwaggerIgnoreAttribute>().Any()) { continue; } if (DescriptionAuthorized(actionContext, api.ActionDescriptor)) { ResourceApi rApi = SwaggerGen.CreateResourceApi(api); r.apis.Add(rApi); ResourceApiOperation rApiOperation = SwaggerGen.CreateResourceApiOperation(api, docProvider); rApi.operations.Add(rApiOperation); foreach (var param in api.ParameterDescriptions) { ResourceApiOperationParameter parameter = SwaggerGen.CreateResourceApiOperationParameter(api, param, docProvider); rApiOperation.parameters.Add(parameter); } } } return(r); }
private ResourceListing GetDocs(HttpActionContext actionContext) { CollectApiDescriptions(); var resourceListing = SwaggerGen.CreateResourceListing(actionContext); var apis = GetApiDescriptionsByController(actionContext.ControllerContext.ControllerDescriptor.ControllerName); foreach (var api in apis) { if (ResourcesConfiguration.IsOperationMapped(api)) { var resourceApi = SwaggerGen.CreateResourceApi(api); resourceListing.AddApi(resourceApi); ResourceApiOperation resourceApiOperation = null; if (!CustomAttributeHelper.HasIgnoreAttribute(api.ActionDescriptor)) { resourceApiOperation = SwaggerGen.CreateResourceApiOperation(api, DocProvider); resourceApi.operations.Add(resourceApiOperation); } var reflectedActionDescriptor = api.ActionDescriptor as ReflectedHttpActionDescriptor; resourceListing.Models.AddRange(SwaggerGen.CreateResourceModel(reflectedActionDescriptor.MethodInfo.ReturnType, DocProvider)); foreach (var param in api.ParameterDescriptions) { if (resourceApiOperation != null) { ResourceApiOperationParameter parameter = SwaggerGen.CreateResourceApiOperationParameter(api, param, DocProvider); resourceApiOperation.parameters.Add(parameter); } resourceListing.Models.AddRange(SwaggerGen.CreateResourceModel(param, DocProvider)); } } } return(resourceListing); }
private ResourceListing getDocs(HttpActionContext actionContext) { var docProvider = (XmlCommentDocumentationProvider)GlobalConfiguration.Configuration.Services.GetDocumentationProvider(); ResourceListing r = SwaggerGen.CreateResourceListing(actionContext); dynamic modelsData = new ExpandoObject(); IDictionary <string, object> modelsDictionary = (IDictionary <string, object>)modelsData; var simpleTypes = new List <string>(); simpleTypes.Add(typeof(bool).Name); simpleTypes.Add(typeof(string).Name); simpleTypes.Add(typeof(int).Name); simpleTypes.Add(typeof(decimal).Name); simpleTypes.Add(typeof(float).Name); simpleTypes.Add(typeof(long).Name); simpleTypes.Add(typeof(byte).Name); simpleTypes.Add(typeof(uint).Name); simpleTypes.Add(typeof(double).Name); foreach (var api in GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions) { string apiControllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName; if (api.Route.Defaults.ContainsKey(SwaggerGen.SWAGGER) || apiControllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper())) { continue; } // Make sure we only report the current controller docs if (!apiControllerName.Equals(actionContext.ControllerContext.ControllerDescriptor.ControllerName)) { continue; } ResourceApi rApi = SwaggerGen.CreateResourceApi(api); r.apis.Add(rApi); ResourceApiOperation rApiOperation = SwaggerGen.CreateResourceApiOperation(api, docProvider); rApi.operations.Add(rApiOperation); foreach (var param in api.ParameterDescriptions) { if (!modelsDictionary.ContainsKey(param.ParameterDescriptor.ParameterType.Name) && !simpleTypes.Contains(param.ParameterDescriptor.ParameterType.Name)) { var props = param.ParameterDescriptor.ParameterType.GetProperties(); var m = new Model { id = param.ParameterDescriptor.ParameterType.Name, }; foreach (var propertyInfo in props) { ((IDictionary <string, object>)m.properties).Add( propertyInfo.Name, new Property() { type = propertyInfo.PropertyType.Name }); } modelsDictionary.Add(param.ParameterDescriptor.ParameterType.Name, m); } ResourceApiOperationParameter parameter = SwaggerGen.CreateResourceApiOperationParameter(api, param, docProvider); var type = param.ParameterDescriptor.ParameterType; rApiOperation.parameters.Add(parameter); } r.models = modelsData; } return(r); }
private ResourceListing getDocs(HttpActionContext actionContext) { var dataTypeRegistry = new DataTypeRegistry(null, null, null); string namespaceDescriptor = actionContext.ControllerContext.RouteData.Values["namespaceDescriptor"].ToString(); string moduleName = actionContext.ControllerContext.RouteData.Values["moduleName"].ToString(); string controllerDisplay = actionContext.ControllerContext.RouteData.Values["controllerDisplay"].ToString(); var docProvider = (XmlCommentDocumentationProvider)GlobalConfiguration.Configuration.Services.GetDocumentationProvider(); ResourceListing r = SwaggerGen.CreateResourceListing(actionContext); r.basePath = r.basePath; //#if DEBUG // r.basePath = r.basePath + "/nexso/"; //#endif r.resourcePath = controllerDisplay; var list = GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions; //var list = WebApiExtensions.GetAllApiDescriptions(GlobalConfiguration.Configuration.Services.GetApiExplorer(), actionContext.ControllerContext.Configuration); foreach (var api in list) { string apiControllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName; if (api.Route.Defaults.ContainsKey(SwaggerGen.SWAGGER) || apiControllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper())) { continue; } // var descriptor = actionContext.ControllerContext.RouteData.Route.GetRouteData(""]; // //Make sure we only report the current controller docs //if (!apiControllerName.Equals(actionContext.ControllerContext.ControllerDescriptor.ControllerName)) // continue; if (!api.RelativePath.ToUpper().Contains((moduleName + "/API/" + namespaceDescriptor + "." + controllerDisplay + "controller").ToUpper())) { continue; } ResourceApi rApi = SwaggerGen.CreateResourceApi(api); rApi.path = "/" + api.RelativePath.ToLower().Replace((namespaceDescriptor + ".").ToLower(), "").Replace("controller", ""); r.apis.Add(rApi); ResourceApiOperation rApiOperation = SwaggerGen.CreateResourceApiOperation(api, docProvider, dataTypeRegistry); rApi.operations.Add(rApiOperation); foreach (var param in api.ParameterDescriptions) { DataType dataType = dataTypeRegistry.GetOrRegister(param.ParameterDescriptor.ParameterType); ResourceApiOperationParameter parameter = SwaggerGen.CreateResourceApiOperationParameter(api, param, docProvider); rApiOperation.parameters.Add(parameter); } var responses = docProvider.GetResponseMessages(api.ActionDescriptor); rApiOperation.responseMessages = SwaggerGen.GetResourceApiResponseMessage(api, docProvider); dataTypeRegistry.GetOrRegister(api.ActionDescriptor.ReturnType); } r.models = dataTypeRegistry.GetModels(); return(r); }