Skip to content

Commit 480c906

Browse files
committed
Improved C# client template, closes RicoSuter#381
1 parent 47c44d9 commit 480c906

File tree

7 files changed

+322
-347
lines changed

7 files changed

+322
-347
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public void When_ConfigurationClass_is_set_then_correct_ctor_is_generated()
3333
var code = generator.GenerateFile();
3434

3535
//// Assert
36-
Assert.IsTrue(code.Contains("public FooClient(string baseUrl, MyConfig configuration) : base(configuration)"));
36+
Assert.IsTrue(code.Contains("public FooClient(MyConfig configuration) : base(configuration)"));
3737
}
3838

3939
[TestMethod]

src/NSwag.CodeGeneration/CodeGenerators/CSharp/Templates/ClientTemplate.cs

Lines changed: 286 additions & 307 deletions
Large diffs are not rendered by default.

src/NSwag.CodeGeneration/CodeGenerators/CSharp/Templates/ClientTemplate.tt

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -35,30 +35,30 @@ public partial interface I<#=Model.Class#>
3535
public partial class <#=Model.Class#> <#if(Model.HasBaseType){#>: <#}#><#if(Model.HasBaseClass){#><#=Model.BaseClass#><#if(Model.GenerateClientInterfaces){#>, <#}#><#}#><#if(Model.GenerateClientInterfaces){#>I<#=Model.Class#><#}#>
3636

