예제 #1
0
        /// <summary>
        /// Get the resource description of the api for swagger documentation
        /// </summary>
        /// <remarks>It is very convenient to have this information available for generating clients. This is the entry point for the swagger UI
        /// </remarks>
        /// <returns>JSON document representing structure of API</returns>
        public HttpResponseMessage Get()
        {
            var docProvider = (XmlCommentDocumentationProvider)GlobalConfiguration.Configuration.Services.GetDocumentationProvider();

            ResourceListing r = SwaggerGen.CreateResourceListing(ControllerContext);
            List <string>   uniqueControllers = new List <string>();

            foreach (var api in GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions.Where(api => api.Route.RouteTemplate.Contains("api")))
            {
                string controllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName;
                if (uniqueControllers.Contains(controllerName) ||
                    controllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper()))
                {
                    continue;
                }

                uniqueControllers.Add(controllerName);

                ResourceApi rApi = SwaggerGen.CreateResourceApi(api);
                r.apis.Add(rApi);
            }

            HttpResponseMessage resp = new HttpResponseMessage();

            resp.Content = new ObjectContent <ResourceListing>(r, ControllerContext.Configuration.Formatters.JsonFormatter);

            return(resp);
        }
예제 #2
0
        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 XPathNavigator GetTypeNode(Type type)
        {
            string controllerTypeName = GetTypeName(type);
            string selectExpression   = String.Format(CultureInfo.InvariantCulture, TypeExpression, controllerTypeName);
            var    doc = SwaggerGen.GetXPathDocument(type);

            return(doc == null ? null : doc.SelectSingleNode(selectExpression));
        }
예제 #4
0
        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);
        }
예제 #5
0
        /// <summary>
        /// Get the resource description of the api for swagger documentation
        /// </summary>
        /// <remarks>It is very convenient to have this information available for generating clients. This is the entry point for the swagger UI
        /// </remarks>
        /// <returns>JSON document representing structure of API</returns>
        public HttpResponseMessage Get()
        {
            //var formatter = RequestContext.Configuration.Formatters.JsonFormatter;
            //formatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            var docProvider = (XmlCommentDocumentationProvider)GlobalConfiguration.Configuration.Services.GetDocumentationProvider();

            ResourceListing r = SwaggerGen.CreateResourceListing(ControllerContext);
            List <string>   uniqueControllers = new List <string>();

            foreach (var api in GlobalConfiguration.Configuration.Services.GetApiExplorer().ApiDescriptions)
            {
                if (!CustomAttributeHelper.HasIgnoreAttribute(api.ActionDescriptor.ControllerDescriptor) &&
                    ResourcesConfiguration.IsResourceMapped(api.ActionDescriptor.ControllerDescriptor))
                {
                    string controllerName = api.ActionDescriptor.ControllerDescriptor.ControllerName;
                    if (uniqueControllers.Contains(controllerName) ||
                        controllerName.ToUpper().Equals(SwaggerGen.SWAGGER.ToUpper()))
                    {
                        continue;
                    }

                    uniqueControllers.Add(controllerName);

                    ResourceApi rApi       = SwaggerGen.CreateResourceApi(api);
                    var         queryIndex = rApi.path.IndexOf("?");

                    if (queryIndex > -1)
                    {
                        rApi.path = rApi.path.Substring(0, queryIndex);
                    }

                    r.AddApi(rApi);

                    // Model
                    foreach (var param in api.ParameterDescriptions)
                    {
                        r.Models.AddRange(SwaggerGen.CreateResourceModel(param, docProvider));
                    }
                }
            }

            HttpResponseMessage resp = new HttpResponseMessage();

            resp.Content = new ObjectContent <ResourceListing>(r, ControllerContext.Configuration.Formatters.JsonFormatter);

            return(resp);
        }
예제 #6
0
        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 XPathNavigator GetMemberNode(HttpActionDescriptor actionDescriptor)
        {
            ReflectedHttpActionDescriptor reflectedActionDescriptor = actionDescriptor as ReflectedHttpActionDescriptor;

            if (reflectedActionDescriptor != null)
            {
                string selectExpression = string.Format(_methodExpression, GetMemberName(reflectedActionDescriptor.MethodInfo));
                var    doc = SwaggerGen.GetXPathDocument(reflectedActionDescriptor.MethodInfo.DeclaringType);
                if (doc != null)
                {
                    XPathNavigator node = doc.SelectSingleNode(selectExpression);
                    if (node != null)
                    {
                        return(node);
                    }
                }
            }

            return(null);
        }
        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);
        }
예제 #10
0
        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);
        }