-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathAllowNullableBodyParametersTests.cs
More file actions
148 lines (133 loc) · 4.26 KB
/
AllowNullableBodyParametersTests.cs
File metadata and controls
148 lines (133 loc) · 4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
using Microsoft.AspNetCore.Mvc;
using NJsonSchema.NewtonsoftJson.Generation;
using NSwag.CodeGeneration.OperationNameGenerators;
using NSwag.CodeGeneration.Tests;
using NSwag.Generation.WebApi;
using System.ComponentModel.DataAnnotations;
using System.Reflection;
namespace NSwag.CodeGeneration.CSharp.Tests
{
public class AllowNullableBodyParametersTests
{
[Fact]
public async Task TestNoGuardForOptionalBodyParameter()
{
// Arrange
var swagger =
@"{
""openapi"": ""3.0.1"",
""paths"": {
""/definitions/{definitionId}/elements"": {
""get"": {
""operationId"": ""elements_LIST_1"",
""requestBody"": {
""content"": {
""*/*"": {
""schema"": {
""type"": ""integer"",
""format"": ""int64""
}
}
}
},
""responses"": {
""200"": {
""description"": ""Success""
}
}
}
}
}
}";
var document = await OpenApiDocument.FromJsonAsync(swagger);
// Act
var codeGen = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings()
{
UseBaseUrl = false,
GenerateClientInterfaces = true,
OperationNameGenerator = new SingleClientFromOperationIdOperationNameGenerator(),
CSharpGeneratorSettings =
{
Namespace = VerifyHelper.GetNameSpace(),
},
});
var code = codeGen.GenerateFile();
// Assert
await VerifyHelper.Verify(code);
CSharpCompiler.AssertCompile(code);
}
[Fact]
public async Task TestNullableBodyWithAllowNullableBodyParameters()
{
// Arrange
var generator = await GenerateCode(true);
generator.Settings.CSharpGeneratorSettings.Namespace = VerifyHelper.GetNameSpace();
// Act
var code = generator.GenerateFile();
// Assert
await VerifyHelper.Verify(code);
CSharpCompiler.AssertCompile(code + @"
namespace AllowNullableBodyParametersTests.TestNullableBodyWithAllowNullableBodyParameters
{
public class MyBaseClass
{
public MyBaseClass(MyConfig configuration) {}
}
public class MyConfig {}
}
");
}
[Fact]
public async Task TestNullableBodyWithoutAllowNullableBodyParameters()
{
// Arrange
var generator = await GenerateCode(false);
generator.Settings.CSharpGeneratorSettings.Namespace = VerifyHelper.GetNameSpace();
// Act
var code = generator.GenerateFile();
// Assert
await VerifyHelper.Verify(code);
CSharpCompiler.AssertCompile(code + @"
namespace AllowNullableBodyParametersTests.TestNullableBodyWithoutAllowNullableBodyParameters
{
public class MyBaseClass
{
public MyBaseClass(MyConfig configuration) {}
}
public class MyConfig {}
}
");
}
private static async Task<CSharpClientGenerator> GenerateCode(bool allowNullableBodyParameters)
{
var swaggerGenerator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings
{
AllowNullableBodyParameters = allowNullableBodyParameters,
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings()
});
var document = await swaggerGenerator.GenerateForControllerAsync<TestController>();
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings
{
InjectHttpClient = false,
ConfigurationClass = "MyConfig",
ClientBaseClass = "MyBaseClass"
});
return generator;
}
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
{
}
}
}
}