3737
{
38+
private string _baseUrl = "<#=Model.BaseUrl#>";
39+
3840
<#if(Model.HasConfigurationClass){#>
39-
public <#=Model.Class#>() : this("<#=Model.BaseUrl#>", null) { }
40-
41-
public <#=Model.Class#>(string baseUrl) : this(baseUrl, null) { }
42-
43-
public <#=Model.Class#>(string baseUrl, <#=Model.ConfigurationClass#> configuration) : base(configuration)
41+
public <#=Model.Class#>(<#=Model.ConfigurationClass#> configuration) : base(configuration)
4442
{
45-
BaseUrl = baseUrl;
4643
}
47-
<#}else{#>
48-
public <#=Model.Class#>() : this("<#=Model.BaseUrl#>") { }
4944

45+
<#}else if(string.IsNullOrEmpty(Model.BaseUrl)){#>
5046
public <#=Model.Class#>(string baseUrl)
5147
{
5248
BaseUrl = baseUrl;
5349
}
50+
5451
<#}#>
52+
public string BaseUrl
53+
{
54+
get { return _baseUrl; }
55+
set { _baseUrl = value; }
56+
}
5557

5658
partial void PrepareRequest(HttpClient request, ref string url);
5759

5860
partial void ProcessResponse(HttpClient request, HttpResponseMessage response);
5961

60-
public string BaseUrl { get; set; }
61-
6262
<#foreach(var operation in Model.Operations){#>
6363
<# if(operation.HasSummary){#> /// <summary><#=ConversionUtilities.ConvertCSharpDocBreaks(operation.Summary, 1)#></summary>
6464
<# }foreach (var parameter in operation.Parameters){#>

src/NSwag.Integration.ClientPCL.Tests/GeoControllerTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public class GeoControllerTests
1414
public async Task SaveItems()
1515
{
1616
//// Arrange
17-
var geoClient = new GeoClient("http://localhost:13452");
17+
var geoClient = new GeoClient { BaseUrl = "http://localhost:13452" };
1818

1919
//// Act
2020
try
@@ -36,7 +36,7 @@ public async Task SaveItems()
3636
public async Task UploadFile()
3737
{
3838
//// Arrange
39-
var geoClient = new GeoClient("http://localhost:13452");
39+
var geoClient = new GeoClient { BaseUrl = "http://localhost:13452" };
4040

4141
//// Act
4242
var result = await geoClient.UploadFileAsync(new FileParameter(new MemoryStream(new byte[] { 1, 2 })));

src/NSwag.Integration.ClientPCL.Tests/PersonsControllerTests.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ public class PersonsControllerTests
1313
public async Task GetAll_SerializationTest()
1414
{
1515
//// Arrange
16-
var personsClient = new PersonsClient("http://localhost:13452");
16+
var personsClient = new PersonsClient { BaseUrl = "http://localhost:13452" }; ;
1717

1818
//// Act
1919
var persons = await personsClient.GetAllAsync();
@@ -27,7 +27,7 @@ public async Task GetAll_SerializationTest()
2727
public async Task GetAll_InheritanceTest()
2828
{
2929
//// Arrange
30-
var personsClient = new PersonsClient("http://localhost:13452");
30+
var personsClient = new PersonsClient { BaseUrl = "http://localhost:13452" };
3131

3232
//// Act
3333
var persons = await personsClient.GetAllAsync();
@@ -42,7 +42,7 @@ public async Task Throw()
4242
{
4343
//// Arrange
4444
var id = Guid.NewGuid();
45-
var personsClient = new PersonsClient("http://localhost:13452");
45+
var personsClient = new PersonsClient { BaseUrl = "http://localhost:13452" };
4646

4747
//// Act
4848
try

src/NSwag.Integration.ClientPCL/ServiceClients.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,18 @@ namespace NSwag.Integration.ClientPCL
3434
[GeneratedCode("NSwag", "6.21.6156.29508")]
3535
public partial class GeoClient
3636
{
37-
public GeoClient() : this("http://localhost:13452") { }
38-
39-
public GeoClient(string baseUrl)
37+
private string _baseUrl = "http://localhost:13452";
38+
39+
public string BaseUrl
4040
{
41-
BaseUrl = baseUrl;
41+
get { return _baseUrl; }
42+
set { _baseUrl = value; }
4243
}
4344

4445
partial void PrepareRequest(HttpClient request, ref string url);
4546

4647
partial void ProcessResponse(HttpClient request, HttpResponseMessage response);
4748

48-
public string BaseUrl { get; set; }
49-
5049
/// <exception cref="GeoClientException">A server side error occurred.</exception>
5150
public Task FromBodyTestAsync(GeoPoint location)
5251
{
@@ -388,19 +387,18 @@ public async Task<byte[]> GetUploadedFileAsync(int id, CancellationToken cancell
388387
[GeneratedCode("NSwag", "6.21.6156.29508")]
389388
public partial class PersonsClient
390389
{
391-
public PersonsClient() : this("http://localhost:13452") { }
392-
393-
public PersonsClient(string baseUrl)
390+
private string _baseUrl = "http://localhost:13452";
391+
392+
public string BaseUrl
394393
{
395-
BaseUrl = baseUrl;
394+
get { return _baseUrl; }
395+
set { _baseUrl = value; }
396396
}
397397

398398
partial void PrepareRequest(HttpClient request, ref string url);
399399

400400
partial void ProcessResponse(HttpClient request, HttpResponseMessage response);
401401

402-
public string BaseUrl { get; set; }
403-
404402
/// <exception cref="PersonsClientException">A server side error occurred.</exception>
405403
public Task<ObservableCollection<Person>> GetAllAsync()
406404
{

src/NSwag.Integration.Console/ServiceClients.cs

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,18 @@ namespace NSwag.Integration.Console
3434
[GeneratedCode("NSwag", "6.21.6156.29508")]
3535
public partial class GeoClient
3636
{
37-
public GeoClient() : this("http://localhost:13452") { }
38-
39-
public GeoClient(string baseUrl)
37+
private string _baseUrl = "http://localhost:13452";
38+
39+
public string BaseUrl
4040
{
41-
BaseUrl = baseUrl;
41+
get { return _baseUrl; }
42+
set { _baseUrl = value; }
4243
}
4344

4445
partial void PrepareRequest(HttpClient request, ref string url);
4546

4647
partial void ProcessResponse(HttpClient request, HttpResponseMessage response);
4748

48-
public string BaseUrl { get; set; }
49-
5049
/// <exception cref="SwaggerException">A server side error occurred.</exception>
5150
public Task FromBodyTestAsync(GeoPoint location)
5251
{
@@ -388,19 +387,18 @@ public async Task<byte[]> GetUploadedFileAsync(int id, CancellationToken cancell
388387
[GeneratedCode("NSwag", "6.21.6156.29508")]
389388
public partial class PersonsClient
390389
{
391-
public PersonsClient() : this("http://localhost:13452") { }
392-
393-
public PersonsClient(string baseUrl)
390+
private string _baseUrl = "http://localhost:13452";
391+
392+
public string BaseUrl
394393
{
395-
BaseUrl = baseUrl;
394+
get { return _baseUrl; }
395+
set { _baseUrl = value; }
396396
}
397397

398398
partial void PrepareRequest(HttpClient request, ref string url);
399399

400400
partial void ProcessResponse(HttpClient request, HttpResponseMessage response);
401401

402-
public string BaseUrl { get; set; }
403-
404402
/// <exception cref="SwaggerException">A server side error occurred.</exception>
405403
public Task<ObservableCollection<Person>> GetAllAsync()
406404
{

0 commit comments

Comments
 (0)