Skip to content

Commit fbb2f1e

Browse files
mario-d-swing328
authored andcommitted
[C#][client][csharp-netcore] Fix csharp netcore defaultheaders (#3562)
* [csharp-netcore] Send default HTTP headers with requests * [csharp-netcore] Add DefaultHeaders in favor of DefaultHeader
1 parent 7520308 commit fbb2f1e

File tree

9 files changed

+132
-36
lines changed

9 files changed

+132
-36
lines changed

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,14 @@ namespace {{packageName}}.Client
270270
}
271271
}
272272

273+
if (configuration.DefaultHeaders != null)
274+
{
275+
foreach (var headerParam in configuration.DefaultHeaders)
276+
{
277+
request.AddHeader(headerParam.Key, headerParam.Value);
278+
}
279+
}
280+
273281
if (options.HeaderParameters != null)
274282
{
275283
foreach (var headerParam in options.HeaderParameters)

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

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ namespace {{packageName}}.Client
3535
#endregion Constants
3636
3737
#region Static Members
38-
38+
3939
/// <summary>
4040
/// Default creation of exceptions for a given method name and response object
4141
/// </summary>
@@ -65,7 +65,7 @@ namespace {{packageName}}.Client
6565
/// Example: http://localhost:3000/v1/
6666
/// </summary>
6767
private String _basePath;
68-
68+
6969
/// <summary>
7070
/// Gets or sets the API key based on the authentication name.
7171
/// This is the key and value comprising the "secret" for acessing an API.
@@ -94,7 +94,7 @@ namespace {{packageName}}.Client
9494
{
9595
UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{packageVersion}}/csharp{{/httpUserAgent}}";
9696
BasePath = "{{{basePath}}}";
97-
DefaultHeader = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>();
97+
DefaultHeaders = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>();
9898
ApiKey = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>();
9999
ApiKeyPrefix = new {{^net35}}Concurrent{{/net35}}Dictionary<string, string>();
100100

@@ -107,25 +107,25 @@ namespace {{packageName}}.Client
107107
/// </summary>
108108
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
109109
public Configuration(
110-
IDictionary<string, string> defaultHeader,
110+
IDictionary<string, string> defaultHeaders,
111111
IDictionary<string, string> apiKey,
112112
IDictionary<string, string> apiKeyPrefix,
113113
string basePath = "{{{basePath}}}") : this()
114114
{
115115
if (string.{{^net35}}IsNullOrWhiteSpace{{/net35}}{{#net35}}IsNullOrEmpty{{/net35}}(basePath))
116116
throw new ArgumentException("The provided basePath is invalid.", "basePath");
117-
if (defaultHeader == null)
118-
throw new ArgumentNullException("defaultHeader");
117+
if (defaultHeaders == null)
118+
throw new ArgumentNullException("defaultHeaders");
119119
if (apiKey == null)
120120
throw new ArgumentNullException("apiKey");
121121
if (apiKeyPrefix == null)
122122
throw new ArgumentNullException("apiKeyPrefix");
123123

124124
BasePath = basePath;
125125

126-
foreach (var keyValuePair in defaultHeader)
126+
foreach (var keyValuePair in defaultHeaders)
127127
{
128-
DefaultHeader.Add(keyValuePair);
128+
DefaultHeaders.Add(keyValuePair);
129129
}
130130

131131
foreach (var keyValuePair in apiKey)
@@ -156,7 +156,23 @@ namespace {{packageName}}.Client
156156
/// <summary>
157157
/// Gets or sets the default header.
158158
/// </summary>
159-
public virtual IDictionary<string, string> DefaultHeader { get; set; }
159+
[Obsolete("Use DefaultHeaders instead.")]
160+
public virtual IDictionary<string, string> DefaultHeader
161+
{
162+
get
163+
{
164+
return DefaultHeaders;
165+
}
166+
set
167+
{
168+
DefaultHeaders = value;
169+
}
170+
}
171+
172+
/// <summary>
173+
/// Gets or sets the default headers.
174+
/// </summary>
175+
public virtual IDictionary<string, string> DefaultHeaders { get; set; }
160176

161177
/// <summary>
162178
/// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
@@ -374,17 +390,17 @@ namespace {{packageName}}.Client
374390
375391
Dictionary<string, string> apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
376392
Dictionary<string, string> apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
377-
Dictionary<string, string> defaultHeader = first.DefaultHeader.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
393+
Dictionary<string, string> defaultHeaders = first.DefaultHeaders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
378394
379395
foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value;
380396
foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value;
381-
foreach (var kvp in second.DefaultHeader) defaultHeader[kvp.Key] = kvp.Value;
397+
foreach (var kvp in second.DefaultHeaders) defaultHeaders[kvp.Key] = kvp.Value;
382398
383399
var config = new Configuration
384400
{
385401
ApiKey = apiKey,
386402
ApiKeyPrefix = apiKeyPrefix,
387-
DefaultHeader = defaultHeader,
403+
DefaultHeader = defaultHeaders,
388404
BasePath = second.BasePath ?? first.BasePath,
389405
Timeout = second.Timeout,
390406
UserAgent = second.UserAgent ?? first.UserAgent,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
{{>partial_header}}
22

3+
using System;
34
using System.Collections.Generic;
45

56
namespace {{packageName}}.Client
@@ -43,8 +44,15 @@ namespace {{packageName}}.Client
4344
/// Gets the default header.
4445
/// </summary>
4546
/// <value>Default header.</value>
47+
[Obsolete("Use DefaultHeaders instead.")]
4648
IDictionary<string, string> DefaultHeader { get; }
4749

50+
/// <summary>
51+
/// Gets the default headers.
52+
/// </summary>
53+
/// <value>Default headers.</value>
54+
IDictionary<string, string> DefaultHeaders { get; }
55+
4856
/// <summary>
4957
/// Gets the temp folder path.
5058
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -273,6 +273,14 @@ private RestRequest newRequest(
273273
}
274274
}
275275

276+
if (configuration.DefaultHeaders != null)
277+
{
278+
foreach (var headerParam in configuration.DefaultHeaders)
279+
{
280+
request.AddHeader(headerParam.Key, headerParam.Value);
281+
}
282+
}
283+
276284
if (options.HeaderParameters != null)
277285
{
278286
foreach (var headerParam in options.HeaderParameters)

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public class Configuration : IReadableConfiguration
4242
#endregion Constants
4343

4444
#region Static Members
45-
45+
4646
/// <summary>
4747
/// Default creation of exceptions for a given method name and response object
4848
/// </summary>
@@ -68,7 +68,7 @@ public class Configuration : IReadableConfiguration
6868
/// Example: http://localhost:3000/v1/
6969
/// </summary>
7070
private String _basePath;
71-
71+
7272
/// <summary>
7373
/// Gets or sets the API key based on the authentication name.
7474
/// This is the key and value comprising the "secret" for acessing an API.
@@ -97,7 +97,7 @@ public Configuration()
9797
{
9898
UserAgent = "OpenAPI-Generator/1.0.0/csharp";
9999
BasePath = "http://petstore.swagger.io:80/v2";
100-
DefaultHeader = new ConcurrentDictionary<string, string>();
100+
DefaultHeaders = new ConcurrentDictionary<string, string>();
101101
ApiKey = new ConcurrentDictionary<string, string>();
102102
ApiKeyPrefix = new ConcurrentDictionary<string, string>();
103103

@@ -110,25 +110,25 @@ public Configuration()
110110
/// </summary>
111111
[System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")]
112112
public Configuration(
113-
IDictionary<string, string> defaultHeader,
113+
IDictionary<string, string> defaultHeaders,
114114
IDictionary<string, string> apiKey,
115115
IDictionary<string, string> apiKeyPrefix,
116116
string basePath = "http://petstore.swagger.io:80/v2") : this()
117117
{
118118
if (string.IsNullOrWhiteSpace(basePath))
119119
throw new ArgumentException("The provided basePath is invalid.", "basePath");
120-
if (defaultHeader == null)
121-
throw new ArgumentNullException("defaultHeader");
120+
if (defaultHeaders == null)
121+
throw new ArgumentNullException("defaultHeaders");
122122
if (apiKey == null)
123123
throw new ArgumentNullException("apiKey");
124124
if (apiKeyPrefix == null)
125125
throw new ArgumentNullException("apiKeyPrefix");
126126

127127
BasePath = basePath;
128128

129-
foreach (var keyValuePair in defaultHeader)
129+
foreach (var keyValuePair in defaultHeaders)
130130
{
131-
DefaultHeader.Add(keyValuePair);
131+
DefaultHeaders.Add(keyValuePair);
132132
}
133133

134134
foreach (var keyValuePair in apiKey)
@@ -159,7 +159,23 @@ public virtual string BasePath {
159159
/// <summary>
160160
/// Gets or sets the default header.
161161
/// </summary>
162-
public virtual IDictionary<string, string> DefaultHeader { get; set; }
162+
[Obsolete("Use DefaultHeaders instead.")]
163+
public virtual IDictionary<string, string> DefaultHeader
164+
{
165+
get
166+
{
167+
return DefaultHeaders;
168+
}
169+
set
170+
{
171+
DefaultHeaders = value;
172+
}
173+
}
174+
175+
/// <summary>
176+
/// Gets or sets the default headers.
177+
/// </summary>
178+
public virtual IDictionary<string, string> DefaultHeaders { get; set; }
163179

164180
/// <summary>
165181
/// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds.
@@ -369,17 +385,17 @@ public static IReadableConfiguration MergeConfigurations(IReadableConfiguration
369385

370386
Dictionary<string, string> apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
371387
Dictionary<string, string> apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
372-
Dictionary<string, string> defaultHeader = first.DefaultHeader.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
388+
Dictionary<string, string> defaultHeaders = first.DefaultHeaders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value);
373389

374390
foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value;
375391
foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value;
376-
foreach (var kvp in second.DefaultHeader) defaultHeader[kvp.Key] = kvp.Value;
392+
foreach (var kvp in second.DefaultHeaders) defaultHeaders[kvp.Key] = kvp.Value;
377393

378394
var config = new Configuration
379395
{
380396
ApiKey = apiKey,
381397
ApiKeyPrefix = apiKeyPrefix,
382-
DefaultHeader = defaultHeader,
398+
DefaultHeader = defaultHeaders,
383399
BasePath = second.BasePath ?? first.BasePath,
384400
Timeout = second.Timeout,
385401
UserAgent = second.UserAgent ?? first.UserAgent,

samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
*/
1010

1111

12+
using System;
1213
using System.Collections.Generic;
1314

1415
namespace Org.OpenAPITools.Client
@@ -52,8 +53,15 @@ public interface IReadableConfiguration
5253
/// Gets the default header.
5354
/// </summary>
5455
/// <value>Default header.</value>
56+
[Obsolete("Use DefaultHeaders instead.")]
5557
IDictionary<string, string> DefaultHeader { get; }
5658

59+
/// <summary>
60+
/// Gets the default headers.
61+
/// </summary>
62+
/// <value>Default headers.</value>
63+
IDictionary<string, string> DefaultHeaders { get; }
64+
5765
/// <summary>
5866
/// Gets the temp folder path.
5967
/// </summary>

samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,14 @@ private RestRequest newRequest(
274274
}
275275
}
276276

277+
if (configuration.DefaultHeaders != null)
278+
{
279+
foreach (var headerParam in configuration.DefaultHeaders)
280+
{
281+
request.AddHeader(headerParam.Key, headerParam.Value);
282+
}
283+
}
284+
277285
if (options.HeaderParameters != null)
278286
{
279287
foreach (var headerParam in options.HeaderParameters)

0 commit comments

Comments
 (0)