From fd96a1e6793985c1abc5404f0f59aee114958960 Mon Sep 17 00:00:00 2001 From: Perth Charernwattanagul Date: Mon, 27 Aug 2018 16:08:11 -0700 Subject: [PATCH] Fix bugs in the routing in sample APIs to allow the endpoitns to be hit. 1) Modify web.config to allow PUT and DELETE to be hit. MVC by default only allows GET and POST. 2) Remove the front slash '/' and the query param from the attribute-based routing since they are prohibited. 3) Register the MVC attribute-based routing in RouteConfig.cs 4) Rename the controllers. MVC requires the controllers to end with suffix `Controller`. If we don't do this, we cannot hit the endpoints :( 5) Fix wrong parameters in SampleV4Controller. --- .../App_Start/RouteConfig.cs | 2 ++ ...pleControllerV1.cs => SampleV1Controller.cs} | 12 ++++++------ ...pleControllerV2.cs => SampleV2Controller.cs} | 8 ++++---- ...pleControllerV3.cs => SampleV3Controller.cs} | 6 +++--- ...pleControllerV4.cs => SampleV4Controller.cs} | 8 ++++---- ...s.DocumentGeneration.Tests.SampleApis.csproj | 8 ++++---- .../web.config | 17 +++++++++++++---- 7 files changed, 36 insertions(+), 25 deletions(-) rename test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/{SampleControllerV1.cs => SampleV1Controller.cs} (94%) rename test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/{SampleControllerV2.cs => SampleV2Controller.cs} (94%) rename test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/{SampleControllerV3.cs => SampleV3Controller.cs} (95%) rename test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/{SampleControllerV4.cs => SampleV4Controller.cs} (93%) diff --git a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/App_Start/RouteConfig.cs b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/App_Start/RouteConfig.cs index ada50b7..92ea72e 100644 --- a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/App_Start/RouteConfig.cs +++ b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/App_Start/RouteConfig.cs @@ -24,6 +24,8 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); + + routes.MapMvcAttributeRoutes(); } } } \ No newline at end of file diff --git a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV1.cs b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV1Controller.cs similarity index 94% rename from test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV1.cs rename to test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV1Controller.cs index 035da79..f67901f 100644 --- a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV1.cs +++ b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV1Controller.cs @@ -13,7 +13,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// /// Define V1 operations. /// - public class SampleControllerV1 : ApiController + public class SampleV1Controller : ApiController { /// /// Sample Get 1 @@ -32,7 +32,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// Group2 /// The sample object 1 [HttpGet] - [Route("/V1/samples/{id}?queryBool={queryBool}")] + [Route("V1/samples/{id}")] public Task SampleGet1(string id, bool queryBool) { throw new NotSupportedException(); @@ -51,8 +51,8 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// Bad request /// The sample object 3 [HttpGet] - [Route("/V1/samples")] - public Task SampleGet2() + [Route("V1/samples")] + public async Task SampleGet2() { throw new NotSupportedException(); } @@ -70,7 +70,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// Sample object posted /// Bad request [HttpPost] - [Route("/V1/samples")] + [Route("V1/samples")] public Task SamplePost([FromBody] SampleObject3 sampleObject) { throw new NotSupportedException(); @@ -91,7 +91,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// Bad request /// The sample object 1 [HttpPut] - [Route("/V1/samples/{id}")] + [Route("V1/samples/{id}")] public Task SamplePut(string id, [FromBody] SampleObject1 sampleObject) { throw new NotSupportedException(); diff --git a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV2.cs b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV2Controller.cs similarity index 94% rename from test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV2.cs rename to test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV2Controller.cs index 223925e..e587aa5 100644 --- a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV2.cs +++ b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV2Controller.cs @@ -14,7 +14,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// /// Defines V2 operations. /// - public class SampleControllerV2 : ApiController + public class SampleV2Controller : ApiController { /// /// Sample delete @@ -29,7 +29,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// Sample object deleted /// Bad request [HttpDelete] - [Route("/V2/samples/{id}")] + [Route("V2/samples/{id}")] public Task DeleteEntity(string id) { throw new NotSupportedException(); @@ -47,7 +47,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// where T is List of sample objects /// Bad request [HttpGet] - [Route("/V2/samples")] + [Route("V2/samples")] public Task> SampleGet1() { throw new NotSupportedException(); @@ -67,7 +67,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// Sample object retrieved /// Bad request [HttpGet] - [Route("/V2/samples/{id}?queryString={queryString}")] + [Route("V2/samples/{id}")] public Task SampleGet2(string id, string queryString) { throw new NotSupportedException(); diff --git a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV3.cs b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV3Controller.cs similarity index 95% rename from test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV3.cs rename to test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV3Controller.cs index fea33da..83f31e0 100644 --- a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV3.cs +++ b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV3Controller.cs @@ -14,7 +14,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// /// Defines V3 operations. /// - public class SampleControllerV3 : ApiController + public class SampleV3Controller : ApiController { /// /// Sample get 1 @@ -34,7 +34,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// /// Bad request [HttpGet] - [Route("/V3/samples")] + [Route("V3/samples")] public Task>> SampleGet1() { throw new NotSupportedException(); @@ -59,7 +59,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// /// Bad request [HttpGet] - [Route("/V3/samples/{id}?queryString={queryString}")] + [Route("V3/samples/{id}")] public Task> SampleGet2(string id, string queryString) { throw new NotSupportedException(); diff --git a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV4.cs b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV4Controller.cs similarity index 93% rename from test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV4.cs rename to test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV4Controller.cs index ffd2d72..466f3b5 100644 --- a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleControllerV4.cs +++ b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Controllers/SampleV4Controller.cs @@ -14,7 +14,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// /// Defines V4 operations. /// - public class SampleControllerV4 : ApiController + public class SampleV4Controller : ApiController { /// /// Sample get 1 @@ -34,7 +34,7 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// /// Bad request [HttpGet] - [Route("/V4/samples")] + [Route("V4/samples")] public Task>> SampleGet1() { throw new NotSupportedException(); @@ -70,8 +70,8 @@ namespace Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApi /// /// Bad request [HttpPost] - [Route("/V4/samples")] - public Task SamplePost1(SampleObject1 sampleObject1, string id) + [Route("V4/samples/{id}")] + public Task SamplePost1([FromBody] SampleObject1 sampleObject1, string id, bool queryBool) { throw new NotSupportedException(); } diff --git a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis.csproj b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis.csproj index c2a562e..f718e0b 100644 --- a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis.csproj +++ b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis.csproj @@ -112,10 +112,10 @@ - - - - + + + + Global.asax diff --git a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/web.config b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/web.config index 78bf685..09928bd 100644 --- a/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/web.config +++ b/test/Microsoft.OpenApi.CSharpAnnotations.DocumentGeneration.Tests.SampleApis/web.config @@ -1,10 +1,10 @@ - + - - + + @@ -17,6 +17,15 @@ --> - + + + + + + + \ No newline at end of file