Skip to content

Commit 344aee9

Browse files
authored
Added feature to suppress the generation of the output of client classes and interfaces. (#4571)
* Added feature to suppress the generation of the output of client classes and interfaces. * Added UI for SuppressClientClassesOutput and SuppressClientInterfacesOutput. * Added documentation.
1 parent aa59e79 commit 344aee9

File tree

7 files changed

+96
-3
lines changed

7 files changed

+96
-3
lines changed

docs/tutorials/GenerateProxyClientWithCLI/generate-proxy-client.md

+2
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ nswag run sample.nswag /runtime:Net50
7777
"codeGenerators": {
7878
"openApiToCSharpClient": {
7979
"generateClientClasses": true,
80+
"suppressClientClassesOutput": false,
8081
"generateClientInterfaces": true,
82+
"suppressClientInterfacesOutput": false,
8183
"generateDtoTypes": true,
8284
"injectHttpClient": true,
8385
"disposeHttpClient": true,

src/NSwag.CodeGeneration.CSharp.Tests/CSharpClientSettingsTests.cs

+52
Original file line numberDiff line numberDiff line change
@@ -173,5 +173,57 @@ public async Task When_client_base_interface_is_specified_then_client_interface_
173173
// Assert
174174
Assert.Contains("public partial interface IFooClient : IClientBase", code);
175175
}
176+
177+
[Fact]
178+
public async Task When_client_class_generation_is_enabled_and_suppressed_then_client_class_is_not_generated()
179+
{
180+
// Arrange
181+
var swaggerGenerator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings
182+
{
183+
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings()
184+
});
185+
186+
var document = await swaggerGenerator.GenerateForControllerAsync<FooController>();
187+
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings
188+
{
189+
GenerateClientClasses = true,
190+
SuppressClientClassesOutput = true,
191+
GenerateClientInterfaces = true,
192+
// SuppressClientInterfacesOutput = false, // default
193+
});
194+
195+
// Act
196+
var code = generator.GenerateFile();
197+
198+
// Assert
199+
Assert.Contains("public partial interface IFooClient", code);
200+
Assert.DoesNotContain("public partial class FooClient : IFooClient", code);
201+
}
202+
203+
[Fact]
204+
public async Task When_client_interface_generation_is_enabled_and_suppressed_then_client_interface_is_not_generated()
205+
{
206+
// Arrange
207+
var swaggerGenerator = new WebApiOpenApiDocumentGenerator(new WebApiOpenApiDocumentGeneratorSettings
208+
{
209+
SchemaSettings = new NewtonsoftJsonSchemaGeneratorSettings()
210+
});
211+
212+
var document = await swaggerGenerator.GenerateForControllerAsync<FooController>();
213+
var generator = new CSharpClientGenerator(document, new CSharpClientGeneratorSettings
214+
{
215+
GenerateClientClasses = true,
216+
// SuppressClientClassesOutput = false, // default
217+
GenerateClientInterfaces = true,
218+
SuppressClientInterfacesOutput = true,
219+
});
220+
221+
// Act
222+
var code = generator.GenerateFile();
223+
224+
// Assert
225+
Assert.DoesNotContain("public partial interface IFooClient", code);
226+
Assert.Contains("public partial class FooClient : IFooClient", code);
227+
}
176228
}
177229
}

src/NSwag.CodeGeneration.CSharp/CSharpClientGenerator.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,17 @@ protected override IEnumerable<CodeArtifact> GenerateClientTypes(string controll
5858
var model = new CSharpClientTemplateModel(controllerName, controllerClassName, operations, exceptionSchema, _document, Settings);
5959
if (model.HasOperations)
6060
{
61-
if (model.GenerateClientInterfaces)
61+
if (model.GenerateClientInterfaces && !model.SuppressClientInterfacesOutput)
6262
{
6363
var interfaceTemplate = Settings.CSharpGeneratorSettings.TemplateFactory.CreateTemplate("CSharp", "Client.Interface", model);
6464
yield return new CodeArtifact(model.Class, CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Contract, interfaceTemplate);
6565
}
6666

67-
var classTemplate = Settings.CSharpGeneratorSettings.TemplateFactory.CreateTemplate("CSharp", "Client.Class", model);
68-
yield return new CodeArtifact(model.Class, CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Client, classTemplate);
67+
if (!model.SuppressClientClassesOutput)
68+
{
69+
var classTemplate = Settings.CSharpGeneratorSettings.TemplateFactory.CreateTemplate("CSharp", "Client.Class", model);
70+
yield return new CodeArtifact(model.Class, CodeArtifactType.Class, CodeArtifactLanguage.CSharp, CodeArtifactCategory.Client, classTemplate);
71+
}
6972
}
7073
}
7174

src/NSwag.CodeGeneration.CSharp/Models/CSharpClientTemplateModel.cs

