Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional generation of abstract WebAPI controllers #884

Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
using System;
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
{
[TestClass]
public class ControllerGenerationFormatTests
{
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_controllergenerationformat_abstract_then_abstractcontroller_is_generated()
{
//// Arrange
var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
var document = await swaggerGen.GenerateForControllerAsync<TestController>();

//// Act
var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings
{
ControllerStyle = CSharpControllerStyleEnum.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_controllergenerationformat_abstract_then_partialcontroller_is_generated()
{
//// Arrange
var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
var document = await swaggerGen.GenerateForControllerAsync<TestController>();

//// Act
var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings
{
ControllerStyle = CSharpControllerStyleEnum.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.IsFalse(code.Contains("abstract class TestController"));
}

[TestMethod]
public async Task When_controllergenerationformat_notsetted_then_partialcontroller_is_generated()
{
//// Arrange
var swaggerGen = new WebApiToSwaggerGenerator(new WebApiToSwaggerGeneratorSettings());
var document = await swaggerGen.GenerateForControllerAsync<TestController>();

//// Act
var codeGen = new SwaggerToCSharpWebApiControllerGenerator(document, new SwaggerToCSharpWebApiControllerGeneratorSettings
{
});
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.IsFalse(code.Contains("abstract class TestController"));
}
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@
</ItemGroup>
<ItemGroup>
<Compile Include="ArrayParameterTests.cs" />
<Compile Include="ControllerGenerationFormatTests.cs" />
<Compile Include="CSharpClientSettingsTests.cs" />
<Compile Include="FileDownloadTests.cs" />
<Compile Include="FormParameterTests.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
//-----------------------------------------------------------------------
// <copyright file="CSharpControllerStyleEnum.cs" company="NSwag">
// Copyright (c) Rico Suter. All rights reserved.
// </copyright>
// <license>https://github.com/NSwag/NSwag/blob/master/LICENSE.md</license>
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

namespace NSwag.CodeGeneration.CSharp.Models
{
/// <summary>The CSharp controller style enum.</summary>
public enum CSharpControllerStyleEnum
{
/// <summary>Generates partial controllers.</summary>
Partial,
/// <summary>Generates abstract controllers.</summary>
Abstract
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,5 +64,11 @@ public CSharpControllerTemplateModel(

/// <summary>Gets a value indicating whether to generate optional parameters.</summary>
public bool GenerateOptionalParameters => _settings.GenerateOptionalParameters;

/// <summary>Gets a value indicating whether to controller generation format (partial)</summary>
public bool GeneratePartialControllers => _settings.ControllerStyle.CompareTo(CSharpControllerStyleEnum.Partial)==0;

/// <summary>Gets a value indicating whether to controller generation format (partial)</summary>
public bool GenerateAbstractControllers => _settings.ControllerStyle.CompareTo(CSharpControllerStyleEnum.Abstract) == 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
// <author>Rico Suter, mail@rsuter.com</author>
//-----------------------------------------------------------------------

using NSwag.CodeGeneration.CSharp.Models;

namespace NSwag.CodeGeneration.CSharp
{
/// <summary>Settings for the <see cref="SwaggerToCSharpWebApiControllerGenerator"/>.</summary>
Expand All @@ -16,9 +18,13 @@ public SwaggerToCSharpWebApiControllerGeneratorSettings()
{
ClassName = "{controller}";
CSharpGeneratorSettings.ArrayType = "System.Collections.Generic.List";
ControllerStyle = CSharpControllerStyleEnum.Partial;
}

/// <summary>Gets or sets the full name of the base class.</summary>
public string ControllerBaseClass { get; set; }

/// <summary>Gets a value indicating whether to controller generation style.(default: partial;abstract, partial.)</summary>
public CSharpControllerStyleEnum ControllerStyle { get; set; }
}
}
Loading