Esempio n. 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 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);
        }
        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);
        }