+6
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,12 @@ public CSharpClientTemplateModel(
8383
/// <summary>Gets a value indicating whether to generate client interfaces.</summary>
8484
public bool GenerateClientInterfaces => _settings.GenerateClientInterfaces;
8585

86+
/// <summary>Gets a value indicating whether to generate the output of client interfaces.</summary>
87+
public bool SuppressClientInterfacesOutput => _settings.SuppressClientInterfacesOutput;
88+
89+
/// <summary>Gets a value indicating whether to generate the output of client classes.</summary>
90+
public bool SuppressClientClassesOutput => _settings.SuppressClientClassesOutput;
91+
8692
/// <summary>Gets client base interface.</summary>
8793
public string ClientBaseInterface => _settings.ClientBaseInterface;
8894

src/NSwag.CodeGeneration/ClientGeneratorBaseSettings.cs

+8
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ public abstract class ClientGeneratorBaseSettings
1919
protected ClientGeneratorBaseSettings()
2020
{
2121
GenerateClientClasses = true;
22+
SuppressClientClassesOutput = false;
23+
SuppressClientInterfacesOutput = false;
2224
GenerateDtoTypes = true;
2325

2426
OperationNameGenerator = new MultipleClientsFromOperationIdOperationNameGenerator();
@@ -43,9 +45,15 @@ protected ClientGeneratorBaseSettings()
4345
/// <summary>Gets or sets a value indicating whether to generate interfaces for the client classes (default: false).</summary>
4446
public bool GenerateClientInterfaces { get; set; }
4547

48+
/// <summary>Gets or sets a value indicating whether to generate the output of interfaces for the client classes (default: false).</summary>
49+
public bool SuppressClientInterfacesOutput { get; set; }
50+
4651
/// <summary>Gets or sets a value indicating whether to generate client types (default: true).</summary>
4752
public bool GenerateClientClasses { get; set; }
4853

54+
/// <summary>Gets or sets a value indicating whether to generate the output of client types (default: false).</summary>
55+
public bool SuppressClientClassesOutput { get; set; }
56+
4957
/// <summary>Gets or sets the operation name generator.</summary>
5058
public IOperationNameGenerator OperationNameGenerator { get; set; }
5159

src/NSwag.Commands/Commands/CodeGeneration/OpenApiToCSharpClientCommand.cs

+14
Original file line numberDiff line numberDiff line change
@@ -46,13 +46,27 @@ public bool GenerateClientClasses
4646
set { Settings.GenerateClientClasses = value; }
4747
}
4848

49+
[Argument(Name = "SuppressClientClassesOutput", IsRequired = false, Description = "Specifies whether generate output for client classes.")]
50+
public bool SuppressClientClassesOutput
51+
{
52+
get { return Settings.SuppressClientClassesOutput; }
53+
set { Settings.SuppressClientClassesOutput = value; }
54+
}
55+
4956
[Argument(Name = "GenerateClientInterfaces", IsRequired = false, Description = "Specifies whether generate interfaces for the client classes.")]
5057
public bool GenerateClientInterfaces
5158
{
5259
get { return Settings.GenerateClientInterfaces; }
5360
set { Settings.GenerateClientInterfaces = value; }
5461
}
5562

63+
[Argument(Name = "SuppressClientInterfacesOutput", IsRequired = false, Description = "Specifies whether generate output for interfaces for the client classes.")]
64+
public bool SuppressClientInterfacesOutput
65+
{
66+
get { return Settings.SuppressClientInterfacesOutput; }
67+
set { Settings.SuppressClientInterfacesOutput = value; }
68+
}
69+
5670
[Argument(Name = "ClientBaseInterface", IsRequired = false, Description = "Base interface for client interfaces (empty for no client base interface).")]
5771
public string ClientBaseInterface
5872
{

src/NSwagStudio/Views/CodeGenerators/SwaggerToCSharpClientGeneratorView.xaml

+8
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@
9090
ToolTip="GenerateClientClasses"
9191
Content="Generate Client Classes" Margin="0,0,0,12" />
9292

93+
<CheckBox IsChecked="{Binding Command.SuppressClientClassesOutput, Mode=TwoWay}"
94+
ToolTip="SuppressClientClassesOutput"
95+
Content="Suppress output of generated Client Classes" Margin="0,0,0,12" />
96+
9397
<StackPanel Visibility="{Binding Command.GenerateClientClasses, Converter={StaticResource VisibilityConverter}}">
9498
<TextBlock Margin="0,0,0,6" TextWrapping="Wrap">
9599
<Run Text="Operation Generation Mode" FontWeight="Bold" />
@@ -154,6 +158,10 @@
154158
ToolTip="GenerateClientInterfaces"
155159
Content="Generate interfaces for Client classes" Margin="0,0,0,12" />
156160

161+
<CheckBox IsChecked="{Binding Command.SuppressClientInterfacesOutput, Mode=TwoWay}"
162+
ToolTip="SuppressClientInterfacesOutput"
163+
Content="Suppress output of generated interfaces for Client classes" Margin="0,0,0,12" />
164+
157165
<TextBlock Text="Base interface for Client Interfaces (optional)" FontWeight="Bold" Margin="0,0,0,6"
158166
Visibility="{Binding Command.GenerateClientInterfaces, Converter={StaticResource VisibilityConverter}}" />
159167
<TextBox Text="{Binding Command.ClientBaseInterface, Mode=TwoWay}"

0 commit comments

Comments
 (0)