From a5957935a41067573fd6b983cd6bee678a957636 Mon Sep 17 00:00:00 2001 From: Dan Schlossberg Date: Sat, 10 Nov 2012 19:18:44 -0500 Subject: [PATCH 1/5] new file --- .../Factories/ResourceListingFactory.cs | 57 +++++++++++++++++++ Swagger.Net/Swagger.Net.csproj | 2 + 2 files changed, 59 insertions(+) create mode 100644 Swagger.Net/Factories/ResourceListingFactory.cs diff --git a/Swagger.Net/Factories/ResourceListingFactory.cs b/Swagger.Net/Factories/ResourceListingFactory.cs new file mode 100644 index 0000000..4bca57a --- /dev/null +++ b/Swagger.Net/Factories/ResourceListingFactory.cs @@ -0,0 +1,57 @@ +using System; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Text; +using System.Web.Http.Description; +using Swagger.Net.Models; + +namespace Swagger.Net.Factories +{ + public class ResourceListingFactory + { + public ResourceListing Create(SwaggerConfiguration config) + { + var apis = GetApis(config.ApiDescriptions, config.DocumentControllerAlias); + var resourceListing = new ResourceListing + { + apiVersion = config.ApiVersion, + swaggerVersion = config.SwaggerVersion, + basePath = config.HttpContext.Request.Url.AbsoluteUri, + apis = apis + }; + return resourceListing; + } + + public IEnumerable GetApis(Collection apiDescriptions, string documentControllerAlias) + { + var uniqueControllers = apiDescriptions + .Select(api => api.ActionDescriptor.ControllerDescriptor.ControllerName) + .Distinct(); + + var rtn = new List(); + + foreach (var controllerName in uniqueControllers) + { + // todo: get real docs from Documentation Provider. + string name = controllerName; + var desc = apiDescriptions.Where(api => api.ActionDescriptor.ControllerDescriptor.ControllerName == name).Select(api => api.Documentation).FirstOrDefault(); + + var listingItem = CreateListingApi(name, desc, documentControllerAlias); + rtn.Add(listingItem); + } + + return rtn; + } + + public ResourceListing.ResourceListingApi CreateListingApi(string name, string description, string documentControllerAlias) + { + return new ResourceListing.ResourceListingApi + { + path = "/" + name, + description = description + }; + + } + } +} diff --git a/Swagger.Net/Swagger.Net.csproj b/Swagger.Net/Swagger.Net.csproj index 52e70fa..27bcf49 100644 --- a/Swagger.Net/Swagger.Net.csproj +++ b/Swagger.Net/Swagger.Net.csproj @@ -38,6 +38,7 @@ + @@ -53,6 +54,7 @@ + From ae552f810b65b7e0c70bcd6b74b88fd6149b8a1d Mon Sep 17 00:00:00 2001 From: Dan Schlossberg Date: Sat, 10 Nov 2012 19:19:56 -0500 Subject: [PATCH 2/5] Reverted to "api/{id}" to make things easier --- Swagger.Net.WebAPI/App_Start/SwaggerNet.cs | 21 ++++++++++----------- Swagger.Net.WebAPI/Views/Home/Index.cshtml | 2 +- Swagger.Net.WebAPI/docs/index.html | 2 +- 3 files changed, 12 insertions(+), 13 deletions(-) diff --git a/Swagger.Net.WebAPI/App_Start/SwaggerNet.cs b/Swagger.Net.WebAPI/App_Start/SwaggerNet.cs index 82043f3..75d988a 100644 --- a/Swagger.Net.WebAPI/App_Start/SwaggerNet.cs +++ b/Swagger.Net.WebAPI/App_Start/SwaggerNet.cs @@ -14,18 +14,17 @@ public static class SwaggerNet { public static void PreStart() { - RouteTable.Routes.MapHttpRoute( - name: "SwaggerResourceList", - routeTemplate: "apidocs/swagger", - defaults: new { swagger = true, controller = "Swagger", action = "GetResourceList" } - ); - RouteTable.Routes.MapHttpRoute( - name: "SwaggerApiDeclaration", - routeTemplate: "apidocs/{controllerName}", - defaults: new { swagger = true, controller = "Swagger", action = "GetApiDeclaration" } - ); + //RouteTable.Routes.MapHttpRoute( + // name: "SwaggerResourceList", + // routeTemplate: "api/swagger", + // defaults: new { swagger = true, controller = "Swagger", action = "GetResourceList" } + //); + //RouteTable.Routes.MapHttpRoute( + // name: "SwaggerApiDeclaration", + // routeTemplate: "apidocs/{controllerName}", + // defaults: new { swagger = true, controller = "Swagger", action = "GetApiDeclaration" } + //); - SwaggerConfiguration.DefaultConfiguration = SwaggerConfiguration.CreateDefaultConfig(GlobalConfiguration.Configuration); } } } \ No newline at end of file diff --git a/Swagger.Net.WebAPI/Views/Home/Index.cshtml b/Swagger.Net.WebAPI/Views/Home/Index.cshtml index 38ebf05..0af07e6 100644 --- a/Swagger.Net.WebAPI/Views/Home/Index.cshtml +++ b/Swagger.Net.WebAPI/Views/Home/Index.cshtml @@ -1,4 +1,4 @@ 

Welcome to ASP.NET Web API!

Navigate to /docs for the Swagger UI docs. These are not included in the NuGet package.
-Navigate to /api/swagger to see the JSON specification emitted.
+Navigate to /api/swagger to see the JSON specification emitted.
You may then navigate to each api path to see the JSON documentation for each ApiController individually. \ No newline at end of file diff --git a/Swagger.Net.WebAPI/docs/index.html b/Swagger.Net.WebAPI/docs/index.html index 361a9c5..d7bf7c7 100644 --- a/Swagger.Net.WebAPI/docs/index.html +++ b/Swagger.Net.WebAPI/docs/index.html @@ -42,7 +42,7 @@