-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #884 from Aqovia/WebApiContractFirstAsAbstractCont…
…rollers Optional generation of abstract WebAPI controllers
- Loading branch information
Showing
10 changed files
with
478 additions
and
96 deletions.
There are no files selected for viewing
92 changes: 92 additions & 0 deletions
92
src/NSwag.CodeGeneration.CSharp.Tests/ControllerGenerationFormatTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")); | ||
} | ||
} | ||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
19 changes: 19 additions & 0 deletions
19
src/NSwag.CodeGeneration.CSharp/Models/CSharpControllerStyleEnum.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.