Exemplo n.º 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);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates an operation parameter
        /// </summary>
        /// <param name="api">Description of the api via the ApiExplorer</param>
        /// <param name="param">Description of a parameter on an operation via the ApiExplorer</param>
        /// <param name="docProvider">Access to the XML docs written in code</param>
        /// <returns>An operation parameter</returns>
        public static ResourceApiOperationParameter CreateResourceApiOperationParameter(ResourceListing r, ApiDescription api, ApiParameterDescription param, XmlCommentDocumentationProvider docProvider)
        {
            string paramType = (param.Source.ToString().Equals(FROMURI)) ? QUERY : BODY;
            var    dataType  = param.ParameterDescriptor.ParameterType.Name;

            switch (dataType)
            {
            case "List`1":
                var dataObjType = param.ParameterDescriptor.ParameterType.GetGenericArguments()[0];
                dataType = string.Format("Array[{0}]", dataObjType.Name);
                break;

            case "Nullable`1":
                var dd = param.ParameterDescriptor.ParameterType.GetGenericArguments()[0];
                dataType = dd.Name;
                break;

            case "Dictionary`2":

                dataType = string.Format("Array[{0},{1}]", param.ParameterDescriptor.ParameterType.GetGenericArguments()[0].Name,
                                         param.ParameterDescriptor.ParameterType.GetGenericArguments()[1].Name);
                break;

            default:
                if (!IsExceptType(param.ParameterDescriptor.ParameterType))
                {
                    AddModelDataObject(r, param.ParameterDescriptor.ParameterType);
                }
                break;
            }
            ResourceApiOperationParameter parameter = new ResourceApiOperationParameter()
            {
                paramType   = (paramType == "query" && api.RelativePath.IndexOf("{" + param.Name + "}") > -1) ? PATH : paramType,
                name        = param.Name,
                description = param.Name.Equals("sessionKey") ? "Login session" : (string.IsNullOrWhiteSpace(param.Documentation) ? NODESCRIPTION : param.Documentation),
                dataType    = dataType,
                required    = docProvider.GetRequired(param.ParameterDescriptor)
            };

            return(parameter);
        }