From 53e788fc45d9e16fb16132c0161b7bac67687808 Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Mon, 14 Aug 2017 10:04:15 +0100 Subject: [PATCH 01/10] WrapResponses bugfix - using System.Net.Http carried on FileTemplate from ControllerTemplate --- src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt b/src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt index 9bdc61dd14..983a7d0968 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt +++ b/src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt @@ -8,7 +8,9 @@ <#foreach(var usage in Model.NamespaceUsages){#>using <#=usage#>; <#}#> - +<# if (Model.WrapResponses){#> +using System.Net.Http; +<# }#> namespace <#=Model.Namespace#> { #pragma warning disable // Disable all warnings From 3ceb24307ce1f37626d3919f54c983ae1ca918b9 Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Mon, 14 Aug 2017 10:07:09 +0100 Subject: [PATCH 02/10] SwaggerToWebApiController new futures- GenerateControllerInterfaces and GenerateAbstractController setting is added + created new test classes --- .../GenerateAbstractControllersTests.cs | 70 +++++++++++++++++++ .../GenerateControllerInterfacesTests.cs | 66 +++++++++++++++++ .../NSwag.CodeGeneration.CSharp.Tests.csproj | 2 + .../Models/CSharpControllerTemplateModel.cs | 5 ++ .../SwaggerToCSharpGeneratorSettings.cs | 6 ++ .../Templates/ControllerTemplate.tt | 51 +++++++++----- .../ClientGeneratorBaseSettings.cs | 8 +++ 7 files changed, 189 insertions(+), 19 deletions(-) create mode 100644 src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs create mode 100644 src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs new file mode 100644 index 0000000000..283c613adb --- /dev/null +++ b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs @@ -0,0 +1,70 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Web.Http; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using NSwag.SwaggerGeneration.WebApi; + +namespace NSwag.CodeGeneration.CSharp.Tests +{ + public class GenerateAbstractControllersTests + { + public class TestController : ApiController + { + [Route("Foo")] + public string Foo() + { + throw new NotImplementedException(); + } + + [Route("Bar")] + public void Bar() + { + throw new NotImplementedException(); + } + } + [TestMethod] + public async Task When_generateabstractcontrollers_true_then_abstractcontroller_is_returned() + { + //// Arrange + var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); + var document = await swaggerGen.GenerateForControllerAsync(); + + //// Act + var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings + { + GenerateControllerInterfaces = false, + GenerateAbstractControllers = true + }); + var code = codeGen.GenerateFile(); + + //// Assert + Assert.IsTrue(code.Contains("abstract class TestController")); + Assert.IsTrue(code.Contains("abstract Foo")); + } + + [TestMethod] + public async Task When_generateabstractcontrollers_false_then_abstractcontroller_is_not_returned() + { + //// Arrange + var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); + var document = await swaggerGen.GenerateForControllerAsync(); + + //// Act + var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings + { + GenerateControllerInterfaces = true, + GenerateAbstractControllers = false + }); + var code = codeGen.GenerateFile(); + + //// Assert + Assert.IsTrue(code.Contains("abstract class TestController")); + Assert.IsTrue(code.Contains("abstract Foo")); + } + } +} + + diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs new file mode 100644 index 0000000000..f8a3360ceb --- /dev/null +++ b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs @@ -0,0 +1,66 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Web.Http; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using NSwag.SwaggerGeneration.WebApi; + +namespace NSwag.CodeGeneration.CSharp.Tests +{ + public class GenerateControllerInterfacesTests + { + public class TestController : ApiController + { + [Route("Foo")] + public string Foo() + { + throw new NotImplementedException(); + } + + [Route("Bar")] + public void Bar() + { + throw new NotImplementedException(); + } + } + [TestMethod] + public async Task When_generatecontrollerinterfaces_true_then_interface_is_returned() + { + //// Arrange + var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); + var document = await swaggerGen.GenerateForControllerAsync(); + + //// Act + var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings + { + GenerateControllerInterfaces = true + }); + var code = codeGen.GenerateFile(); + + //// Assert + Assert.IsTrue(code.Contains("ITestController")); + Assert.IsTrue(code.Contains("private ITestController _implementation; ")); + } + + [TestMethod] + public async Task When_generatecontrollerinterfaces_false_then_interface_is_not_returned() + { + //// Arrange + var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); + var document = await swaggerGen.GenerateForControllerAsync(); + + //// Act + var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings + { + GenerateControllerInterfaces = false + }); + var code = codeGen.GenerateFile(); + + //// Assert + Assert.IsFalse(code.Contains("ITestController")); + Assert.IsFalse(code.Contains("private ITestController _implementation; ")); + } + } +} diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/NSwag.CodeGeneration.CSharp.Tests.csproj b/src/NSwag.CodeGeneration.CSharp.Tests/NSwag.CodeGeneration.CSharp.Tests.csproj index 10538e8fe9..83d17c3076 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/NSwag.CodeGeneration.CSharp.Tests.csproj +++ b/src/NSwag.CodeGeneration.CSharp.Tests/NSwag.CodeGeneration.CSharp.Tests.csproj @@ -66,6 +66,8 @@ + + diff --git a/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs index a48e4d6a79..c79a6cb3ec 100644 --- a/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs @@ -64,5 +64,10 @@ public CSharpControllerTemplateModel( /// Gets a value indicating whether to generate optional parameters. public bool GenerateOptionalParameters => _settings.GenerateOptionalParameters; + + /// Gets a value indicating whether to generate controller interfaces. + public bool GenerateControllerInterfaces => _settings.GenerateControllerInterfaces; + /// Gets a value indicating whether to generate abstract controllers. + public bool GenerateAbstractControllers => _settings.GenerateAbstractControllers; } } \ No newline at end of file diff --git a/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs b/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs index be6cb4eedd..7ab53e6b0f 100644 --- a/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs +++ b/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs @@ -28,6 +28,8 @@ protected SwaggerToCSharpGeneratorSettings() GenerateResponseClasses = true; ResponseClass = "SwaggerResponse"; + GenerateControllerInterfaces = true; + GenerateAbstractControllers = false; } /// Gets or sets the CSharp generator settings. @@ -50,5 +52,9 @@ protected SwaggerToCSharpGeneratorSettings() /// Gets or sets the name of the response class (supports the '{controller}' placeholder). public string ResponseClass { get; set; } + /// Gets a value indicating whether to generate controller interfaces. + public bool GenerateControllerInterfaces { get; set; } + /// Gets a value indicating whether to generate abstract controllers. + public bool GenerateAbstractControllers { get; set; } } } \ No newline at end of file diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt index 6445cc007b..c6fda31400 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt +++ b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt @@ -1,10 +1,8 @@ <#@ template visibility="internal" #> <#@ import namespace="NJsonSchema" #> -<# if (Model.WrapResponses){#> -using System.Net.Http; -<# }#> +<# if (Model.GenerateControllerInterfaces){#> [System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] public interface I<#=Model.Class#>Controller { @@ -15,47 +13,62 @@ public interface I<#=Model.Class#>Controller <# }}if(operation.HasResultDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1)#> <# }if(operation.IsDeprecated){#> [System.Obsolete] <# }#> <#=operation.ResultType#> <#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.Type#> <#=parameter.VariableName#><#if(Model.GenerateOptionalParameters && parameter.IsOptional){#> = null<#}#><#if(!parameter.IsLast){#>, <#}}#>); - <#}#> } +<# }#> [System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] <#if(Model.HasBasePath){#> -[System.Web.Http.RoutePrefix("<#=Model.BasePath#>")] -<#}#> -public partial class <#=Model.Class#>Controller : <#if(Model.HasBaseClass){#><#=Model.BaseClass#><#}else{#>System.Web.Http.ApiController<#}#> +[System.Web.Http.RoutePrefix("<#=Model.BasePath#>")]<#}#> +public <# if(Model.GenerateAbstractControllers){#>abstract class <#=Model.Class#>ControllerBase : <#}#><#else{#>partial class <#=Model.Class#>Controller : <#}#><#if(Model.HasBaseClass){#><#=Model.BaseClass#><#}else{#>System.Web.Http.ApiController<#}#> { - private I<#=Model.Class#>Controller _implementation; + <# if (Model.GenerateControllerInterfaces){#> + private I<#=Model.Class #>Controller _implementation; - public <#=Model.Class#>Controller(I<#=Model.Class#>Controller implementation) + public <#=Model.Class #>Controller(I<#=Model.Class #>Controller implementation) { _implementation = implementation; } - -<#foreach(var operation in Model.Operations){#> + <#}#><#foreach(var operation in Model.Operations){#> <# if(operation.HasSummary){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1)#> <# }foreach (var parameter in operation.Parameters){#> <# if(parameter.HasDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1)#> <# }}if(operation.HasResultDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1)#> -<# }if(operation.IsDeprecated){#> [System.Obsolete] -<# }#> [System.Web.Http.Http<#=operation.HttpMethodUpper#>, System.Web.Http.Route("<#=operation.Path#>")] - public <#if(Model.WrapResponses){#>async System.Threading.Tasks.Task<#}else{#><#=operation.ResultType#><#}#> <#=operation.ActualOperationName#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.Type#> <#=parameter.VariableName#><#if(Model.GenerateOptionalParameters && parameter.IsOptional){#> = null<#}#><#if(!parameter.IsLast){#>, <#}}#>) - { +<# }if(operation.IsDeprecated){#> [System.Obsolete]<#}#> + [System.Web.Http.Http<#=operation.HttpMethodUpper#>, System.Web.Http.Route("<#=operation.Path#>")] + public <# if(Model.GenerateAbstractControllers){#>abstract <#}#> <# if(Model.WrapResponses){#><# if(!Model.GenerateAbstractControllers){#>async <#}#>System.Threading.Tasks.Task<#}else{#><#=operation.ResultType#><#}#> <#=operation.ActualOperationName#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.Type#> <#=parameter.VariableName#><# if(Model.GenerateOptionalParameters && parameter.IsOptional){#> = null<#}#><# if(!parameter.IsLast){#>, <#}}#>)<# if(Model.GenerateAbstractControllers){#>; + <#}else {#> + { + <#}#> + <# if(Model.GenerateControllerInterfaces){#> <# if (Model.WrapResponses){#> - var result = await _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); + var result = await _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><# if(!parameter.IsLast){#>, <#}}#>); var status = (System.Net.HttpStatusCode)System.Enum.Parse(typeof(System.Net.HttpStatusCode), result.StatusCode); - HttpResponseMessage response = Request.CreateResponse(status<#if(operation.UnwrappedResultType != "void"){#>, result.Result<#}#>); + HttpResponseMessage response = Request.CreateResponse(status<# if(operation.UnwrappedResultType != "void"){#>, result.Result<#}#>); foreach (var header in result.Headers) response.Headers.Add(header.Key, header.Value); return response; <# }else{#> - return _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); + return _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); <# }#> - } +<#}else {#> + <# if (!Model.GenerateAbstractControllers){#> + <# if (Model.WrapResponses){#> + var result = System.Threading.Tasks.Task<<#=operation.UnwrappedResultType#>>.FromResult(new <#=operation.UnwrappedResultType#>()); + var status = System.Net.HttpStatusCode.OK; + HttpResponseMessage response = Request.CreateResponse(status<# if(operation.UnwrappedResultType != "void"){#>, result<#}#>); + + return response; +<# }else{#> + return System.Threading.Tasks.Task<<#=operation.UnwrappedResultType#>>.FromResult(new <#=operation.UnwrappedResultType#>()); +<# }#> + } + <#}#> + <#}#> <#}#> } diff --git a/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs b/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs index 078da99acd..5db430055c 100644 --- a/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs +++ b/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs @@ -23,6 +23,8 @@ protected ClientGeneratorBaseSettings() OperationNameGenerator = new MultipleClientsFromOperationIdOperationNameGenerator(); ParameterNameGenerator = new DefaultParameterNameGenerator(); + GenerateControllerInterfaces = true; + GenerateAbstractControllers = false; } /// Gets the code generator settings. @@ -46,6 +48,12 @@ protected ClientGeneratorBaseSettings() /// Gets or sets a value indicating whether to reorder parameters (required first, optional at the end) and generate optional C# parameters (default: true). public bool GenerateOptionalParameters { get; set; } + /// Gets a value indicating whether to generate controller interfaces. + public bool GenerateControllerInterfaces { get; set; } + + /// Gets a value indicating whether to generate abstract controllers. + public bool GenerateAbstractControllers { get; set; } + /// Gets or sets the parameter name generator. public IParameterNameGenerator ParameterNameGenerator { get; set; } From e7186e06b90bc358140c43d5ed9867104da7c8ea Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Mon, 14 Aug 2017 10:08:03 +0100 Subject: [PATCH 03/10] SwaggerToCSharpControllerCommand new futures- GenerateControllerInterfaces and GenerateAbstractController setting is added --- .../Commands/SwaggerToCSharpControllerCommand.cs | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs b/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs index f80746ba37..2764de8a21 100644 --- a/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs +++ b/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs @@ -28,6 +28,20 @@ public string ControllerBaseClass set { Settings.ControllerBaseClass = value; } } + [Argument(Name = "GenerateControllerInterfaces", Description = "The controller base class (empty for 'ApiController').", IsRequired = false)] + public bool GenerateControllerInterfaces + { + get { return Settings.GenerateControllerInterfaces; } + set { Settings.GenerateControllerInterfaces = value; } + } + + [Argument(Name = "GenerateAbstractControllers", Description = "The controller base class (empty for 'ApiController').", IsRequired = false)] + public bool GenerateAbstractControllers + { + get { return Settings.GenerateAbstractControllers; } + set { Settings.GenerateAbstractControllers = value; } + } + public override async Task RunAsync(CommandLineProcessor processor, IConsoleHost host) { var code = await RunAsync(); From 80eee6b789dc3d5023b4ecec19a385aea78d093c Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Mon, 14 Aug 2017 17:27:32 +0100 Subject: [PATCH 04/10] Code review - fixed test errors https://github.com/Aqovia/NSwag/pull/1/files/e7186e06b90bc358140c43d5ed9867104da7c8ea#r132965358 https://github.com/Aqovia/NSwag/pull/1/files/e7186e06b90bc358140c43d5ed9867104da7c8ea#r132965807 --- .../GenerateAbstractControllersTests.cs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs index 283c613adb..da0d1c0108 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs +++ b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs @@ -35,7 +35,6 @@ public async Task When_generateabstractcontrollers_true_then_abstractcontroller_ //// Act var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings { - GenerateControllerInterfaces = false, GenerateAbstractControllers = true }); var code = codeGen.GenerateFile(); @@ -55,14 +54,13 @@ public async Task When_generateabstractcontrollers_false_then_abstractcontroller //// Act var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings { - GenerateControllerInterfaces = true, GenerateAbstractControllers = false }); var code = codeGen.GenerateFile(); //// Assert - Assert.IsTrue(code.Contains("abstract class TestController")); - Assert.IsTrue(code.Contains("abstract Foo")); + Assert.IsFalse(code.Contains("abstract class TestController")); + Assert.IsFalse(code.Contains("abstract Foo")); } } } From 2d57b5fdd6b14207bea773e74c42743a6e1d9a3c Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Mon, 14 Aug 2017 17:29:38 +0100 Subject: [PATCH 05/10] Code review - fixed alligment https://github.com/Aqovia/NSwag/pull/1/files/e7186e06b90bc358140c43d5ed9867104da7c8ea#r132967498 --- .../Templates/ControllerTemplate.tt | 43 ++++++++++--------- 1 file changed, 22 insertions(+), 21 deletions(-) diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt index c6fda31400..f438cd776e 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt +++ b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt @@ -13,51 +13,52 @@ public interface I<#=Model.Class#>Controller <# }}if(operation.HasResultDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1)#> <# }if(operation.IsDeprecated){#> [System.Obsolete] <# }#> <#=operation.ResultType#> <#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.Type#> <#=parameter.VariableName#><#if(Model.GenerateOptionalParameters && parameter.IsOptional){#> = null<#}#><#if(!parameter.IsLast){#>, <#}}#>); + <#}#> } <# }#> [System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] <#if(Model.HasBasePath){#> -[System.Web.Http.RoutePrefix("<#=Model.BasePath#>")]<#}#> +[System.Web.Http.RoutePrefix("<#=Model.BasePath#>")] +<#}#> public <# if(Model.GenerateAbstractControllers){#>abstract class <#=Model.Class#>ControllerBase : <#}#><#else{#>partial class <#=Model.Class#>Controller : <#}#><#if(Model.HasBaseClass){#><#=Model.BaseClass#><#}else{#>System.Web.Http.ApiController<#}#> -{ - <# if (Model.GenerateControllerInterfaces){#> - private I<#=Model.Class #>Controller _implementation; +{<# if (Model.GenerateControllerInterfaces){#> + private I<#=Model.Class #>Controller _implementation; public <#=Model.Class #>Controller(I<#=Model.Class #>Controller implementation) { _implementation = implementation; } - <#}#><#foreach(var operation in Model.Operations){#> +<#}#> + <#foreach(var operation in Model.Operations){#> <# if(operation.HasSummary){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1)#> <# }foreach (var parameter in operation.Parameters){#> <# if(parameter.HasDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1)#> <# }}if(operation.HasResultDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1)#> -<# }if(operation.IsDeprecated){#> [System.Obsolete]<#}#> - [System.Web.Http.Http<#=operation.HttpMethodUpper#>, System.Web.Http.Route("<#=operation.Path#>")] +<# }if(operation.IsDeprecated){#> [System.Obsolete] +<#}#> [System.Web.Http.Http<#=operation.HttpMethodUpper#>, System.Web.Http.Route("<#=operation.Path#>")] public <# if(Model.GenerateAbstractControllers){#>abstract <#}#> <# if(Model.WrapResponses){#><# if(!Model.GenerateAbstractControllers){#>async <#}#>System.Threading.Tasks.Task<#}else{#><#=operation.ResultType#><#}#> <#=operation.ActualOperationName#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.Type#> <#=parameter.VariableName#><# if(Model.GenerateOptionalParameters && parameter.IsOptional){#> = null<#}#><# if(!parameter.IsLast){#>, <#}}#>)<# if(Model.GenerateAbstractControllers){#>; - <#}else {#> - { - <#}#> - <# if(Model.GenerateControllerInterfaces){#> +<#}else {#> {<#}#> +<# if(Model.GenerateControllerInterfaces){#> <# if (Model.WrapResponses){#> - var result = await _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><# if(!parameter.IsLast){#>, <#}}#>); + var result = await _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); var status = (System.Net.HttpStatusCode)System.Enum.Parse(typeof(System.Net.HttpStatusCode), result.StatusCode); - HttpResponseMessage response = Request.CreateResponse(status<# if(operation.UnwrappedResultType != "void"){#>, result.Result<#}#>); + HttpResponseMessage response = Request.CreateResponse(status<#if(operation.UnwrappedResultType != "void"){#>, result.Result<#}#>); foreach (var header in result.Headers) response.Headers.Add(header.Key, header.Value); return response; <# }else{#> - return _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); + + return _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); <# }#> - } -<#}else {#> - <# if (!Model.GenerateAbstractControllers){#> - <# if (Model.WrapResponses){#> + + }<#}else {#> +<# if (!Model.GenerateAbstractControllers){#> +<# if (Model.WrapResponses){#> var result = System.Threading.Tasks.Task<<#=operation.UnwrappedResultType#>>.FromResult(new <#=operation.UnwrappedResultType#>()); var status = System.Net.HttpStatusCode.OK; @@ -65,10 +66,10 @@ public <# if(Model.GenerateAbstractControllers){#>abstract class <#=Model.Class# return response; <# }else{#> + return System.Threading.Tasks.Task<<#=operation.UnwrappedResultType#>>.FromResult(new <#=operation.UnwrappedResultType#>()); -<# }#> - } + <#}#>} <#}#> - <#}#> +<#}#> <#}#> } From 8b68c9f65e8d6885b0834bea3c3a5c0420866eec Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Mon, 14 Aug 2017 17:57:05 +0100 Subject: [PATCH 06/10] Code review- unit tests fixed https://github.com/Aqovia/NSwag/pull/1/files#r132965358 --- .../GenerateAbstractControllersTests.cs | 7 +++---- .../GenerateControllerInterfacesTests.cs | 5 +++-- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs index da0d1c0108..a2e6fb92a9 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs +++ b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs @@ -9,6 +9,7 @@ namespace NSwag.CodeGeneration.CSharp.Tests { + [TestClass] public class GenerateAbstractControllersTests { public class TestController : ApiController @@ -33,7 +34,7 @@ public async Task When_generateabstractcontrollers_true_then_abstractcontroller_ var document = await swaggerGen.GenerateForControllerAsync(); //// Act - var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings + var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings { GenerateAbstractControllers = true }); @@ -41,7 +42,6 @@ public async Task When_generateabstractcontrollers_true_then_abstractcontroller_ //// Assert Assert.IsTrue(code.Contains("abstract class TestController")); - Assert.IsTrue(code.Contains("abstract Foo")); } [TestMethod] @@ -52,7 +52,7 @@ public async Task When_generateabstractcontrollers_false_then_abstractcontroller var document = await swaggerGen.GenerateForControllerAsync(); //// Act - var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings + var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings { GenerateAbstractControllers = false }); @@ -60,7 +60,6 @@ public async Task When_generateabstractcontrollers_false_then_abstractcontroller //// Assert Assert.IsFalse(code.Contains("abstract class TestController")); - Assert.IsFalse(code.Contains("abstract Foo")); } } } diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs index f8a3360ceb..a5b9d20980 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs +++ b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs @@ -9,6 +9,7 @@ namespace NSwag.CodeGeneration.CSharp.Tests { + [TestClass] public class GenerateControllerInterfacesTests { public class TestController : ApiController @@ -33,7 +34,7 @@ public async Task When_generatecontrollerinterfaces_true_then_interface_is_retur var document = await swaggerGen.GenerateForControllerAsync(); //// Act - var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings + var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings { GenerateControllerInterfaces = true }); @@ -52,7 +53,7 @@ public async Task When_generatecontrollerinterfaces_false_then_interface_is_not_ var document = await swaggerGen.GenerateForControllerAsync(); //// Act - var codeGen = new SwaggerToCSharpClientGenerator(document, new SwaggerToCSharpClientGeneratorSettings + var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings() { GenerateControllerInterfaces = false }); From 3dfc227a9fda0c37454aaeae9b0153d2b1787c53 Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Tue, 15 Aug 2017 09:55:42 +0100 Subject: [PATCH 07/10] Bug fix - WrapResponses using System.Net.Http --- src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt b/src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt index 983a7d0968..5fc262708f 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt +++ b/src/NSwag.CodeGeneration.CSharp/Templates/FileTemplate.tt @@ -8,8 +8,7 @@ <#foreach(var usage in Model.NamespaceUsages){#>using <#=usage#>; <#}#> -<# if (Model.WrapResponses){#> -using System.Net.Http; +<# if (Model.WrapResponses){#>using System.Net.Http; <# }#> namespace <#=Model.Namespace#> { From 8e006ea499b49ae090d2b551e0ce99fb0ba02fb5 Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Tue, 15 Aug 2017 17:02:30 +0100 Subject: [PATCH 08/10] GenerateControllerInterfaces and GenerateAbstractController settings are deleted. ControllerGenerationFormat parameter is added. (default:partial)Test classes are deleted and new test class is added. --- ....cs => ControllerGenerationFormatTests.cs} | 48 +++++++++---- .../GenerateAbstractControllersTests.cs | 67 ------------------- .../NSwag.CodeGeneration.CSharp.Tests.csproj | 3 +- .../Models/CSharpControllerTemplateModel.cs | 6 +- .../SwaggerToCSharpGeneratorSettings.cs | 11 ++- .../Templates/ControllerTemplate.tt | 65 ++++++++++-------- .../ClientGeneratorBaseSettings.cs | 10 +-- .../SwaggerToCSharpControllerCommand.cs | 19 ++---- 8 files changed, 89 insertions(+), 140 deletions(-) rename src/NSwag.CodeGeneration.CSharp.Tests/{GenerateControllerInterfacesTests.cs => ControllerGenerationFormatTests.cs} (53%) delete mode 100644 src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs similarity index 53% rename from src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs rename to src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs index a5b9d20980..1b2a679042 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateControllerInterfacesTests.cs +++ b/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs @@ -1,7 +1,4 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; using System.Threading.Tasks; using System.Web.Http; using Microsoft.VisualStudio.TestTools.UnitTesting; @@ -10,7 +7,7 @@ namespace NSwag.CodeGeneration.CSharp.Tests { [TestClass] - public class GenerateControllerInterfacesTests + public class ControllerGenerationFormatTests { public class TestController : ApiController { @@ -26,8 +23,30 @@ public void Bar() throw new NotImplementedException(); } } + + [TestMethod] + public async Task When_controllergenerationformat_abstract_then_abstractcontroller_is_generated() + { + //// Arrange + var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); + var document = await swaggerGen.GenerateForControllerAsync(); + + //// Act + var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings + { + ControllerGenerationFormat = "abstract", + }); + var code = codeGen.GenerateFile(); + + //// Assert + Assert.IsTrue(code.Contains("abstract class TestController")); + Assert.IsFalse(code.Contains("ITestController")); + Assert.IsFalse(code.Contains("private ITestController _implementation;")); + Assert.IsFalse(code.Contains("partial")); + } + [TestMethod] - public async Task When_generatecontrollerinterfaces_true_then_interface_is_returned() + public async Task When_controllergenerationformat_abstract_then_partialcontroller_is_generated() { //// Arrange var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); @@ -36,32 +55,37 @@ public async Task When_generatecontrollerinterfaces_true_then_interface_is_retur //// Act var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings { - GenerateControllerInterfaces = true + ControllerGenerationFormat = "partial", }); var code = codeGen.GenerateFile(); //// Assert + Assert.IsTrue(code.Contains("partial class TestController")); Assert.IsTrue(code.Contains("ITestController")); - Assert.IsTrue(code.Contains("private ITestController _implementation; ")); + Assert.IsTrue(code.Contains("private ITestController _implementation;")); + Assert.IsFalse(code.Contains("abstract class TestController")); } [TestMethod] - public async Task When_generatecontrollerinterfaces_false_then_interface_is_not_returned() + public async Task When_controllergenerationformat_notsetted_then_partialcontroller_is_generated() { //// Arrange var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); var document = await swaggerGen.GenerateForControllerAsync(); //// Act - var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings() + var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings { - GenerateControllerInterfaces = false }); var code = codeGen.GenerateFile(); //// Assert - Assert.IsFalse(code.Contains("ITestController")); - Assert.IsFalse(code.Contains("private ITestController _implementation; ")); + Assert.IsTrue(code.Contains("partial class TestController")); + Assert.IsTrue(code.Contains("ITestController")); + Assert.IsTrue(code.Contains("private ITestController _implementation;")); + Assert.IsFalse(code.Contains("abstract class TestController")); } } } + + diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs deleted file mode 100644 index a2e6fb92a9..0000000000 --- a/src/NSwag.CodeGeneration.CSharp.Tests/GenerateAbstractControllersTests.cs +++ /dev/null @@ -1,67 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Web.Http; -using Microsoft.VisualStudio.TestTools.UnitTesting; -using NSwag.SwaggerGeneration.WebApi; - -namespace NSwag.CodeGeneration.CSharp.Tests -{ - [TestClass] - public class GenerateAbstractControllersTests - { - public class TestController : ApiController - { - [Route("Foo")] - public string Foo() - { - throw new NotImplementedException(); - } - - [Route("Bar")] - public void Bar() - { - throw new NotImplementedException(); - } - } - [TestMethod] - public async Task When_generateabstractcontrollers_true_then_abstractcontroller_is_returned() - { - //// Arrange - var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); - var document = await swaggerGen.GenerateForControllerAsync(); - - //// Act - var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings - { - GenerateAbstractControllers = true - }); - var code = codeGen.GenerateFile(); - - //// Assert - Assert.IsTrue(code.Contains("abstract class TestController")); - } - - [TestMethod] - public async Task When_generateabstractcontrollers_false_then_abstractcontroller_is_not_returned() - { - //// Arrange - var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings()); - var document = await swaggerGen.GenerateForControllerAsync(); - - //// Act - var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings - { - GenerateAbstractControllers = false - }); - var code = codeGen.GenerateFile(); - - //// Assert - Assert.IsFalse(code.Contains("abstract class TestController")); - } - } -} - - diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/NSwag.CodeGeneration.CSharp.Tests.csproj b/src/NSwag.CodeGeneration.CSharp.Tests/NSwag.CodeGeneration.CSharp.Tests.csproj index 83d17c3076..25ffce9125 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/NSwag.CodeGeneration.CSharp.Tests.csproj +++ b/src/NSwag.CodeGeneration.CSharp.Tests/NSwag.CodeGeneration.CSharp.Tests.csproj @@ -63,11 +63,10 @@ + - - diff --git a/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs index c79a6cb3ec..35c02504ec 100644 --- a/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs @@ -65,9 +65,7 @@ public CSharpControllerTemplateModel( /// Gets a value indicating whether to generate optional parameters. public bool GenerateOptionalParameters => _settings.GenerateOptionalParameters; - /// Gets a value indicating whether to generate controller interfaces. - public bool GenerateControllerInterfaces => _settings.GenerateControllerInterfaces; - /// Gets a value indicating whether to generate abstract controllers. - public bool GenerateAbstractControllers => _settings.GenerateAbstractControllers; + /// Gets a value indicating whether to controller generation format.(default: partial;abstract, partial.) + public string ControllerGenerationFormat => _settings.ControllerGenerationFormat; } } \ No newline at end of file diff --git a/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs b/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs index 7ab53e6b0f..e1c47c03ef 100644 --- a/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs +++ b/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs @@ -28,8 +28,7 @@ protected SwaggerToCSharpGeneratorSettings() GenerateResponseClasses = true; ResponseClass = "SwaggerResponse"; - GenerateControllerInterfaces = true; - GenerateAbstractControllers = false; + ControllerGenerationFormat = "partial"; } /// Gets or sets the CSharp generator settings. @@ -52,9 +51,9 @@ protected SwaggerToCSharpGeneratorSettings() /// Gets or sets the name of the response class (supports the '{controller}' placeholder). public string ResponseClass { get; set; } - /// Gets a value indicating whether to generate controller interfaces. - public bool GenerateControllerInterfaces { get; set; } - /// Gets a value indicating whether to generate abstract controllers. - public bool GenerateAbstractControllers { get; set; } + + /// Gets a value indicating whether to controller generation format.(default: partial;abstract, partial.) + public string ControllerGenerationFormat { get; set; } + } } \ No newline at end of file diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt index f438cd776e..70f6a749cd 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt +++ b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt @@ -1,9 +1,12 @@ <#@ template visibility="internal" #> <#@ import namespace="NJsonSchema" #> +<# + var isPartial = Model.ControllerGenerationFormat == "partial"; + var isAbstract = Model.ControllerGenerationFormat == "abstract"; +#> -<# if (Model.GenerateControllerInterfaces){#> -[System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] +<# if (isPartial){#>[System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] public interface I<#=Model.Class#>Controller { <#foreach(var operation in Model.Operations){#> @@ -16,31 +19,30 @@ public interface I<#=Model.Class#>Controller <#}#> } -<# }#> - +<#}#> [System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] <#if(Model.HasBasePath){#> [System.Web.Http.RoutePrefix("<#=Model.BasePath#>")] <#}#> -public <# if(Model.GenerateAbstractControllers){#>abstract class <#=Model.Class#>ControllerBase : <#}#><#else{#>partial class <#=Model.Class#>Controller : <#}#><#if(Model.HasBaseClass){#><#=Model.BaseClass#><#}else{#>System.Web.Http.ApiController<#}#> -{<# if (Model.GenerateControllerInterfaces){#> - private I<#=Model.Class #>Controller _implementation; +<# if (isPartial){#> +public partial class <#=Model.Class#>Controller : <#if(Model.HasBaseClass){#><#=Model.BaseClass#><#}else{#>System.Web.Http.ApiController<#}#> +{ + private I<#=Model.Class#>Controller _implementation; - public <#=Model.Class #>Controller(I<#=Model.Class #>Controller implementation) + public <#=Model.Class#>Controller(I<#=Model.Class#>Controller implementation) { _implementation = implementation; } -<#}#> - <#foreach(var operation in Model.Operations){#> + +<#foreach(var operation in Model.Operations){#> <# if(operation.HasSummary){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1)#> <# }foreach (var parameter in operation.Parameters){#> <# if(parameter.HasDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1)#> <# }}if(operation.HasResultDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1)#> <# }if(operation.IsDeprecated){#> [System.Obsolete] -<#}#> [System.Web.Http.Http<#=operation.HttpMethodUpper#>, System.Web.Http.Route("<#=operation.Path#>")] - public <# if(Model.GenerateAbstractControllers){#>abstract <#}#> <# if(Model.WrapResponses){#><# if(!Model.GenerateAbstractControllers){#>async <#}#>System.Threading.Tasks.Task<#}else{#><#=operation.ResultType#><#}#> <#=operation.ActualOperationName#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.Type#> <#=parameter.VariableName#><# if(Model.GenerateOptionalParameters && parameter.IsOptional){#> = null<#}#><# if(!parameter.IsLast){#>, <#}}#>)<# if(Model.GenerateAbstractControllers){#>; -<#}else {#> {<#}#> -<# if(Model.GenerateControllerInterfaces){#> +<# }#> [System.Web.Http.Http<#=operation.HttpMethodUpper#>, System.Web.Http.Route("<#=operation.Path#>")] + public <#if(Model.WrapResponses){#>async System.Threading.Tasks.Task<#}else{#><#=operation.ResultType#><#}#> <#=operation.ActualOperationName#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.Type#> <#=parameter.VariableName#><#if(Model.GenerateOptionalParameters && parameter.IsOptional){#> = null<#}#><#if(!parameter.IsLast){#>, <#}}#>) + { <# if (Model.WrapResponses){#> var result = await _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); @@ -52,24 +54,29 @@ public <# if(Model.GenerateAbstractControllers){#>abstract class <#=Model.Class# return response; <# }else{#> - - return _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); + return _implementation.<#=operation.ActualOperationName#>Async(<#foreach(var parameter in operation.Parameters){#><#=parameter.VariableName#><#if(!parameter.IsLast){#>, <#}}#>); <# }#> + + } - }<#}else {#> -<# if (!Model.GenerateAbstractControllers){#> -<# if (Model.WrapResponses){#> - var result = System.Threading.Tasks.Task<<#=operation.UnwrappedResultType#>>.FromResult(new <#=operation.UnwrappedResultType#>()); +<#}#> +} +<# }else if (isAbstract){#> - var status = System.Net.HttpStatusCode.OK; - HttpResponseMessage response = Request.CreateResponse(status<# if(operation.UnwrappedResultType != "void"){#>, result<#}#>); - - return response; -<# }else{#> +public abstract class <#=Model.Class#>ControllerBase : <#if(Model.HasBaseClass){#><#=Model.BaseClass#><#}else{#>System.Web.Http.ApiController<#}#> + +{ +<#foreach(var operation in Model.Operations){#> +<# if(operation.HasSummary){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1)#> +<# }foreach (var parameter in operation.Parameters){#> +<# if(parameter.HasDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1)#> +<# }}if(operation.HasResultDescription){#> /// <#=ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1)#> +<# }if(operation.IsDeprecated){#> [System.Obsolete] +<# }#> [System.Web.Http.Http<#=operation.HttpMethodUpper#>, System.Web.Http.Route("<#=operation.Path#>")] + public abstract <# if(Model.WrapResponses){#>System.Threading.Tasks.Task<#}else{#><#=operation.ResultType#><#}#> <#=operation.ActualOperationName#>(<#foreach(var parameter in operation.Parameters){#><#=parameter.Type#> <#=parameter.VariableName#><# if(Model.GenerateOptionalParameters && parameter.IsOptional){#> = null<#}#><# if(!parameter.IsLast){#>, <#}}#>); - return System.Threading.Tasks.Task<<#=operation.UnwrappedResultType#>>.FromResult(new <#=operation.UnwrappedResultType#>()); - <#}#>} - <#}#> -<#}#> <#}#> } +<#}#> + + diff --git a/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs b/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs index 5db430055c..037e4964b7 100644 --- a/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs +++ b/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs @@ -23,8 +23,7 @@ protected ClientGeneratorBaseSettings() OperationNameGenerator = new MultipleClientsFromOperationIdOperationNameGenerator(); ParameterNameGenerator = new DefaultParameterNameGenerator(); - GenerateControllerInterfaces = true; - GenerateAbstractControllers = false; + ControllerGenerationFormat = "partial"; } /// Gets the code generator settings. @@ -48,11 +47,8 @@ protected ClientGeneratorBaseSettings() /// Gets or sets a value indicating whether to reorder parameters (required first, optional at the end) and generate optional C# parameters (default: true). public bool GenerateOptionalParameters { get; set; } - /// Gets a value indicating whether to generate controller interfaces. - public bool GenerateControllerInterfaces { get; set; } - - /// Gets a value indicating whether to generate abstract controllers. - public bool GenerateAbstractControllers { get; set; } + /// Gets a value indicating whether to controller generation format.(default: partial;abstract, partial.) + public string ControllerGenerationFormat { get; set; } /// Gets or sets the parameter name generator. public IParameterNameGenerator ParameterNameGenerator { get; set; } diff --git a/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs b/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs index 2764de8a21..d6aff042db 100644 --- a/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs +++ b/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs @@ -27,21 +27,14 @@ public string ControllerBaseClass get { return Settings.ControllerBaseClass; } set { Settings.ControllerBaseClass = value; } } - - [Argument(Name = "GenerateControllerInterfaces", Description = "The controller base class (empty for 'ApiController').", IsRequired = false)] - public bool GenerateControllerInterfaces + + [Argument(Name = "ControllerGenerationFormat", Description = "The controller generation format (default: partial;abstract, partial.).", IsRequired = false)] + public string ControllerGenerationFormat { - get { return Settings.GenerateControllerInterfaces; } - set { Settings.GenerateControllerInterfaces = value; } + get { return Settings.ControllerGenerationFormat; } + set { Settings.ControllerGenerationFormat = value; } } - - [Argument(Name = "GenerateAbstractControllers", Description = "The controller base class (empty for 'ApiController').", IsRequired = false)] - public bool GenerateAbstractControllers - { - get { return Settings.GenerateAbstractControllers; } - set { Settings.GenerateAbstractControllers = value; } - } - + public override async Task RunAsync(CommandLineProcessor processor, IConsoleHost host) { var code = await RunAsync(); From d8ec60efc2c07b771c4315406f3607ef0403c46c Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Wed, 16 Aug 2017 10:47:12 +0100 Subject: [PATCH 09/10] template changed --- .../Templates/ControllerTemplate.cs | 406 ++++++++++++++---- 1 file changed, 322 insertions(+), 84 deletions(-) diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.cs b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.cs index 699a03040e..488d11c283 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.cs +++ b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.cs @@ -16,7 +16,7 @@ namespace NSwag.CodeGeneration.CSharp.Templates /// Class to produce the template output /// - #line 1 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 1 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" [global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.TextTemplating", "15.0.0.0")] internal partial class ControllerTemplate : ControllerTemplateBase { @@ -28,252 +28,268 @@ public virtual string TransformText() { this.Write("\r\n"); - #line 4 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" - if (Model.WrapResponses){ + #line 4 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + + var isPartial = Model.ControllerGenerationFormat == "partial"; + var isAbstract = Model.ControllerGenerationFormat == "abstract"; + #line default #line hidden - this.Write("using System.Net.Http;\r\n"); + this.Write("\r\n"); - #line 6 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" - } + #line 9 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + if (isPartial){ #line default #line hidden - this.Write("\r\n[System.CodeDom.Compiler.GeneratedCode(\"NSwag\", \""); + this.Write("[System.CodeDom.Compiler.GeneratedCode(\"NSwag\", \""); - #line 8 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 9 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(SwaggerDocument.ToolchainVersion)); #line default #line hidden this.Write("\")]\r\npublic interface I"); - #line 9 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 10 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("Controller\r\n{\r\n"); - #line 11 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 12 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var operation in Model.Operations){ #line default #line hidden - #line 12 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(operation.HasSummary){ #line default #line hidden this.Write(" /// "); - #line 12 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1))); #line default #line hidden this.Write("\r\n"); - #line 13 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 14 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }foreach (var parameter in operation.Parameters){ #line default #line hidden - #line 14 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 15 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(parameter.HasDescription){ #line default #line hidden this.Write(" /// "); - #line 14 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 15 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1))); #line default #line hidden this.Write("\r\n"); - #line 15 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 16 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }}if(operation.HasResultDescription){ #line default #line hidden this.Write(" /// "); - #line 15 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 16 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1))); #line default #line hidden this.Write("\r\n"); - #line 16 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 17 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }if(operation.IsDeprecated){ #line default #line hidden this.Write(" [System.Obsolete]\r\n"); - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" "); - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ResultType)); #line default #line hidden this.Write(" "); - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("Async("); - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type)); #line default #line hidden this.Write(" "); - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.GenerateOptionalParameters && parameter.IsOptional){ #line default #line hidden this.Write(" = null"); - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 17 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default #line hidden this.Write(");\r\n\r\n"); - #line 19 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 20 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +} + + #line default + #line hidden + this.Write("}\r\n"); + + #line 22 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden - this.Write("}\r\n\r\n[System.CodeDom.Compiler.GeneratedCode(\"NSwag\", \""); + this.Write("[System.CodeDom.Compiler.GeneratedCode(\"NSwag\", \""); - #line 22 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 23 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(SwaggerDocument.ToolchainVersion)); #line default #line hidden this.Write("\")]\r\n"); - #line 23 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 24 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.HasBasePath){ #line default #line hidden this.Write("[System.Web.Http.RoutePrefix(\""); - #line 24 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 25 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.BasePath)); #line default #line hidden this.Write("\")]\r\n"); - #line 25 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 26 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } + #line default + #line hidden + + #line 27 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + if (isPartial){ + #line default #line hidden this.Write("public partial class "); - #line 26 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("Controller : "); - #line 26 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.HasBaseClass){ #line default #line hidden - #line 26 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.BaseClass)); #line default #line hidden - #line 26 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }else{ #line default #line hidden this.Write("System.Web.Http.ApiController"); - #line 26 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" \r\n{\r\n private I"); - #line 28 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 30 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("Controller _implementation; \r\n\r\n public "); - #line 30 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 32 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("Controller(I"); - #line 30 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 32 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default @@ -281,208 +297,208 @@ public virtual string TransformText() this.Write("Controller implementation)\r\n {\r\n _implementation = implementation; \r\n " + " }\r\n\r\n"); - #line 35 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 37 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var operation in Model.Operations){ #line default #line hidden - #line 36 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 38 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(operation.HasSummary){ #line default #line hidden this.Write(" /// "); - #line 36 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 38 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1))); #line default #line hidden this.Write("\r\n"); - #line 37 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 39 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }foreach (var parameter in operation.Parameters){ #line default #line hidden - #line 38 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(parameter.HasDescription){ #line default #line hidden this.Write(" /// "); - #line 38 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1))); #line default #line hidden this.Write("\r\n"); - #line 39 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 41 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }}if(operation.HasResultDescription){ #line default #line hidden this.Write(" /// "); - #line 39 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 41 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1))); #line default #line hidden this.Write("\r\n"); - #line 40 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 42 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }if(operation.IsDeprecated){ #line default #line hidden this.Write(" [System.Obsolete]\r\n"); - #line 41 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" [System.Web.Http.Http"); - #line 41 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.HttpMethodUpper)); #line default #line hidden this.Write(", System.Web.Http.Route(\""); - #line 41 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.Path)); #line default #line hidden this.Write("\")]\r\n public "); - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.WrapResponses){ #line default #line hidden this.Write("async System.Threading.Tasks.Task"); - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }else{ #line default #line hidden - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ResultType)); #line default #line hidden - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" "); - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("("); - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type)); #line default #line hidden this.Write(" "); - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.GenerateOptionalParameters && parameter.IsOptional){ #line default #line hidden this.Write(" = null"); - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 42 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default #line hidden this.Write(")\r\n { \r\n"); - #line 44 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 46 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if (Model.WrapResponses){ #line default #line hidden this.Write(" var result = await _implementation."); - #line 45 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("Async("); - #line 45 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 45 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 45 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 45 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default @@ -491,14 +507,14 @@ public virtual string TransformText() "stem.Net.HttpStatusCode), result.StatusCode);\r\n HttpResponseMessage respo" + "nse = Request.CreateResponse(status"); - #line 48 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 50 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(operation.UnwrappedResultType != "void"){ #line default #line hidden this.Write(", result.Result"); - #line 48 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 50 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default @@ -506,59 +522,281 @@ public virtual string TransformText() this.Write(");\r\n \r\n foreach (var header in result.Headers)\r\n response.He" + "aders.Add(header.Key, header.Value);\r\n\r\n return response;\r\n"); - #line 54 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 56 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }else{ #line default #line hidden this.Write(" return _implementation."); - #line 55 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("Async("); - #line 55 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 55 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 55 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 55 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default #line hidden this.Write(");\r\n"); - #line 56 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 58 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" \r\n }\r\n\r\n"); - #line 60 "C:\Data\Projects\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 62 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +} + + #line default + #line hidden + this.Write("}\r\n"); + + #line 64 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + }else if (isAbstract){ + + #line default + #line hidden + this.Write("\r\npublic abstract class "); + + #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); + + #line default + #line hidden + this.Write("ControllerBase : "); + + #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +if(Model.HasBaseClass){ + + #line default + #line hidden + + #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(Model.BaseClass)); + + #line default + #line hidden + + #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +}else{ + + #line default + #line hidden + this.Write("System.Web.Http.ApiController"); + + #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +} + + #line default + #line hidden + this.Write("\r\n{\r\n"); + + #line 69 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +foreach(var operation in Model.Operations){ + + #line default + #line hidden + + #line 70 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + if(operation.HasSummary){ + + #line default + #line hidden + this.Write(" /// "); + + #line 70 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1))); + + #line default + #line hidden + this.Write("\r\n"); + + #line 71 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + }foreach (var parameter in operation.Parameters){ + + #line default + #line hidden + + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + if(parameter.HasDescription){ + + #line default + #line hidden + this.Write(" /// "); + + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1))); + + #line default + #line hidden + this.Write("\r\n"); + + #line 73 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + }}if(operation.HasResultDescription){ + + #line default + #line hidden + this.Write(" /// "); + + #line 73 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1))); + + #line default + #line hidden + this.Write("\r\n"); + + #line 74 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + }if(operation.IsDeprecated){ + + #line default + #line hidden + this.Write(" [System.Obsolete]\r\n"); + + #line 75 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + } + + #line default + #line hidden + this.Write(" [System.Web.Http.Http"); + + #line 75 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(operation.HttpMethodUpper)); + + #line default + #line hidden + this.Write(", System.Web.Http.Route(\""); + + #line 75 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(operation.Path)); + + #line default + #line hidden + this.Write("\")]\r\n\tpublic abstract "); + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + if(Model.WrapResponses){ + + #line default + #line hidden + this.Write("System.Threading.Tasks.Task"); + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +}else{ + + #line default + #line hidden + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(operation.ResultType)); + + #line default + #line hidden + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +} + + #line default + #line hidden + this.Write(" "); + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); + + #line default + #line hidden + this.Write("("); + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +foreach(var parameter in operation.Parameters){ + + #line default + #line hidden + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type)); + + #line default + #line hidden + this.Write(" "); + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); + + #line default + #line hidden + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + if(Model.GenerateOptionalParameters && parameter.IsOptional){ + + #line default + #line hidden + this.Write(" = null"); + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +} + + #line default + #line hidden + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + if(!parameter.IsLast){ + + #line default + #line hidden + this.Write(", "); + + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +}} + + #line default + #line hidden + this.Write(");\r\n\r\n"); + + #line 78 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write("}\r\n"); + + #line 80 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" +} + + #line default + #line hidden + this.Write("\r\n\r\n"); return this.GenerationEnvironment.ToString(); } } From 53a086a69edfa60b363e7677849cd6b409c33cd6 Mon Sep 17 00:00:00 2001 From: Gozde Ozdemir Date: Wed, 16 Aug 2017 15:45:38 +0100 Subject: [PATCH 10/10] Changes consist of owners comments We should call the setting ControllerStyle and use an enum ControllerStyle (Partial/Abstract) Move the setting from SwaggerToCSharpGeneratorSettings to SwaggerToCSharpWebApiControllerGeneratorSettings Please create properties on the template model (GeneratePartialControllers/GenerateAbstractControllers) instead of inline in the template (this way we can better move to another template engine later) --- .../ControllerGenerationFormatTests.cs | 5 +- .../Models/CSharpControllerStyleEnum.cs | 19 ++ .../Models/CSharpControllerTemplateModel.cs | 7 +- .../SwaggerToCSharpGeneratorSettings.cs | 5 - ...CSharpWebApiControllerGeneratorSettings.cs | 6 + .../Templates/ControllerTemplate.cs | 240 +++++++++--------- .../Templates/ControllerTemplate.tt | 12 +- .../ClientGeneratorBaseSettings.cs | 6 +- .../SwaggerToCSharpControllerCommand.cs | 13 +- 9 files changed, 162 insertions(+), 151 deletions(-) create mode 100644 src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerStyleEnum.cs diff --git a/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs b/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs index 1b2a679042..ec0f20c016 100644 --- a/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs +++ b/src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs @@ -2,6 +2,7 @@ using System.Threading.Tasks; using System.Web.Http; using Microsoft.VisualStudio.TestTools.UnitTesting; +using NSwag.CodeGeneration.CSharp.Models; using NSwag.SwaggerGeneration.WebApi; namespace NSwag.CodeGeneration.CSharp.Tests @@ -34,7 +35,7 @@ public async Task When_controllergenerationformat_abstract_then_abstractcontroll //// Act var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings { - ControllerGenerationFormat = "abstract", + ControllerStyle = CSharpControllerStyleEnum.Abstract, }); var code = codeGen.GenerateFile(); @@ -55,7 +56,7 @@ public async Task When_controllergenerationformat_abstract_then_partialcontrolle //// Act var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings { - ControllerGenerationFormat = "partial", + ControllerStyle = CSharpControllerStyleEnum.Partial, }); var code = codeGen.GenerateFile(); diff --git a/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerStyleEnum.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerStyleEnum.cs new file mode 100644 index 0000000000..1937e9fc3f --- /dev/null +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerStyleEnum.cs @@ -0,0 +1,19 @@ +//----------------------------------------------------------------------- +// +// Copyright (c) Rico Suter. All rights reserved. +// +// https://github.com/NSwag/NSwag/blob/master/LICENSE.md +// Rico Suter, mail@rsuter.com +//----------------------------------------------------------------------- + +namespace NSwag.CodeGeneration.CSharp.Models +{ + /// The CSharp controller style enum. + public enum CSharpControllerStyleEnum + { + /// Generates partial controllers. + Partial, + /// Generates abstract controllers. + Abstract + } +} \ No newline at end of file diff --git a/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs b/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs index 35c02504ec..32506d14cd 100644 --- a/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs +++ b/src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerTemplateModel.cs @@ -65,7 +65,10 @@ public CSharpControllerTemplateModel( /// Gets a value indicating whether to generate optional parameters. public bool GenerateOptionalParameters => _settings.GenerateOptionalParameters; - /// Gets a value indicating whether to controller generation format.(default: partial;abstract, partial.) - public string ControllerGenerationFormat => _settings.ControllerGenerationFormat; + /// Gets a value indicating whether to controller generation format (partial) + public bool GeneratePartialControllers => _settings.ControllerStyle.CompareTo(CSharpControllerStyleEnum.Partial)==0; + + /// Gets a value indicating whether to controller generation format (partial) + public bool GenerateAbstractControllers => _settings.ControllerStyle.CompareTo(CSharpControllerStyleEnum.Abstract) == 0; } } \ No newline at end of file diff --git a/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs b/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs index e1c47c03ef..be6cb4eedd 100644 --- a/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs +++ b/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpGeneratorSettings.cs @@ -28,7 +28,6 @@ protected SwaggerToCSharpGeneratorSettings() GenerateResponseClasses = true; ResponseClass = "SwaggerResponse"; - ControllerGenerationFormat = "partial"; } /// Gets or sets the CSharp generator settings. @@ -51,9 +50,5 @@ protected SwaggerToCSharpGeneratorSettings() /// Gets or sets the name of the response class (supports the '{controller}' placeholder). public string ResponseClass { get; set; } - - /// Gets a value indicating whether to controller generation format.(default: partial;abstract, partial.) - public string ControllerGenerationFormat { get; set; } - } } \ No newline at end of file diff --git a/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpWebApiControllerGeneratorSettings.cs b/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpWebApiControllerGeneratorSettings.cs index 841b40948b..c4615df0b4 100644 --- a/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpWebApiControllerGeneratorSettings.cs +++ b/src/NSwag.CodeGeneration.CSharp/SwaggerToCSharpWebApiControllerGeneratorSettings.cs @@ -6,6 +6,8 @@ // Rico Suter, mail@rsuter.com //----------------------------------------------------------------------- +using NSwag.CodeGeneration.CSharp.Models; + namespace NSwag.CodeGeneration.CSharp { /// Settings for the . @@ -16,9 +18,13 @@ public SwaggerToCSharpWebApiControllerGeneratorSettings() { ClassName = "{controller}"; CSharpGeneratorSettings.ArrayType = "System.Collections.Generic.List"; + ControllerStyle = CSharpControllerStyleEnum.Partial; } /// Gets or sets the full name of the base class. public string ControllerBaseClass { get; set; } + + /// Gets a value indicating whether to controller generation style.(default: partial;abstract, partial.) + public CSharpControllerStyleEnum ControllerStyle { get; set; } } } \ No newline at end of file diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.cs b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.cs index 488d11c283..339e0bcf5a 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.cs +++ b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.cs @@ -29,267 +29,257 @@ public virtual string TransformText() this.Write("\r\n"); #line 4 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" - - var isPartial = Model.ControllerGenerationFormat == "partial"; - var isAbstract = Model.ControllerGenerationFormat == "abstract"; - - - #line default - #line hidden - this.Write("\r\n"); - - #line 9 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" - if (isPartial){ + if (Model.GeneratePartialControllers){ #line default #line hidden this.Write("[System.CodeDom.Compiler.GeneratedCode(\"NSwag\", \""); - #line 9 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 4 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(SwaggerDocument.ToolchainVersion)); #line default #line hidden this.Write("\")]\r\npublic interface I"); - #line 10 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 5 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("Controller\r\n{\r\n"); - #line 12 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 7 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var operation in Model.Operations){ #line default #line hidden - #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 8 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(operation.HasSummary){ #line default #line hidden this.Write(" /// "); - #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 8 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1))); #line default #line hidden this.Write("\r\n"); - #line 14 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 9 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }foreach (var parameter in operation.Parameters){ #line default #line hidden - #line 15 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 10 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(parameter.HasDescription){ #line default #line hidden this.Write(" /// "); - #line 15 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 10 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1))); #line default #line hidden this.Write("\r\n"); - #line 16 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 11 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }}if(operation.HasResultDescription){ #line default #line hidden this.Write(" /// "); - #line 16 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 11 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1))); #line default #line hidden this.Write("\r\n"); - #line 17 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 12 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }if(operation.IsDeprecated){ #line default #line hidden this.Write(" [System.Obsolete]\r\n"); - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" "); - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ResultType)); #line default #line hidden this.Write(" "); - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("Async("); - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type)); #line default #line hidden this.Write(" "); - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.GenerateOptionalParameters && parameter.IsOptional){ #line default #line hidden this.Write(" = null"); - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 13 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default #line hidden this.Write(");\r\n\r\n"); - #line 20 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 15 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden - this.Write("}\r\n"); + this.Write("}\r\n\r\n"); - #line 22 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 18 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write("[System.CodeDom.Compiler.GeneratedCode(\"NSwag\", \""); - #line 23 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 19 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(SwaggerDocument.ToolchainVersion)); #line default #line hidden this.Write("\")]\r\n"); - #line 24 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 20 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.HasBasePath){ #line default #line hidden this.Write("[System.Web.Http.RoutePrefix(\""); - #line 25 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 21 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.BasePath)); #line default #line hidden this.Write("\")]\r\n"); - #line 26 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 22 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden - #line 27 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" - if (isPartial){ + #line 23 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + if (Model.GeneratePartialControllers){ #line default #line hidden this.Write("public partial class "); - #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 24 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("Controller : "); - #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 24 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.HasBaseClass){ #line default #line hidden - #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 24 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.BaseClass)); #line default #line hidden - #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 24 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }else{ #line default #line hidden this.Write("System.Web.Http.ApiController"); - #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 24 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" \r\n{\r\n private I"); - #line 30 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 26 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("Controller _implementation; \r\n\r\n public "); - #line 32 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("Controller(I"); - #line 32 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 28 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default @@ -297,208 +287,208 @@ public virtual string TransformText() this.Write("Controller implementation)\r\n {\r\n _implementation = implementation; \r\n " + " }\r\n\r\n"); - #line 37 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 33 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var operation in Model.Operations){ #line default #line hidden - #line 38 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 34 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(operation.HasSummary){ #line default #line hidden this.Write(" /// "); - #line 38 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 34 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1))); #line default #line hidden this.Write("\r\n"); - #line 39 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 35 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }foreach (var parameter in operation.Parameters){ #line default #line hidden - #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 36 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(parameter.HasDescription){ #line default #line hidden this.Write(" /// "); - #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 36 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1))); #line default #line hidden this.Write("\r\n"); - #line 41 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 37 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }}if(operation.HasResultDescription){ #line default #line hidden this.Write(" /// "); - #line 41 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 37 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1))); #line default #line hidden this.Write("\r\n"); - #line 42 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 38 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }if(operation.IsDeprecated){ #line default #line hidden this.Write(" [System.Obsolete]\r\n"); - #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 39 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" [System.Web.Http.Http"); - #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 39 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.HttpMethodUpper)); #line default #line hidden this.Write(", System.Web.Http.Route(\""); - #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 39 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.Path)); #line default #line hidden this.Write("\")]\r\n public "); - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.WrapResponses){ #line default #line hidden this.Write("async System.Threading.Tasks.Task"); - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }else{ #line default #line hidden - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ResultType)); #line default #line hidden - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" "); - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("("); - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type)); #line default #line hidden this.Write(" "); - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.GenerateOptionalParameters && parameter.IsOptional){ #line default #line hidden this.Write(" = null"); - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 44 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 40 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default #line hidden this.Write(")\r\n { \r\n"); - #line 46 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 42 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if (Model.WrapResponses){ #line default #line hidden this.Write(" var result = await _implementation."); - #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("Async("); - #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 47 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 43 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default @@ -507,14 +497,14 @@ public virtual string TransformText() "stem.Net.HttpStatusCode), result.StatusCode);\r\n HttpResponseMessage respo" + "nse = Request.CreateResponse(status"); - #line 50 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 46 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(operation.UnwrappedResultType != "void"){ #line default #line hidden this.Write(", result.Result"); - #line 50 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 46 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default @@ -522,276 +512,276 @@ public virtual string TransformText() this.Write(");\r\n \r\n foreach (var header in result.Headers)\r\n response.He" + "aders.Add(header.Key, header.Value);\r\n\r\n return response;\r\n"); - #line 56 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 52 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }else{ #line default #line hidden this.Write(" return _implementation."); - #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 53 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("Async("); - #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 53 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 53 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 53 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 57 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 53 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default #line hidden this.Write(");\r\n"); - #line 58 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 54 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" \r\n }\r\n\r\n"); - #line 62 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 58 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write("}\r\n"); - #line 64 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" - }else if (isAbstract){ + #line 60 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + }else if (Model.GenerateAbstractControllers){ #line default #line hidden this.Write("\r\npublic abstract class "); - #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 62 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.Class)); #line default #line hidden this.Write("ControllerBase : "); - #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 62 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.HasBaseClass){ #line default #line hidden - #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 62 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(Model.BaseClass)); #line default #line hidden - #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 62 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }else{ #line default #line hidden this.Write("System.Web.Http.ApiController"); - #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 62 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write("\r\n{\r\n"); - #line 69 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 65 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var operation in Model.Operations){ #line default #line hidden - #line 70 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(operation.HasSummary){ #line default #line hidden this.Write(" /// "); - #line 70 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 66 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1))); #line default #line hidden this.Write("\r\n"); - #line 71 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 67 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }foreach (var parameter in operation.Parameters){ #line default #line hidden - #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 68 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(parameter.HasDescription){ #line default #line hidden this.Write(" /// "); - #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 68 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(parameter.Description, 1))); #line default #line hidden this.Write("\r\n"); - #line 73 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 69 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }}if(operation.HasResultDescription){ #line default #line hidden this.Write(" /// "); - #line 73 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 69 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(ConversionUtilities.ConvertCSharpDocBreaks(operation.ResultDescription, 1))); #line default #line hidden this.Write("\r\n"); - #line 74 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 70 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }if(operation.IsDeprecated){ #line default #line hidden this.Write(" [System.Obsolete]\r\n"); - #line 75 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 71 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" [System.Web.Http.Http"); - #line 75 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 71 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.HttpMethodUpper)); #line default #line hidden this.Write(", System.Web.Http.Route(\""); - #line 75 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 71 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.Path)); #line default #line hidden this.Write("\")]\r\n\tpublic abstract "); - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.WrapResponses){ #line default #line hidden this.Write("System.Threading.Tasks.Task"); - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }else{ #line default #line hidden - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ResultType)); #line default #line hidden - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write(" "); - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(operation.ActualOperationName)); #line default #line hidden this.Write("("); - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" foreach(var parameter in operation.Parameters){ #line default #line hidden - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.Type)); #line default #line hidden this.Write(" "); - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" this.Write(this.ToStringHelper.ToStringWithCulture(parameter.VariableName)); #line default #line hidden - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(Model.GenerateOptionalParameters && parameter.IsOptional){ #line default #line hidden this.Write(" = null"); - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" if(!parameter.IsLast){ #line default #line hidden this.Write(", "); - #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 72 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" }} #line default #line hidden this.Write(");\r\n\r\n"); - #line 78 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 74 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default #line hidden this.Write("}\r\n"); - #line 80 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" + #line 76 "E:\dev\NSwag\src\NSwag.CodeGeneration.CSharp\Templates\ControllerTemplate.tt" } #line default diff --git a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt index 70f6a749cd..780ce9dc65 100644 --- a/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt +++ b/src/NSwag.CodeGeneration.CSharp/Templates/ControllerTemplate.tt @@ -1,12 +1,7 @@ <#@ template visibility="internal" #> <#@ import namespace="NJsonSchema" #> -<# - var isPartial = Model.ControllerGenerationFormat == "partial"; - var isAbstract = Model.ControllerGenerationFormat == "abstract"; -#> - -<# if (isPartial){#>[System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] +<# if (Model.GeneratePartialControllers){#>[System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] public interface I<#=Model.Class#>Controller { <#foreach(var operation in Model.Operations){#> @@ -19,12 +14,13 @@ public interface I<#=Model.Class#>Controller <#}#> } + <#}#> [System.CodeDom.Compiler.GeneratedCode("NSwag", "<#=SwaggerDocument.ToolchainVersion#>")] <#if(Model.HasBasePath){#> [System.Web.Http.RoutePrefix("<#=Model.BasePath#>")] <#}#> -<# if (isPartial){#> +<# if (Model.GeneratePartialControllers){#> public partial class <#=Model.Class#>Controller : <#if(Model.HasBaseClass){#><#=Model.BaseClass#><#}else{#>System.Web.Http.ApiController<#}#> { private I<#=Model.Class#>Controller _implementation; @@ -61,7 +57,7 @@ public partial class <#=Model.Class#>Controller : <#if(Model.HasBaseClass){#><#= <#}#> } -<# }else if (isAbstract){#> +<# }else if (Model.GenerateAbstractControllers){#> public abstract class <#=Model.Class#>ControllerBase : <#if(Model.HasBaseClass){#><#=Model.BaseClass#><#}else{#>System.Web.Http.ApiController<#}#> diff --git a/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs b/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs index 037e4964b7..c4185c22eb 100644 --- a/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs +++ b/src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs @@ -23,7 +23,6 @@ protected ClientGeneratorBaseSettings() OperationNameGenerator = new MultipleClientsFromOperationIdOperationNameGenerator(); ParameterNameGenerator = new DefaultParameterNameGenerator(); - ControllerGenerationFormat = "partial"; } /// Gets the code generator settings. @@ -47,10 +46,7 @@ protected ClientGeneratorBaseSettings() /// Gets or sets a value indicating whether to reorder parameters (required first, optional at the end) and generate optional C# parameters (default: true). public bool GenerateOptionalParameters { get; set; } - /// Gets a value indicating whether to controller generation format.(default: partial;abstract, partial.) - public string ControllerGenerationFormat { get; set; } - - /// Gets or sets the parameter name generator. + /// Gets or sets the parameter name generator. public IParameterNameGenerator ParameterNameGenerator { get; set; } /// Generates the name of the controller based on the provided settings. diff --git a/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs b/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs index d6aff042db..0d894a0d65 100644 --- a/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs +++ b/src/NSwag.Commands/Commands/SwaggerToCSharpControllerCommand.cs @@ -9,6 +9,7 @@ using System.Threading.Tasks; using NConsole; using NSwag.CodeGeneration.CSharp; +using NSwag.CodeGeneration.CSharp.Models; #pragma warning disable 1591 @@ -28,11 +29,15 @@ public string ControllerBaseClass set { Settings.ControllerBaseClass = value; } } - [Argument(Name = "ControllerGenerationFormat", Description = "The controller generation format (default: partial;abstract, partial.).", IsRequired = false)] - public string ControllerGenerationFormat + [Argument(Name = "ControllerStyle", Description = "The controller generation style (default: partial;abstract, partial.).", IsRequired = false)] + public string ControllerStyle { - get { return Settings.ControllerGenerationFormat; } - set { Settings.ControllerGenerationFormat = value; } + get { return Settings.ControllerStyle.ToString(); } + set + { + CSharpControllerStyleEnum enumValue; + Settings.ControllerStyle = System.Enum.TryParse(value, true, out enumValue) ? enumValue : CSharpControllerStyleEnum.Partial; + } } public override async Task RunAsync(CommandLineProcessor processor, IConsoleHost host)