コード例 #1
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);
        }
コード例 #2
0
        /// <summary>
        /// Creates an api operation
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An api operation</returns>
        public static ResourceApiOperation CreateResourceApiOperation(ApiDescription api, XmlCommentDocumentationProvider docProvider)
        {
            ResourceApiOperation rApiOperation = new ResourceApiOperation()
            {
                httpMethod    = api.HttpMethod.ToString(),
                nickname      = docProvider.GetNickname(api.ActionDescriptor),
                responseClass = docProvider.GetResponseClass(api.ActionDescriptor),
                summary       = api.Documentation,
                notes         = docProvider.GetNotes(api.ActionDescriptor),
                parameters    = new List <ResourceApiOperationParameter>()
            };

            return(rApiOperation);
        }
コード例 #3
0
        /// <summary>
        /// Creates an api operation
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An api operation</returns>
        public static ResourceApiOperation CreateResourceApiOperation(ResourceListing r, ApiDescription api, XmlCommentDocumentationProvider docProvider)
        {
            ResourceApiOperation rApiOperation = new ResourceApiOperation()
            {
                httpMethod    = api.HttpMethod.ToString(),
                nickname      = docProvider.GetNickname(api.ActionDescriptor),
                responseClass = docProvider.GetResponseClass(api.ActionDescriptor),
                summary       = docProvider.GetDocumentation(api.ActionDescriptor),
                notes         = docProvider.GetNotes(api.ActionDescriptor),
                parameters    = new List <ResourceApiOperationParameter>(),
            };

            if (string.IsNullOrEmpty(rApiOperation.notes) || rApiOperation.notes.Equals("No Documentation Found."))
            {
                rApiOperation.notes = rApiOperation.summary;
            }
            return(rApiOperation);
        }
コード例 #4
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);
        }
コード例 #5
0
        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);
        }
コード例 #6
0
ファイル: SwaggerModels.cs プロジェクト: OOcm1987/Nexso
        /// <summary>
        /// Creates an api operation
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An api operation</returns>
        public static ResourceApiOperation CreateResourceApiOperation(ApiDescription api, XmlCommentDocumentationProvider docProvider, DataTypeRegistry dataTypeRegistry)
        {
            ResourceApiOperation rApiOperation = new ResourceApiOperation()
            {
                httpMethod       = api.HttpMethod.ToString(),
                nickname         = docProvider.GetNickname(api.ActionDescriptor),
                responseClass    = docProvider.GetResponseClass(api.ActionDescriptor),
                summary          = api.Documentation,
                notes            = docProvider.GetNotes(api.ActionDescriptor),
                type             = docProvider.GetResponseClass(api.ActionDescriptor),
                parameters       = new List <ResourceApiOperationParameter>(),
                responseMessages = new List <ResourceApiOperationResponseMessage>(),
            };

            var responseType = api.ActualResponseType();

            if (responseType == null)
            {
                rApiOperation.type = "void";
            }
            else
            {
                var dataType = dataTypeRegistry.GetOrRegister(responseType);
                if (dataType.Type == "object")
                {
                    rApiOperation.type = dataType.Id;
                }
                else
                {
                    rApiOperation.type   = dataType.Type;
                    rApiOperation.format = dataType.Format;
                    rApiOperation.items  = dataType.Items;
                    rApiOperation.Enum   = dataType.Enum;
                }
            }

            return(rApiOperation);
        }
コード例 #7
0
ファイル: SwaggerModels.cs プロジェクト: WalterZC/Swagger.Net
        /// <summary>
        /// Creates an api operation
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An api operation</returns>
        public static ResourceApiOperation CreateResourceApiOperation(ApiDescription api, XmlCommentDocumentationProvider docProvider)
        {
            ResponseMeta responseMeta = docProvider.GetResponseType(api.ActionDescriptor);
            SwaggerType  swaggerType  = Helper.GetSwaggerType(responseMeta.Type);


            ResourceApiOperation rApiOperation = new ResourceApiOperation
            {
                httpMethod       = api.HttpMethod.ToString(),
                nickname         = docProvider.GetNickname(api.ActionDescriptor),
                items            = swaggerType.Items,
                type             = swaggerType.Name,
                summary          = api.Documentation,
                notes            = docProvider.GetNotes(api.ActionDescriptor),
                parameters       = new List <ResourceApiOperationParameter>(),
                responseMessages = docProvider.GetResponseCodes(api.ActionDescriptor)
            };

            var typesToReturn = new ConcurrentDictionary <string, string>();

            Helper.TryToAddModels(models, responseMeta.Type, docProvider, typesToReturn);
            return(rApiOperation);
        }
