From fbb2f1e05a6685499447758749f6b8129a6dfe1c Mon Sep 17 00:00:00 2001 From: Mario De Schaepmeester Date: Thu, 8 Aug 2019 18:24:47 +0200 Subject: [PATCH] [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 --- .../csharp-netcore/ApiClient.mustache | 8 ++++ .../csharp-netcore/Configuration.mustache | 40 +++++++++++++------ .../IReadableConfiguration.mustache | 8 ++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 8 ++++ .../Org.OpenAPITools/Client/Configuration.cs | 40 +++++++++++++------ .../Client/IReadableConfiguration.cs | 8 ++++ .../src/Org.OpenAPITools/Client/ApiClient.cs | 8 ++++ .../Org.OpenAPITools/Client/Configuration.cs | 40 +++++++++++++------ .../Client/IReadableConfiguration.cs | 8 ++++ 9 files changed, 132 insertions(+), 36 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache index f3232ec5f16..55b3e96c4c7 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/ApiClient.mustache @@ -270,6 +270,14 @@ namespace {{packageName}}.Client } } + if (configuration.DefaultHeaders != null) + { + foreach (var headerParam in configuration.DefaultHeaders) + { + request.AddHeader(headerParam.Key, headerParam.Value); + } + } + if (options.HeaderParameters != null) { foreach (var headerParam in options.HeaderParameters) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache index 7f70ee4c5e6..f9a29c90aa3 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache @@ -35,7 +35,7 @@ namespace {{packageName}}.Client #endregion Constants #region Static Members - + /// /// Default creation of exceptions for a given method name and response object /// @@ -65,7 +65,7 @@ namespace {{packageName}}.Client /// Example: http://localhost:3000/v1/ /// private String _basePath; - + /// /// Gets or sets the API key based on the authentication name. /// This is the key and value comprising the "secret" for acessing an API. @@ -94,7 +94,7 @@ namespace {{packageName}}.Client { UserAgent = "{{#httpUserAgent}}{{.}}{{/httpUserAgent}}{{^httpUserAgent}}OpenAPI-Generator/{{packageVersion}}/csharp{{/httpUserAgent}}"; BasePath = "{{{basePath}}}"; - DefaultHeader = new {{^net35}}Concurrent{{/net35}}Dictionary(); + DefaultHeaders = new {{^net35}}Concurrent{{/net35}}Dictionary(); ApiKey = new {{^net35}}Concurrent{{/net35}}Dictionary(); ApiKeyPrefix = new {{^net35}}Concurrent{{/net35}}Dictionary(); @@ -107,15 +107,15 @@ namespace {{packageName}}.Client /// [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] public Configuration( - IDictionary defaultHeader, + IDictionary defaultHeaders, IDictionary apiKey, IDictionary apiKeyPrefix, string basePath = "{{{basePath}}}") : this() { if (string.{{^net35}}IsNullOrWhiteSpace{{/net35}}{{#net35}}IsNullOrEmpty{{/net35}}(basePath)) throw new ArgumentException("The provided basePath is invalid.", "basePath"); - if (defaultHeader == null) - throw new ArgumentNullException("defaultHeader"); + if (defaultHeaders == null) + throw new ArgumentNullException("defaultHeaders"); if (apiKey == null) throw new ArgumentNullException("apiKey"); if (apiKeyPrefix == null) @@ -123,9 +123,9 @@ namespace {{packageName}}.Client BasePath = basePath; - foreach (var keyValuePair in defaultHeader) + foreach (var keyValuePair in defaultHeaders) { - DefaultHeader.Add(keyValuePair); + DefaultHeaders.Add(keyValuePair); } foreach (var keyValuePair in apiKey) @@ -156,7 +156,23 @@ namespace {{packageName}}.Client /// /// Gets or sets the default header. /// - public virtual IDictionary DefaultHeader { get; set; } + [Obsolete("Use DefaultHeaders instead.")] + public virtual IDictionary DefaultHeader + { + get + { + return DefaultHeaders; + } + set + { + DefaultHeaders = value; + } + } + + /// + /// Gets or sets the default headers. + /// + public virtual IDictionary DefaultHeaders { get; set; } /// /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds. @@ -374,17 +390,17 @@ namespace {{packageName}}.Client Dictionary apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); - Dictionary defaultHeader = first.DefaultHeader.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + Dictionary defaultHeaders = first.DefaultHeaders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value; - foreach (var kvp in second.DefaultHeader) defaultHeader[kvp.Key] = kvp.Value; + foreach (var kvp in second.DefaultHeaders) defaultHeaders[kvp.Key] = kvp.Value; var config = new Configuration { ApiKey = apiKey, ApiKeyPrefix = apiKeyPrefix, - DefaultHeader = defaultHeader, + DefaultHeader = defaultHeaders, BasePath = second.BasePath ?? first.BasePath, Timeout = second.Timeout, UserAgent = second.UserAgent ?? first.UserAgent, diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/IReadableConfiguration.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/IReadableConfiguration.mustache index ce165e0c81d..0acef255d6c 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/IReadableConfiguration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/IReadableConfiguration.mustache @@ -1,5 +1,6 @@ {{>partial_header}} +using System; using System.Collections.Generic; namespace {{packageName}}.Client @@ -43,8 +44,15 @@ namespace {{packageName}}.Client /// Gets the default header. /// /// Default header. + [Obsolete("Use DefaultHeaders instead.")] IDictionary DefaultHeader { get; } + /// + /// Gets the default headers. + /// + /// Default headers. + IDictionary DefaultHeaders { get; } + /// /// Gets the temp folder path. /// diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs index cb5dc1a6bca..af5165ee251 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ApiClient.cs @@ -273,6 +273,14 @@ namespace Org.OpenAPITools.Client } } + if (configuration.DefaultHeaders != null) + { + foreach (var headerParam in configuration.DefaultHeaders) + { + request.AddHeader(headerParam.Key, headerParam.Value); + } + } + if (options.HeaderParameters != null) { foreach (var headerParam in options.HeaderParameters) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs index 520e1420265..9fadf1712f3 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Configuration.cs @@ -42,7 +42,7 @@ namespace Org.OpenAPITools.Client #endregion Constants #region Static Members - + /// /// Default creation of exceptions for a given method name and response object /// @@ -68,7 +68,7 @@ namespace Org.OpenAPITools.Client /// Example: http://localhost:3000/v1/ /// private String _basePath; - + /// /// Gets or sets the API key based on the authentication name. /// This is the key and value comprising the "secret" for acessing an API. @@ -97,7 +97,7 @@ namespace Org.OpenAPITools.Client { UserAgent = "OpenAPI-Generator/1.0.0/csharp"; BasePath = "http://petstore.swagger.io:80/v2"; - DefaultHeader = new ConcurrentDictionary(); + DefaultHeaders = new ConcurrentDictionary(); ApiKey = new ConcurrentDictionary(); ApiKeyPrefix = new ConcurrentDictionary(); @@ -110,15 +110,15 @@ namespace Org.OpenAPITools.Client /// [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] public Configuration( - IDictionary defaultHeader, + IDictionary defaultHeaders, IDictionary apiKey, IDictionary apiKeyPrefix, string basePath = "http://petstore.swagger.io:80/v2") : this() { if (string.IsNullOrWhiteSpace(basePath)) throw new ArgumentException("The provided basePath is invalid.", "basePath"); - if (defaultHeader == null) - throw new ArgumentNullException("defaultHeader"); + if (defaultHeaders == null) + throw new ArgumentNullException("defaultHeaders"); if (apiKey == null) throw new ArgumentNullException("apiKey"); if (apiKeyPrefix == null) @@ -126,9 +126,9 @@ namespace Org.OpenAPITools.Client BasePath = basePath; - foreach (var keyValuePair in defaultHeader) + foreach (var keyValuePair in defaultHeaders) { - DefaultHeader.Add(keyValuePair); + DefaultHeaders.Add(keyValuePair); } foreach (var keyValuePair in apiKey) @@ -159,7 +159,23 @@ namespace Org.OpenAPITools.Client /// /// Gets or sets the default header. /// - public virtual IDictionary DefaultHeader { get; set; } + [Obsolete("Use DefaultHeaders instead.")] + public virtual IDictionary DefaultHeader + { + get + { + return DefaultHeaders; + } + set + { + DefaultHeaders = value; + } + } + + /// + /// Gets or sets the default headers. + /// + public virtual IDictionary DefaultHeaders { get; set; } /// /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds. @@ -369,17 +385,17 @@ namespace Org.OpenAPITools.Client Dictionary apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); - Dictionary defaultHeader = first.DefaultHeader.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + Dictionary defaultHeaders = first.DefaultHeaders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value; - foreach (var kvp in second.DefaultHeader) defaultHeader[kvp.Key] = kvp.Value; + foreach (var kvp in second.DefaultHeaders) defaultHeaders[kvp.Key] = kvp.Value; var config = new Configuration { ApiKey = apiKey, ApiKeyPrefix = apiKeyPrefix, - DefaultHeader = defaultHeader, + DefaultHeader = defaultHeaders, BasePath = second.BasePath ?? first.BasePath, Timeout = second.Timeout, UserAgent = second.UserAgent ?? first.UserAgent, diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs index 23e1a0c2e19..8faa7f57838 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/IReadableConfiguration.cs @@ -9,6 +9,7 @@ */ +using System; using System.Collections.Generic; namespace Org.OpenAPITools.Client @@ -52,8 +53,15 @@ namespace Org.OpenAPITools.Client /// Gets the default header. /// /// Default header. + [Obsolete("Use DefaultHeaders instead.")] IDictionary DefaultHeader { get; } + /// + /// Gets the default headers. + /// + /// Default headers. + IDictionary DefaultHeaders { get; } + /// /// Gets the temp folder path. /// diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs index e3db56be21b..05c50bceac0 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ApiClient.cs @@ -274,6 +274,14 @@ namespace Org.OpenAPITools.Client } } + if (configuration.DefaultHeaders != null) + { + foreach (var headerParam in configuration.DefaultHeaders) + { + request.AddHeader(headerParam.Key, headerParam.Value); + } + } + if (options.HeaderParameters != null) { foreach (var headerParam in options.HeaderParameters) diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs index 25010fbef9e..4317d59a939 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Configuration.cs @@ -42,7 +42,7 @@ namespace Org.OpenAPITools.Client #endregion Constants #region Static Members - + /// /// Default creation of exceptions for a given method name and response object /// @@ -72,7 +72,7 @@ namespace Org.OpenAPITools.Client /// Example: http://localhost:3000/v1/ /// private String _basePath; - + /// /// Gets or sets the API key based on the authentication name. /// This is the key and value comprising the "secret" for acessing an API. @@ -101,7 +101,7 @@ namespace Org.OpenAPITools.Client { UserAgent = "OpenAPI-Generator/1.0.0/csharp"; BasePath = "http://petstore.swagger.io:80/v2"; - DefaultHeader = new ConcurrentDictionary(); + DefaultHeaders = new ConcurrentDictionary(); ApiKey = new ConcurrentDictionary(); ApiKeyPrefix = new ConcurrentDictionary(); @@ -114,15 +114,15 @@ namespace Org.OpenAPITools.Client /// [System.Diagnostics.CodeAnalysis.SuppressMessage("ReSharper", "VirtualMemberCallInConstructor")] public Configuration( - IDictionary defaultHeader, + IDictionary defaultHeaders, IDictionary apiKey, IDictionary apiKeyPrefix, string basePath = "http://petstore.swagger.io:80/v2") : this() { if (string.IsNullOrWhiteSpace(basePath)) throw new ArgumentException("The provided basePath is invalid.", "basePath"); - if (defaultHeader == null) - throw new ArgumentNullException("defaultHeader"); + if (defaultHeaders == null) + throw new ArgumentNullException("defaultHeaders"); if (apiKey == null) throw new ArgumentNullException("apiKey"); if (apiKeyPrefix == null) @@ -130,9 +130,9 @@ namespace Org.OpenAPITools.Client BasePath = basePath; - foreach (var keyValuePair in defaultHeader) + foreach (var keyValuePair in defaultHeaders) { - DefaultHeader.Add(keyValuePair); + DefaultHeaders.Add(keyValuePair); } foreach (var keyValuePair in apiKey) @@ -163,7 +163,23 @@ namespace Org.OpenAPITools.Client /// /// Gets or sets the default header. /// - public virtual IDictionary DefaultHeader { get; set; } + [Obsolete("Use DefaultHeaders instead.")] + public virtual IDictionary DefaultHeader + { + get + { + return DefaultHeaders; + } + set + { + DefaultHeaders = value; + } + } + + /// + /// Gets or sets the default headers. + /// + public virtual IDictionary DefaultHeaders { get; set; } /// /// Gets or sets the HTTP timeout (milliseconds) of ApiClient. Default to 100000 milliseconds. @@ -374,17 +390,17 @@ namespace Org.OpenAPITools.Client Dictionary apiKey = first.ApiKey.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); Dictionary apiKeyPrefix = first.ApiKeyPrefix.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); - Dictionary defaultHeader = first.DefaultHeader.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); + Dictionary defaultHeaders = first.DefaultHeaders.ToDictionary(kvp => kvp.Key, kvp => kvp.Value); foreach (var kvp in second.ApiKey) apiKey[kvp.Key] = kvp.Value; foreach (var kvp in second.ApiKeyPrefix) apiKeyPrefix[kvp.Key] = kvp.Value; - foreach (var kvp in second.DefaultHeader) defaultHeader[kvp.Key] = kvp.Value; + foreach (var kvp in second.DefaultHeaders) defaultHeaders[kvp.Key] = kvp.Value; var config = new Configuration { ApiKey = apiKey, ApiKeyPrefix = apiKeyPrefix, - DefaultHeader = defaultHeader, + DefaultHeader = defaultHeaders, BasePath = second.BasePath ?? first.BasePath, Timeout = second.Timeout, UserAgent = second.UserAgent ?? first.UserAgent, diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/IReadableConfiguration.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/IReadableConfiguration.cs index 23e1a0c2e19..8faa7f57838 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/IReadableConfiguration.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/IReadableConfiguration.cs @@ -9,6 +9,7 @@ */ +using System; using System.Collections.Generic; namespace Org.OpenAPITools.Client @@ -52,8 +53,15 @@ namespace Org.OpenAPITools.Client /// Gets the default header. /// /// Default header. + [Obsolete("Use DefaultHeaders instead.")] IDictionary DefaultHeader { get; } + /// + /// Gets the default headers. + /// + /// Default headers. + IDictionary DefaultHeaders { get; } + /// /// Gets the temp folder path. ///