コード例 #1
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);
        }
コード例 #2
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);
        }