From 86b24f94d1c105f1300e8a1d29a35d08f6fc3182 Mon Sep 17 00:00:00 2001 From: Cesar De la Torre Date: Mon, 11 Jan 2016 12:41:31 -0800 Subject: [PATCH 1/3] Code implementing Swagger support in our Azure Mobile App service by using SwashBuckle. For more info, check this glob post out: http://blogs.msdn.com/b/cesardelatorre/archive/2015/12/16/migrating-an-azure-mobile-app-service-to-powerapps-it-managed-service-healthclinic-biz-sample-apps.aspx --- .../MyHealth.MobileApp.csproj | 4 + src/MyHealth.MobileApp/Startup.cs | 75 ++++++++++++++++++- src/MyHealth.MobileApp/packages.config | 1 + 3 files changed, 79 insertions(+), 1 deletion(-) diff --git a/src/MyHealth.MobileApp/MyHealth.MobileApp.csproj b/src/MyHealth.MobileApp/MyHealth.MobileApp.csproj index efb3b82..f273d78 100644 --- a/src/MyHealth.MobileApp/MyHealth.MobileApp.csproj +++ b/src/MyHealth.MobileApp/MyHealth.MobileApp.csproj @@ -163,6 +163,10 @@ ..\..\packages\RazorEngine.3.7.0\lib\net45\RazorEngine.dll True + + ..\..\packages\Swashbuckle.Core.5.2.2\lib\net40\Swashbuckle.Core.dll + True + ..\..\packages\System.IdentityModel.Tokens.Jwt.4.0.2.205111437\lib\net45\System.IdentityModel.Tokens.Jwt.dll True diff --git a/src/MyHealth.MobileApp/Startup.cs b/src/MyHealth.MobileApp/Startup.cs index fbdb7ee..a0455ca 100644 --- a/src/MyHealth.MobileApp/Startup.cs +++ b/src/MyHealth.MobileApp/Startup.cs @@ -3,8 +3,19 @@ using Owin; using System.Web.Http; -[assembly: OwinStartup(typeof(MyHealth.MobileApp.Startup))] +//(CDLTLL) HTTP Description needed for Swagger +using System.Web.Http.Description; + +//(CDLTLL) Needed for Filters +using System.Globalization; +using System.Linq; + +//(CDLTLL) - Has the HttpConfiguration extension methods with .EnableSwagger and .EnableSwaggerUi +using Swashbuckle.Application; +using Swashbuckle.Swagger; +using Newtonsoft.Json.Serialization; +[assembly: OwinStartup(typeof(MyHealth.MobileApp.Startup))] namespace MyHealth.MobileApp { public class Startup @@ -13,6 +24,27 @@ public void Configuration(IAppBuilder app) { HttpConfiguration config = new HttpConfiguration(); + config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); + + //(CDLTLL) + //See info about OWIN in https://github.com/domaindrivendev/Swashbuckle + //Manually enable the Swagger docs and, optionally, the swagger-ui by invoking the + //extension methods (in namespace Swashbuckle.Application) on an instance of HttpConfiguration in Startup.cs + config + .EnableSwagger(c => + { + c.SingleApiVersion("v1", "MyHealth.MobileApp"); + // Set filter to eliminate duplicate operation ids from being generated + // when there are multiple operations with the same verb in the API. + c.OperationFilter(); + + //(CDLTLL) Set another filter to eliminate duplciation operation ids from being generated + // when there are multiple Web API routes, like /tables and /api + c.OperationFilter(); + } + ) + .EnableSwaggerUi(); + new MobileAppConfiguration() .UseDefaultConfiguration() .ApplyTo(config); @@ -20,4 +52,45 @@ public void Configuration(IAppBuilder app) app.UseWebApi(config); } } + + //(CDLTLL) + internal class IncludeParameterNamesInOperationIdFilter : IOperationFilter + { + public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) + { + if (operation.parameters != null) + { + // Select the capitalized parameter names + var parameters = operation.parameters.Select( + p => CultureInfo.InvariantCulture.TextInfo.ToTitleCase(p.name)); + + // Set the operation id to match the format "OperationByParam1AndParam2" + operation.operationId = string.Format( + "{0}By{1}", + operation.operationId, + string.Join("And", parameters)); + } + } + } + + //(CDLTLL) + internal class IncludeRouteNameInOperationIdFilter : IOperationFilter + { + public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) + { + + //Find current Route name + string routeTemplate = apiDescription.Route.RouteTemplate; + string BaseRouteName = routeTemplate.Substring(0, routeTemplate.IndexOf('/')); + string postfix = "_" + BaseRouteName + "_Route"; + + // Set the operation id to match the format "OperationThroughRouteName" + operation.operationId = string.Format( + "{0}_Through{1}", + operation.operationId, + postfix); + + } + } + } diff --git a/src/MyHealth.MobileApp/packages.config b/src/MyHealth.MobileApp/packages.config index b076596..ecdf1e0 100644 --- a/src/MyHealth.MobileApp/packages.config +++ b/src/MyHealth.MobileApp/packages.config @@ -37,6 +37,7 @@ + \ No newline at end of file From 9d61c45615bf5ac7f96e66a00a9397bb29fdbdab Mon Sep 17 00:00:00 2001 From: Cesar De la Torre Date: Mon, 11 Jan 2016 16:03:02 -0800 Subject: [PATCH 2/3] Minor changes in comments related to Swagger implementation --- src/MyHealth.MobileApp/Startup.cs | 13 +++++-------- 1 file changed, 5 insertions(+), 8 deletions(-) diff --git a/src/MyHealth.MobileApp/Startup.cs b/src/MyHealth.MobileApp/Startup.cs index a0455ca..51368f9 100644 --- a/src/MyHealth.MobileApp/Startup.cs +++ b/src/MyHealth.MobileApp/Startup.cs @@ -3,14 +3,14 @@ using Owin; using System.Web.Http; -//(CDLTLL) HTTP Description needed for Swagger +//HTTP Description needed for Swagger using System.Web.Http.Description; -//(CDLTLL) Needed for Filters +//Needed for Swashbuckle Filters using System.Globalization; using System.Linq; -//(CDLTLL) - Has the HttpConfiguration extension methods with .EnableSwagger and .EnableSwaggerUi +//Has the HttpConfiguration extension methods with .EnableSwagger and .EnableSwaggerUi using Swashbuckle.Application; using Swashbuckle.Swagger; using Newtonsoft.Json.Serialization; @@ -25,8 +25,7 @@ public void Configuration(IAppBuilder app) HttpConfiguration config = new HttpConfiguration(); config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); - - //(CDLTLL) + //See info about OWIN in https://github.com/domaindrivendev/Swashbuckle //Manually enable the Swagger docs and, optionally, the swagger-ui by invoking the //extension methods (in namespace Swashbuckle.Application) on an instance of HttpConfiguration in Startup.cs @@ -38,7 +37,7 @@ public void Configuration(IAppBuilder app) // when there are multiple operations with the same verb in the API. c.OperationFilter(); - //(CDLTLL) Set another filter to eliminate duplciation operation ids from being generated + // Set another filter to eliminate duplciation operation ids from being generated // when there are multiple Web API routes, like /tables and /api c.OperationFilter(); } @@ -53,7 +52,6 @@ public void Configuration(IAppBuilder app) } } - //(CDLTLL) internal class IncludeParameterNamesInOperationIdFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) @@ -73,7 +71,6 @@ public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescrip } } - //(CDLTLL) internal class IncludeRouteNameInOperationIdFilter : IOperationFilter { public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) From b3716423972170fb72f50636b285292f4933e0bc Mon Sep 17 00:00:00 2001 From: Cesar De la Torre Date: Wed, 13 Jan 2016 10:38:50 -0800 Subject: [PATCH 3/3] Automatic changes --- NuGet.Config | 1 + 1 file changed, 1 insertion(+) diff --git a/NuGet.Config b/NuGet.Config index 7b24038..0707da0 100644 --- a/NuGet.Config +++ b/NuGet.Config @@ -6,5 +6,6 @@ +