forked from RicoSuter/NSwag
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[WIP]Update Client.Class.liquid - issue 2736. AllowEmptyInputInBodyMo…
…delBinding (RicoSuter#3195) * Update Client.Class.liquid - issue 2736. Fix for AllowEmptyInputInBodyModelBinding * Update Client.Class.liquid Co-authored-by: Vitaly Melnik <[email protected]> Co-authored-by: Rico Suter <[email protected]>
- Loading branch information
1 parent
1ffae3a
commit e5d6903
Showing
2 changed files
with
78 additions
and
1 deletion.
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
src/NSwag.CodeGeneration.CSharp.Tests/AllowNullableBodyParametersTests.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,77 @@ | ||
using System.ComponentModel.DataAnnotations; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.Mvc; | ||
using NSwag.Generation.WebApi; | ||
using Xunit; | ||
|
||
namespace NSwag.CodeGeneration.CSharp.Tests | ||
{ | ||
public class AllowNullableBodyParametersTests | ||
{ | ||
[Fact] | ||
public async Task TestNullableBodyWithAllowNullableBodyParameters() | ||
{ | ||
//// Arrange | ||
var generator = await GenerateCode(true); | ||
|
||
//// Act | ||
var code = generator.GenerateFile(); | ||
|
||
//// Assert | ||
Assert.False(HasGuardForBodyIsNull(code)); | ||
} | ||
|
||
[Fact] | ||
public async Task TestNullableBodyWithoutAllowNullableBodyParameters() | ||
{ | ||
//// Arrange | ||
var generator = await GenerateCode(false); | ||
|
||
//// Act | ||
var code = generator.GenerateFile(); | ||
|
||
//// Assert | ||
Assert.True(HasGuardForBodyIsNull(code)); | ||
} | ||
|
||
private static async Task<CSharpClientGenerator> GenerateCode(bool allowNullableBodyParameters) | ||
{ | ||
var swaggerGenerator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings | ||
{ | ||
AllowNullableBodyParameters = allowNullableBodyParameters | ||
}); | ||
var document = await swaggerGenerator.GenerateForControllerAsync<TestController>(); | ||
|
||
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings | ||
{ | ||
InjectHttpClient = false, | ||
ConfigurationClass = "MyConfig", | ||
ClientBaseClass = "MyBaseClass" | ||
}); | ||
return generator; | ||
} | ||
|
||
private static bool HasGuardForBodyIsNull(string code) | ||
{ | ||
return code.Contains("throw new System.ArgumentNullException"); | ||
} | ||
|
||
public class TestController : Controller | ||
{ | ||
[Route("Foo")] | ||
public string Foo([FromBody] [Required] T requiredBody) | ||
{ | ||
return string.Empty; | ||
} | ||
|
||
[Route("Bar")] | ||
public void Bar([FromBody] T notRequiredBody) | ||
{ | ||
} | ||
|
||
public class T | ||
{ | ||
} | ||
} | ||
} | ||
} |
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