Skip to content

Commit e9fa936

Browse files
[csharp][netcore-httpclient] Refactor of constructors: removed obsolete attribute (#9373)
* Removed obsolete attribute on constructors without HttpClient prameter * add clickable link in the tooltip * update doc, add tests Co-authored-by: William Cheng <wing328hk@gmail.com>
1 parent d21743e commit e9fa936

21 files changed

Lines changed: 287 additions & 71 deletions

File tree

modules/openapi-generator/src/main/resources/csharp-netcore/README.mustache

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,9 @@ services.AddHttpClient<YourApiClass>(httpClient =>
137137
```csharp
138138
using System.Collections.Generic;
139139
using System.Diagnostics;
140+
{{#useHttpClient}}
141+
using System.Net.Http;
142+
{{/useHttpClient}}
140143
using {{packageName}}.{{apiPackage}};
141144
using {{packageName}}.Client;
142145
using {{packageName}}.{{modelPackage}};
@@ -174,7 +177,15 @@ namespace Example
174177
{{/authMethods}}
175178

176179
{{/hasAuthMethods}}
180+
{{#useHttpClient}}
181+
// create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
182+
HttpClient httpClient = new HttpClient();
183+
HttpClientHandler httpClientHandler = new HttpClientHandler();
184+
var apiInstance = new {{classname}}(httpClient, config, httpClientHandler);
185+
{{/useHttpClient}}
186+
{{^useHttpClient}}
177187
var apiInstance = new {{classname}}(config);
188+
{{/useHttpClient}}
178189
{{#allParams}}
179190
{{#isPrimitiveType}}
180191
var {{paramName}} = {{{example}}}; // {{{dataType}}} | {{{description}}}{{^required}} (optional) {{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}

modules/openapi-generator/src/main/resources/csharp-netcore/api_doc.mustache

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Method | HTTP request | Description
2222
```csharp
2323
using System.Collections.Generic;
2424
using System.Diagnostics;
25+
{{#useHttpClient}}
26+
using System.Net.Http;
27+
{{/useHttpClient}}
2528
using {{packageName}}.{{apiPackage}};
2629
using {{packageName}}.Client;
2730
using {{packageName}}.{{modelPackage}};
@@ -58,7 +61,15 @@ namespace Example
5861
{{/authMethods}}
5962

6063
{{/hasAuthMethods}}
64+
{{#useHttpClient}}
65+
// create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
66+
HttpClient httpClient = new HttpClient();
67+
HttpClientHandler httpClientHandler = new HttpClientHandler();
68+
var apiInstance = new {{classname}}(httpClient, config, httpClientHandler);
69+
{{/useHttpClient}}
70+
{{^useHttpClient}}
6171
var apiInstance = new {{classname}}(config);
72+
{{/useHttpClient}}
6273
{{#allParams}}
6374
{{#isPrimitiveType}}
6475
var {{paramName}} = {{{example}}}; // {{{dataType}}} | {{{description}}}{{^required}} (optional) {{/required}}{{#defaultValue}} (default to {{{.}}}){{/defaultValue}}

modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/ApiClient.mustache

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,19 +191,21 @@ namespace {{packageName}}.Client
191191

192192
/// <summary>
193193
/// Initializes a new instance of the <see cref="ApiClient" />, defaulting to the global configurations' base url.
194+
/// **IMPORTANT** This will also create an istance of HttpClient, which is less than ideal.
195+
/// It's better to reuse the <see href="https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net">HttpClient and HttpClientHander</see>.
194196
/// </summary>
195-
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
196197
public ApiClient() :
197198
this({{packageName}}.Client.GlobalConfiguration.Instance.BasePath)
198199
{
199200
}
200201

201202
/// <summary>
202203
/// Initializes a new instance of the <see cref="ApiClient" />.
204+
/// **IMPORTANT** This will also create an istance of HttpClient, which is less than ideal.
205+
/// It's better to reuse the <see href="https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net">HttpClient and HttpClientHander</see>.
203206
/// </summary>
204207
/// <param name="basePath">The target service's base path in URL format.</param>
205208
/// <exception cref="ArgumentException"></exception>
206-
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
207209
public ApiClient(String basePath)
208210
{
209211
if (string.IsNullOrEmpty(basePath)) throw new ArgumentException("basePath cannot be empty");

modules/openapi-generator/src/main/resources/csharp-netcore/libraries/httpclient/api.mustache

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,20 +106,22 @@ namespace {{packageName}}.{{apiPackage}}
106106

107107
/// <summary>
108108
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
109+
/// **IMPORTANT** This will also create an istance of HttpClient, which is less than ideal.
110+
/// It's better to reuse the <see href="https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net">HttpClient and HttpClientHander</see>.
109111
/// </summary>
110112
/// <returns></returns>
111-
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
112113
public {{classname}}() : this((string)null)
113114
{
114115
}
115116

116117
/// <summary>
117118
/// Initializes a new instance of the <see cref="{{classname}}"/> class.
119+
/// **IMPORTANT** This will also create an istance of HttpClient, which is less than ideal.
120+
/// It's better to reuse the <see href="https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net">HttpClient and HttpClientHander</see>.
118121
/// </summary>
119122
/// <param name="basePath">The target service's base path in URL format.</param>
120123
/// <exception cref="ArgumentException"></exception>
121124
/// <returns></returns>
122-
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
123125
public {{classname}}(String basePath)
124126
{
125127
this.Configuration = {{packageName}}.Client.Configuration.MergeConfigurations(
@@ -136,11 +138,12 @@ namespace {{packageName}}.{{apiPackage}}
136138

137139
/// <summary>
138140
/// Initializes a new instance of the <see cref="{{classname}}"/> class using Configuration object.
141+
/// **IMPORTANT** This will also create an istance of HttpClient, which is less than ideal.
142+
/// It's better to reuse the <see href="https://docs.microsoft.com/en-us/dotnet/architecture/microservices/implement-resilient-applications/use-httpclientfactory-to-implement-resilient-http-requests#issues-with-the-original-httpclient-class-available-in-net">HttpClient and HttpClientHander</see>.
139143
/// </summary>
140144
/// <param name="configuration">An instance of Configuration.</param>
141145
/// <exception cref="ArgumentNullException"></exception>
142146
/// <returns></returns>
143-
[Obsolete("Constructors without HttpClient have non-trivial drawbacks and are thus considered deprecated. Check README.md for details.")]
144147
public {{classname}}({{packageName}}.Client.Configuration configuration)
145148
{
146149
if (configuration == null) throw new ArgumentNullException("configuration");

samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ services.AddHttpClient<YourApiClass>(httpClient =>
8383
```csharp
8484
using System.Collections.Generic;
8585
using System.Diagnostics;
86+
using System.Net.Http;
8687
using Org.OpenAPITools.Api;
8788
using Org.OpenAPITools.Client;
8889
using Org.OpenAPITools.Model;
@@ -96,7 +97,10 @@ namespace Example
9697

9798
Configuration config = new Configuration();
9899
config.BasePath = "http://petstore.swagger.io:80/v2";
99-
var apiInstance = new AnotherFakeApi(config);
100+
// create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
101+
HttpClient httpClient = new HttpClient();
102+
HttpClientHandler httpClientHandler = new HttpClientHandler();
103+
var apiInstance = new AnotherFakeApi(httpClient, config, httpClientHandler);
100104
var modelClient = new ModelClient(); // ModelClient | client model
101105
102106
try

samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/docs/AnotherFakeApi.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ To test special tags and operation ID starting with number
1919
```csharp
2020
using System.Collections.Generic;
2121
using System.Diagnostics;
22+
using System.Net.Http;
2223
using Org.OpenAPITools.Api;
2324
using Org.OpenAPITools.Client;
2425
using Org.OpenAPITools.Model;
@@ -31,7 +32,10 @@ namespace Example
3132
{
3233
Configuration config = new Configuration();
3334
config.BasePath = "http://petstore.swagger.io:80/v2";
34-
var apiInstance = new AnotherFakeApi(config);
35+
// create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
36+
HttpClient httpClient = new HttpClient();
37+
HttpClientHandler httpClientHandler = new HttpClientHandler();
38+
var apiInstance = new AnotherFakeApi(httpClient, config, httpClientHandler);
3539
var modelClient = new ModelClient(); // ModelClient | client model
3640
3741
try

samples/client/petstore/csharp-netcore/OpenAPIClient-httpclient/docs/DefaultApi.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ Method | HTTP request | Description
1717
```csharp
1818
using System.Collections.Generic;
1919
using System.Diagnostics;
20+
using System.Net.Http;
2021
using Org.OpenAPITools.Api;
2122
using Org.OpenAPITools.Client;
2223
using Org.OpenAPITools.Model;
@@ -29,7 +30,10 @@ namespace Example
2930
{
3031
Configuration config = new Configuration();
3132
config.BasePath = "http://petstore.swagger.io:80/v2";
32-
var apiInstance = new DefaultApi(config);
33+
// create instances of HttpClient, HttpClientHandler to be reused later with different Api classes
34+
HttpClient httpClient = new HttpClient();
35+
HttpClientHandler httpClientHandler = new HttpClientHandler();
36+
var apiInstance = new DefaultApi(httpClient, config, httpClientHandler);
3337

3438
try
3539
{

0 commit comments

Comments
 (0)