コード例 #8
0
        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);
        }
コード例 #9
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);
        }
コード例 #10
0
ファイル: SwaggerModels.cs プロジェクト: DveMac/Swagger.Net
        /// <summary>
        /// Creates an api operation
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An api operation</returns>
        public static ResourceApiOperation CreateResourceApiOperation(ApiDescription api, XmlCommentDocumentationProvider docProvider)
        {
            ResourceApiOperation rApiOperation = new ResourceApiOperation()
            {
                httpMethod = api.HttpMethod.ToString(),
                nickname = docProvider.GetNickname(api.ActionDescriptor),
                responseClass = docProvider.GetResponseClass(api.ActionDescriptor),
                summary = api.Documentation,
                notes = docProvider.GetNotes(api.ActionDescriptor),
                parameters = new List<ResourceApiOperationParameter>()
            };

            return rApiOperation;
        }
コード例 #11
0
ファイル: SwaggerModels.cs プロジェクト: Jaryli/Swagger.Net
        /// <summary>
        /// Creates an api operation
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An api operation</returns>
        public static ResourceApiOperation CreateResourceApiOperation(ApiDescription api, XmlCommentDocumentationProvider docProvider)
        {
            ResponseMeta responseMeta = docProvider.GetResponseType(api.ActionDescriptor);
            SwaggerType swaggerType = Helper.GetSwaggerType(responseMeta.Type);


            ResourceApiOperation rApiOperation = new ResourceApiOperation
            {
                httpMethod = api.HttpMethod.ToString(),
                nickname = docProvider.GetNickname(api.ActionDescriptor),
                items = swaggerType.Items,
                type = swaggerType.Name,
                summary = api.Documentation,
                notes = docProvider.GetNotes(api.ActionDescriptor),
                parameters = new List<ResourceApiOperationParameter>(),
                responseMessages = docProvider.GetResponseCodes(api.ActionDescriptor)
            };

            var typesToReturn = new ConcurrentDictionary<string, string>();
            Helper.TryToAddModels(models, responseMeta.Type, docProvider, typesToReturn);
            return rApiOperation;
        }
コード例 #12
0
        /// <summary>
        /// Creates an api operation
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An api operation</returns>
        public static ResourceApiOperation CreateResourceApiOperation(ApiDescription api,
            XmlCommentDocumentationProvider docProvider, HttpControllerDescriptor controllerDescriptor)

        {
            var parts = docProvider.GetNotes(api.ActionDescriptor).Split(new string[] { "schema=" }, StringSplitOptions.None);
            ReflectedHttpActionDescriptor actionDescriptor = (api.ActionDescriptor as ReflectedHttpActionDescriptor);
            string _attributes = "";
            _attributes += GetCustomAttributesAsString(actionDescriptor.MethodInfo.GetCustomAttributes(true));
            _attributes += GetCustomAttributesAsString(controllerDescriptor.ControllerType.GetCustomAttributes(true));
            
            ResourceApiOperation rApiOperation = new ResourceApiOperation()
            {
                httpMethod = api.HttpMethod.ToString(),
                nickname = docProvider.GetNickname(api.ActionDescriptor),
                responseClass = docProvider.GetResponseClass(api.ActionDescriptor),
                summary = api.Documentation+" "+_attributes,
                notes = parts[0],
                schema = parts.Length > 1 ? parts[1] : "",
                parameters = new List<ResourceApiOperationParameter>(),
            };

            return rApiOperation;
        }