From f356de606b367aee34aac57da34108827aa6b139 Mon Sep 17 00:00:00 2001 From: Tatsuro Shibamura Date: Mon, 3 Feb 2020 01:15:33 +0900 Subject: [PATCH] [csharp-netcore] Improved `Multimap` and `ClientUtils` implementation (#5122) * Improvement Multimap impl * Fixed missing semi-colon * Fixed compile error using .NET Standard 2.0 * Fixed compile error using .NET Standard 2.0 * Update sample projects * Apply modifications to additional code flows --- .../csharp-netcore/ClientUtils.mustache | 69 ++---- .../csharp-netcore/Multimap.mustache | 129 ++++------ .../resources/csharp-netcore/api.mustache | 48 +--- .../src/Org.OpenAPITools/Api/FakeApi.cs | 224 +++--------------- .../Api/FakeClassnameTags123Api.cs | 16 +- .../src/Org.OpenAPITools/Api/PetApi.cs | 32 +-- .../src/Org.OpenAPITools/Api/UserApi.cs | 32 +-- .../Org.OpenAPITools/Client/ClientUtils.cs | 69 ++---- .../src/Org.OpenAPITools/Client/Multimap.cs | 120 +++++----- .../src/Org.OpenAPITools/Api/FakeApi.cs | 224 +++--------------- .../Api/FakeClassnameTags123Api.cs | 16 +- .../src/Org.OpenAPITools/Api/PetApi.cs | 32 +-- .../src/Org.OpenAPITools/Api/UserApi.cs | 32 +-- .../Org.OpenAPITools/Client/ClientUtils.cs | 69 ++---- .../src/Org.OpenAPITools/Client/Multimap.cs | 120 +++++----- 15 files changed, 289 insertions(+), 943 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/ClientUtils.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/ClientUtils.mustache index 4528441a5a..8601b2e9c3 100755 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/ClientUtils.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/ClientUtils.mustache @@ -2,13 +2,10 @@ using System; using System.Collections; -using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; {{#useCompareNetObjects}} using KellermanSoftware.CompareNetObjects; {{/useCompareNetObjects}} @@ -58,15 +55,11 @@ namespace {{packageName}}.Client { var parameters = new Multimap(); - if (IsCollection(value) && collectionFormat == "multi") + if (value is ICollection collection && collectionFormat == "multi") { - var valueCollection = value as IEnumerable; - if (valueCollection != null) + foreach (var item in collection) { - foreach (var item in valueCollection) - { - parameters.Add(name, ParameterToString(item)); - } + parameters.Add(name, ParameterToString(item)); } } else @@ -87,49 +80,26 @@ namespace {{packageName}}.Client /// Formatted string. public static string ParameterToString(object obj, IReadableConfiguration configuration = null) { - if (obj is DateTime) + if (obj is DateTime dateTime) // Return a formatted date string - Can be customized with Configuration.DateTimeFormat // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // For example: 2009-06-15T13:45:30.0000000 - return ((DateTime)obj).ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); - else if (obj is DateTimeOffset) + return dateTime.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); + if (obj is DateTimeOffset dateTimeOffset) // Return a formatted date string - Can be customized with Configuration.DateTimeFormat // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // For example: 2009-06-15T13:45:30.0000000 - return ((DateTimeOffset)obj).ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); - else if (obj is bool) - return (bool)obj ? "true" : "false"; - else - { - if (obj is IList) - { - var list = obj as IList; - var flattenedString = new StringBuilder(); - foreach (var param in list) - { - if (flattenedString.Length > 0) - flattenedString.Append(","); - flattenedString.Append(param); - } - return flattenedString.ToString(); - } - - return Convert.ToString (obj); - } + return dateTimeOffset.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); + if (obj is bool boolean) + return boolean ? "true" : "false"; + if (obj is ICollection collection) + return string.Join(",", collection.Cast()); + + return Convert.ToString(obj); } - - /// - /// Check if generic object is a collection. - /// - /// - /// True if object is a collection type - private static bool IsCollection(object value) - { - return value is IList || value is ICollection; - } - + /// /// URL encode a string /// Credit/Ref: https://github.com/restsharp/RestSharp/blob/master/RestSharp/Extensions/StringExtensions.cs#L50 @@ -172,7 +142,7 @@ namespace {{packageName}}.Client /// Encoded string. public static string Base64Encode(string text) { - return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); + return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); } /// @@ -182,14 +152,9 @@ namespace {{packageName}}.Client /// Byte array public static byte[] ReadAsBytes(Stream inputStream) { - byte[] buf = new byte[16*1024]; - using (MemoryStream ms = new MemoryStream()) + using (var ms = new MemoryStream()) { - int count; - while ((count = inputStream.Read(buf, 0, buf.Length)) > 0) - { - ms.Write(buf, 0, count); - } + inputStream.CopyTo(ms); return ms.ToArray(); } } diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/Multimap.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/Multimap.mustache index 7e8c97a832..8624af0092 100755 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/Multimap.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/Multimap.mustache @@ -2,7 +2,6 @@ using System; using System.Collections; -{{^net35}}using System.Collections.Concurrent;{{/net35}} using System.Collections.Generic; namespace {{packageName}}.Client @@ -10,13 +9,13 @@ namespace {{packageName}}.Client /// /// A dictionary in which one key has many associated values. /// - /// The type of the key + /// The type of the key /// The type of the value associated with the key. - public class Multimap : IDictionary> + public class Multimap : IDictionary> { #region Private Fields - private readonly {{^net35}}Concurrent{{/net35}}Dictionary> _dictionary; + private readonly Dictionary> _dictionary; #endregion Private Fields @@ -27,16 +26,16 @@ namespace {{packageName}}.Client /// public Multimap() { - _dictionary = new {{^net35}}Concurrent{{/net35}}Dictionary>(); + _dictionary = new Dictionary>(); } /// /// Constructor with comparer. /// /// - public Multimap(IEqualityComparer comparer) + public Multimap(IEqualityComparer comparer) { - _dictionary = new {{^net35}}Concurrent{{/net35}}Dictionary>(comparer); + _dictionary = new Dictionary>(comparer); } #endregion Constructors @@ -47,7 +46,7 @@ namespace {{packageName}}.Client /// To get the enumerator. /// /// Enumerator - public IEnumerator>> GetEnumerator() + public IEnumerator>> GetEnumerator() { return _dictionary.GetEnumerator(); } @@ -68,12 +67,25 @@ namespace {{packageName}}.Client /// Add values to Multimap /// /// Key value pair - public void Add(KeyValuePair> item) + public void Add(KeyValuePair> item) { if (!TryAdd(item.Key, item.Value)) throw new InvalidOperationException("Could not add values to Multimap."); } + /// + /// Add Multimap to Multimap + /// + /// Multimap + public void Add(Multimap multimap) + { + foreach (var item in multimap) + { + if (!TryAdd(item.Key, item.Value)) + throw new InvalidOperationException("Could not add values to Multimap."); + } + } + /// /// Clear Multimap /// @@ -88,7 +100,7 @@ namespace {{packageName}}.Client /// Key value pair /// Method needs to be implemented /// true if the Multimap contains the item; otherwise, false. - public bool Contains(KeyValuePair> item) + public bool Contains(KeyValuePair> item) { throw new NotImplementedException(); } @@ -101,7 +113,7 @@ namespace {{packageName}}.Client /// from Multimap. The array must have zero-based indexing. /// The zero-based index in array at which copying begins. /// Method needs to be implemented - public void CopyTo(KeyValuePair>[] array, int arrayIndex) + public void CopyTo(KeyValuePair>[] array, int arrayIndex) { throw new NotImplementedException(); } @@ -112,7 +124,7 @@ namespace {{packageName}}.Client /// Key value pair /// true if the item is successfully removed; otherwise, false. /// Method needs to be implemented - public bool Remove(KeyValuePair> item) + public bool Remove(KeyValuePair> item) { throw new NotImplementedException(); } @@ -120,24 +132,12 @@ namespace {{packageName}}.Client /// /// Gets the number of items contained in the Multimap. /// - public int Count - { - get - { - return _dictionary.Count; - } - } + public int Count => _dictionary.Count; /// /// Gets a value indicating whether the Multimap is read-only. /// - public bool IsReadOnly - { - get - { - return false; - } - } + public bool IsReadOnly => false; /// /// Adds an item with the provided key and value to the Multimap. @@ -145,12 +145,11 @@ namespace {{packageName}}.Client /// The object to use as the key of the item to add. /// The object to use as the value of the item to add. /// Thrown when couldn't add the value to Multimap. - public void Add(T key, IList value) + public void Add(TKey key, IList value) { if (value != null && value.Count > 0) { - IList list; - if (_dictionary.TryGetValue(key, out list)) + if (_dictionary.TryGetValue(key, out var list)) { foreach (var k in value) list.Add(k); } @@ -169,7 +168,7 @@ namespace {{packageName}}.Client /// The key to locate in the Multimap. /// true if the Multimap contains an item with /// the key; otherwise, false. - public bool ContainsKey(T key) + public bool ContainsKey(TKey key) { return _dictionary.ContainsKey(key); } @@ -179,10 +178,9 @@ namespace {{packageName}}.Client /// /// The key to locate in the Multimap. /// true if the item is successfully removed; otherwise, false. - public bool Remove(T key) + public bool Remove(TKey key) { - IList list; - return TryRemove(key, out list); + return TryRemove(key, out var _); } /// @@ -194,7 +192,7 @@ namespace {{packageName}}.Client /// This parameter is passed uninitialized. /// true if the object that implements Multimap contains /// an item with the specified key; otherwise, false. - public bool TryGetValue(T key, out IList value) + public bool TryGetValue(TKey key, out IList value) { return _dictionary.TryGetValue(key, out value); } @@ -204,36 +202,21 @@ namespace {{packageName}}.Client /// /// The key of the item to get or set. /// The value of the specified key. - public IList this[T key] + public IList this[TKey key] { - get - { - return _dictionary[key]; - } - set { _dictionary[key] = value; } + get => _dictionary[key]; + set => _dictionary[key] = value; } /// /// Gets a System.Collections.Generic.ICollection containing the keys of the Multimap. /// - public ICollection Keys - { - get - { - return _dictionary.Keys; - } - } + public ICollection Keys => _dictionary.Keys; /// /// Gets a System.Collections.Generic.ICollection containing the values of the Multimap. /// - public ICollection> Values - { - get - { - return _dictionary.Values; - } - } + public ICollection> Values => _dictionary.Values; /// /// Copy the items of the Multimap to an System.Array, @@ -244,7 +227,7 @@ namespace {{packageName}}.Client /// The zero-based index in array at which copying begins. public void CopyTo(Array array, int index) { - ((ICollection) _dictionary).CopyTo(array, index); + ((ICollection)_dictionary).CopyTo(array, index); } /// @@ -253,19 +236,17 @@ namespace {{packageName}}.Client /// The object to use as the key of the item to add. /// The object to use as the value of the item to add. /// Thrown when couldn't add value to Multimap. - public void Add(T key, TValue value) + public void Add(TKey key, TValue value) { if (value != null) { - IList list; - if (_dictionary.TryGetValue(key, out list)) + if (_dictionary.TryGetValue(key, out var list)) { list.Add(value); } else { - list = new List(); - list.Add(value); + list = new List { value }; if (!TryAdd(key, list)) throw new InvalidOperationException("Could not add value to Multimap."); } @@ -279,45 +260,27 @@ namespace {{packageName}}.Client /** * Helper method to encapsulate generator differences between dictionary types. */ - private bool TryRemove(T key, out IList value) + private bool TryRemove(TKey key, out IList value) { - {{^net35}}return _dictionary.TryRemove(key, out value);{{/net35}} - {{#net35}}try - { - _dictionary.TryGetValue(key, out value); - _dictionary.Remove(key); - } -#pragma warning disable 168 - catch (ArgumentException e) -#pragma warning restore 168 - { - value = null; - return false; - } - - return true;{{/net35}} + _dictionary.TryGetValue(key, out value); + return _dictionary.Remove(key); } /** * Helper method to encapsulate generator differences between dictionary types. */ - private bool TryAdd(T key, IList value) + private bool TryAdd(TKey key, IList value) { - {{^net35}}return _dictionary.TryAdd(key, value);{{/net35}} - {{#net35}} try { _dictionary.Add(key, value); } -#pragma warning disable 168 - catch (ArgumentException e) -#pragma warning restore 168 + catch (ArgumentException) { return false; } return true; - {{/net35}} } #endregion Private Members } diff --git a/modules/openapi-generator/src/main/resources/csharp-netcore/api.mustache b/modules/openapi-generator/src/main/resources/csharp-netcore/api.mustache index e8a32d382a..f149f196b3 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/api.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/api.mustache @@ -268,23 +268,11 @@ namespace {{packageName}}.{{apiPackage}} {{^vendorExtensions.x-csharp-value-type}} if ({{paramName}} != null) { - foreach (var _kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})); } {{/vendorExtensions.x-csharp-value-type}} {{#vendorExtensions.x-csharp-value-type}} - foreach (var _kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})); {{/vendorExtensions.x-csharp-value-type}} {{/queryParams}} {{#headerParams}} @@ -333,13 +321,7 @@ namespace {{packageName}}.{{apiPackage}} {{#isKeyInQuery}} if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))) { - foreach (var _kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))); } {{/isKeyInQuery}} {{/isApiKey}} @@ -438,23 +420,11 @@ namespace {{packageName}}.{{apiPackage}} {{^vendorExtensions.x-csharp-value-type}} if ({{paramName}} != null) { - foreach (var _kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})); } {{/vendorExtensions.x-csharp-value-type}} {{#vendorExtensions.x-csharp-value-type}} - foreach (var _kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("{{#collectionFormat}}{{collectionFormat}}{{/collectionFormat}}", "{{baseName}}", {{paramName}})); {{/vendorExtensions.x-csharp-value-type}} {{/queryParams}} {{#headerParams}} @@ -503,13 +473,7 @@ namespace {{packageName}}.{{apiPackage}} {{#isKeyInQuery}} if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))) { - foreach (var _kvp in {{packageName}}.Client.ClientUtils.ParameterToMultiMap("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add({{packageName}}.Client.ClientUtils.ParameterToMultiMap("", "{{keyParamName}}", this.Configuration.GetApiKeyWithPrefix("{{keyParamName}}"))); } {{/isKeyInQuery}} {{/isApiKey}} diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/FakeApi.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/FakeApi.cs index 6c0797b7ff..a4ab49f442 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/FakeApi.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/FakeApi.cs @@ -1567,13 +1567,7 @@ namespace Org.OpenAPITools.Api if (query != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query", query)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query", query)); } localVarRequestOptions.Data = body; @@ -1639,13 +1633,7 @@ namespace Org.OpenAPITools.Api if (query != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query", query)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query", query)); } localVarRequestOptions.Data = body; @@ -2110,43 +2098,19 @@ namespace Org.OpenAPITools.Api if (enumQueryStringArray != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "enum_query_string_array", enumQueryStringArray)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "enum_query_string_array", enumQueryStringArray)); } if (enumQueryString != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_string", enumQueryString)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_string", enumQueryString)); } if (enumQueryInteger != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_integer", enumQueryInteger)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_integer", enumQueryInteger)); } if (enumQueryDouble != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_double", enumQueryDouble)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_double", enumQueryDouble)); } if (enumHeaderStringArray != null) localVarRequestOptions.HeaderParameters.Add("enum_header_string_array", Org.OpenAPITools.Client.ClientUtils.ParameterToString(enumHeaderStringArray)); // header parameter @@ -2227,43 +2191,19 @@ namespace Org.OpenAPITools.Api if (enumQueryStringArray != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "enum_query_string_array", enumQueryStringArray)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "enum_query_string_array", enumQueryStringArray)); } if (enumQueryString != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_string", enumQueryString)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_string", enumQueryString)); } if (enumQueryInteger != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_integer", enumQueryInteger)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_integer", enumQueryInteger)); } if (enumQueryDouble != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_double", enumQueryDouble)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_double", enumQueryDouble)); } if (enumHeaderStringArray != null) localVarRequestOptions.HeaderParameters.Add("enum_header_string_array", Org.OpenAPITools.Client.ClientUtils.ParameterToString(enumHeaderStringArray)); // header parameter @@ -2336,39 +2276,15 @@ namespace Org.OpenAPITools.Api var localVarAccept = Org.OpenAPITools.Client.ClientUtils.SelectHeaderAccept(_accepts); if (localVarAccept != null) localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept); - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_string_group", requiredStringGroup)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_int64_group", requiredInt64Group)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_string_group", requiredStringGroup)); + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_int64_group", requiredInt64Group)); if (stringGroup != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "string_group", stringGroup)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "string_group", stringGroup)); } if (int64Group != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "int64_group", int64Group)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "int64_group", int64Group)); } localVarRequestOptions.HeaderParameters.Add("required_boolean_group", Org.OpenAPITools.Client.ClientUtils.ParameterToString(requiredBooleanGroup)); // header parameter if (booleanGroup != null) @@ -2433,39 +2349,15 @@ namespace Org.OpenAPITools.Api foreach (var _accept in _accepts) localVarRequestOptions.HeaderParameters.Add("Accept", _accept); - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_string_group", requiredStringGroup)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_int64_group", requiredInt64Group)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_string_group", requiredStringGroup)); + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_int64_group", requiredInt64Group)); if (stringGroup != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "string_group", stringGroup)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "string_group", stringGroup)); } if (int64Group != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "int64_group", int64Group)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "int64_group", int64Group)); } localVarRequestOptions.HeaderParameters.Add("required_boolean_group", Org.OpenAPITools.Client.ClientUtils.ParameterToString(requiredBooleanGroup)); // header parameter if (booleanGroup != null) @@ -2797,53 +2689,23 @@ namespace Org.OpenAPITools.Api if (pipe != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "pipe", pipe)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "pipe", pipe)); } if (ioutil != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "ioutil", ioutil)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "ioutil", ioutil)); } if (http != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("space", "http", http)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("space", "http", http)); } if (url != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "url", url)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "url", url)); } if (context != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("multi", "context", context)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("multi", "context", context)); } @@ -2925,53 +2787,23 @@ namespace Org.OpenAPITools.Api if (pipe != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "pipe", pipe)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "pipe", pipe)); } if (ioutil != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "ioutil", ioutil)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "ioutil", ioutil)); } if (http != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("space", "http", http)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("space", "http", http)); } if (url != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "url", url)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "url", url)); } if (context != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("multi", "context", context)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("multi", "context", context)); } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs index ebf022334c..492bae795c 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs @@ -244,13 +244,7 @@ namespace Org.OpenAPITools.Api // authentication (api_key_query) required if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("api_key_query"))) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "api_key_query", this.Configuration.GetApiKeyWithPrefix("api_key_query"))) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "api_key_query", this.Configuration.GetApiKeyWithPrefix("api_key_query"))); } // make the HTTP request @@ -313,13 +307,7 @@ namespace Org.OpenAPITools.Api // authentication (api_key_query) required if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("api_key_query"))) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "api_key_query", this.Configuration.GetApiKeyWithPrefix("api_key_query"))) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "api_key_query", this.Configuration.GetApiKeyWithPrefix("api_key_query"))); } // make the HTTP request diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/PetApi.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/PetApi.cs index 5c4a624d26..0fe84290ce 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/PetApi.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/PetApi.cs @@ -851,13 +851,7 @@ namespace Org.OpenAPITools.Api if (status != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "status", status)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "status", status)); } // authentication (petstore_auth) required @@ -924,13 +918,7 @@ namespace Org.OpenAPITools.Api if (status != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "status", status)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "status", status)); } // authentication (petstore_auth) required @@ -996,13 +984,7 @@ namespace Org.OpenAPITools.Api if (tags != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "tags", tags)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "tags", tags)); } // authentication (petstore_auth) required @@ -1069,13 +1051,7 @@ namespace Org.OpenAPITools.Api if (tags != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "tags", tags)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "tags", tags)); } // authentication (petstore_auth) required diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/UserApi.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/UserApi.cs index 3f1585ce46..57e4e369cc 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/UserApi.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Api/UserApi.cs @@ -1100,23 +1100,11 @@ namespace Org.OpenAPITools.Api if (username != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "username", username)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "username", username)); } if (password != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "password", password)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "password", password)); } @@ -1183,23 +1171,11 @@ namespace Org.OpenAPITools.Api if (username != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "username", username)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "username", username)); } if (password != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "password", password)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "password", password)); } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ClientUtils.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ClientUtils.cs index dde50ca71d..764ea009ef 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ClientUtils.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/ClientUtils.cs @@ -11,13 +11,10 @@ using System; using System.Collections; -using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; using KellermanSoftware.CompareNetObjects; namespace Org.OpenAPITools.Client @@ -63,15 +60,11 @@ namespace Org.OpenAPITools.Client { var parameters = new Multimap(); - if (IsCollection(value) && collectionFormat == "multi") + if (value is ICollection collection && collectionFormat == "multi") { - var valueCollection = value as IEnumerable; - if (valueCollection != null) + foreach (var item in collection) { - foreach (var item in valueCollection) - { - parameters.Add(name, ParameterToString(item)); - } + parameters.Add(name, ParameterToString(item)); } } else @@ -92,49 +85,26 @@ namespace Org.OpenAPITools.Client /// Formatted string. public static string ParameterToString(object obj, IReadableConfiguration configuration = null) { - if (obj is DateTime) + if (obj is DateTime dateTime) // Return a formatted date string - Can be customized with Configuration.DateTimeFormat // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // For example: 2009-06-15T13:45:30.0000000 - return ((DateTime)obj).ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); - else if (obj is DateTimeOffset) + return dateTime.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); + if (obj is DateTimeOffset dateTimeOffset) // Return a formatted date string - Can be customized with Configuration.DateTimeFormat // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // For example: 2009-06-15T13:45:30.0000000 - return ((DateTimeOffset)obj).ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); - else if (obj is bool) - return (bool)obj ? "true" : "false"; - else - { - if (obj is IList) - { - var list = obj as IList; - var flattenedString = new StringBuilder(); - foreach (var param in list) - { - if (flattenedString.Length > 0) - flattenedString.Append(","); - flattenedString.Append(param); - } - return flattenedString.ToString(); - } - - return Convert.ToString (obj); - } + return dateTimeOffset.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); + if (obj is bool boolean) + return boolean ? "true" : "false"; + if (obj is ICollection collection) + return string.Join(",", collection.Cast()); + + return Convert.ToString(obj); } - - /// - /// Check if generic object is a collection. - /// - /// - /// True if object is a collection type - private static bool IsCollection(object value) - { - return value is IList || value is ICollection; - } - + /// /// URL encode a string /// Credit/Ref: https://github.com/restsharp/RestSharp/blob/master/RestSharp/Extensions/StringExtensions.cs#L50 @@ -177,7 +147,7 @@ namespace Org.OpenAPITools.Client /// Encoded string. public static string Base64Encode(string text) { - return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); + return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); } /// @@ -187,14 +157,9 @@ namespace Org.OpenAPITools.Client /// Byte array public static byte[] ReadAsBytes(Stream inputStream) { - byte[] buf = new byte[16*1024]; - using (MemoryStream ms = new MemoryStream()) + using (var ms = new MemoryStream()) { - int count; - while ((count = inputStream.Read(buf, 0, buf.Length)) > 0) - { - ms.Write(buf, 0, count); - } + inputStream.CopyTo(ms); return ms.ToArray(); } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Multimap.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Multimap.cs index 4bde7f7ffe..b0449fb764 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Multimap.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools/Client/Multimap.cs @@ -11,7 +11,6 @@ using System; using System.Collections; -using System.Collections.Concurrent; using System.Collections.Generic; namespace Org.OpenAPITools.Client @@ -19,13 +18,13 @@ namespace Org.OpenAPITools.Client /// /// A dictionary in which one key has many associated values. /// - /// The type of the key + /// The type of the key /// The type of the value associated with the key. - public class Multimap : IDictionary> + public class Multimap : IDictionary> { #region Private Fields - private readonly ConcurrentDictionary> _dictionary; + private readonly Dictionary> _dictionary; #endregion Private Fields @@ -36,16 +35,16 @@ namespace Org.OpenAPITools.Client /// public Multimap() { - _dictionary = new ConcurrentDictionary>(); + _dictionary = new Dictionary>(); } /// /// Constructor with comparer. /// /// - public Multimap(IEqualityComparer comparer) + public Multimap(IEqualityComparer comparer) { - _dictionary = new ConcurrentDictionary>(comparer); + _dictionary = new Dictionary>(comparer); } #endregion Constructors @@ -56,7 +55,7 @@ namespace Org.OpenAPITools.Client /// To get the enumerator. /// /// Enumerator - public IEnumerator>> GetEnumerator() + public IEnumerator>> GetEnumerator() { return _dictionary.GetEnumerator(); } @@ -77,12 +76,25 @@ namespace Org.OpenAPITools.Client /// Add values to Multimap /// /// Key value pair - public void Add(KeyValuePair> item) + public void Add(KeyValuePair> item) { if (!TryAdd(item.Key, item.Value)) throw new InvalidOperationException("Could not add values to Multimap."); } + /// + /// Add Multimap to Multimap + /// + /// Multimap + public void Add(Multimap multimap) + { + foreach (var item in multimap) + { + if (!TryAdd(item.Key, item.Value)) + throw new InvalidOperationException("Could not add values to Multimap."); + } + } + /// /// Clear Multimap /// @@ -97,7 +109,7 @@ namespace Org.OpenAPITools.Client /// Key value pair /// Method needs to be implemented /// true if the Multimap contains the item; otherwise, false. - public bool Contains(KeyValuePair> item) + public bool Contains(KeyValuePair> item) { throw new NotImplementedException(); } @@ -110,7 +122,7 @@ namespace Org.OpenAPITools.Client /// from Multimap. The array must have zero-based indexing. /// The zero-based index in array at which copying begins. /// Method needs to be implemented - public void CopyTo(KeyValuePair>[] array, int arrayIndex) + public void CopyTo(KeyValuePair>[] array, int arrayIndex) { throw new NotImplementedException(); } @@ -121,7 +133,7 @@ namespace Org.OpenAPITools.Client /// Key value pair /// true if the item is successfully removed; otherwise, false. /// Method needs to be implemented - public bool Remove(KeyValuePair> item) + public bool Remove(KeyValuePair> item) { throw new NotImplementedException(); } @@ -129,24 +141,12 @@ namespace Org.OpenAPITools.Client /// /// Gets the number of items contained in the Multimap. /// - public int Count - { - get - { - return _dictionary.Count; - } - } + public int Count => _dictionary.Count; /// /// Gets a value indicating whether the Multimap is read-only. /// - public bool IsReadOnly - { - get - { - return false; - } - } + public bool IsReadOnly => false; /// /// Adds an item with the provided key and value to the Multimap. @@ -154,12 +154,11 @@ namespace Org.OpenAPITools.Client /// The object to use as the key of the item to add. /// The object to use as the value of the item to add. /// Thrown when couldn't add the value to Multimap. - public void Add(T key, IList value) + public void Add(TKey key, IList value) { if (value != null && value.Count > 0) { - IList list; - if (_dictionary.TryGetValue(key, out list)) + if (_dictionary.TryGetValue(key, out var list)) { foreach (var k in value) list.Add(k); } @@ -178,7 +177,7 @@ namespace Org.OpenAPITools.Client /// The key to locate in the Multimap. /// true if the Multimap contains an item with /// the key; otherwise, false. - public bool ContainsKey(T key) + public bool ContainsKey(TKey key) { return _dictionary.ContainsKey(key); } @@ -188,10 +187,9 @@ namespace Org.OpenAPITools.Client /// /// The key to locate in the Multimap. /// true if the item is successfully removed; otherwise, false. - public bool Remove(T key) + public bool Remove(TKey key) { - IList list; - return TryRemove(key, out list); + return TryRemove(key, out var _); } /// @@ -203,7 +201,7 @@ namespace Org.OpenAPITools.Client /// This parameter is passed uninitialized. /// true if the object that implements Multimap contains /// an item with the specified key; otherwise, false. - public bool TryGetValue(T key, out IList value) + public bool TryGetValue(TKey key, out IList value) { return _dictionary.TryGetValue(key, out value); } @@ -213,36 +211,21 @@ namespace Org.OpenAPITools.Client /// /// The key of the item to get or set. /// The value of the specified key. - public IList this[T key] + public IList this[TKey key] { - get - { - return _dictionary[key]; - } - set { _dictionary[key] = value; } + get => _dictionary[key]; + set => _dictionary[key] = value; } /// /// Gets a System.Collections.Generic.ICollection containing the keys of the Multimap. /// - public ICollection Keys - { - get - { - return _dictionary.Keys; - } - } + public ICollection Keys => _dictionary.Keys; /// /// Gets a System.Collections.Generic.ICollection containing the values of the Multimap. /// - public ICollection> Values - { - get - { - return _dictionary.Values; - } - } + public ICollection> Values => _dictionary.Values; /// /// Copy the items of the Multimap to an System.Array, @@ -253,7 +236,7 @@ namespace Org.OpenAPITools.Client /// The zero-based index in array at which copying begins. public void CopyTo(Array array, int index) { - ((ICollection) _dictionary).CopyTo(array, index); + ((ICollection)_dictionary).CopyTo(array, index); } /// @@ -262,19 +245,17 @@ namespace Org.OpenAPITools.Client /// The object to use as the key of the item to add. /// The object to use as the value of the item to add. /// Thrown when couldn't add value to Multimap. - public void Add(T key, TValue value) + public void Add(TKey key, TValue value) { if (value != null) { - IList list; - if (_dictionary.TryGetValue(key, out list)) + if (_dictionary.TryGetValue(key, out var list)) { list.Add(value); } else { - list = new List(); - list.Add(value); + list = new List { value }; if (!TryAdd(key, list)) throw new InvalidOperationException("Could not add value to Multimap."); } @@ -288,18 +269,27 @@ namespace Org.OpenAPITools.Client /** * Helper method to encapsulate generator differences between dictionary types. */ - private bool TryRemove(T key, out IList value) + private bool TryRemove(TKey key, out IList value) { - return _dictionary.TryRemove(key, out value); - + _dictionary.TryGetValue(key, out value); + return _dictionary.Remove(key); } /** * Helper method to encapsulate generator differences between dictionary types. */ - private bool TryAdd(T key, IList value) + private bool TryAdd(TKey key, IList value) { - return _dictionary.TryAdd(key, value); + try + { + _dictionary.Add(key, value); + } + catch (ArgumentException) + { + return false; + } + + return true; } #endregion Private Members } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/FakeApi.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/FakeApi.cs index 6c0797b7ff..a4ab49f442 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/FakeApi.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/FakeApi.cs @@ -1567,13 +1567,7 @@ namespace Org.OpenAPITools.Api if (query != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query", query)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query", query)); } localVarRequestOptions.Data = body; @@ -1639,13 +1633,7 @@ namespace Org.OpenAPITools.Api if (query != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query", query)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "query", query)); } localVarRequestOptions.Data = body; @@ -2110,43 +2098,19 @@ namespace Org.OpenAPITools.Api if (enumQueryStringArray != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "enum_query_string_array", enumQueryStringArray)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "enum_query_string_array", enumQueryStringArray)); } if (enumQueryString != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_string", enumQueryString)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_string", enumQueryString)); } if (enumQueryInteger != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_integer", enumQueryInteger)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_integer", enumQueryInteger)); } if (enumQueryDouble != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_double", enumQueryDouble)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_double", enumQueryDouble)); } if (enumHeaderStringArray != null) localVarRequestOptions.HeaderParameters.Add("enum_header_string_array", Org.OpenAPITools.Client.ClientUtils.ParameterToString(enumHeaderStringArray)); // header parameter @@ -2227,43 +2191,19 @@ namespace Org.OpenAPITools.Api if (enumQueryStringArray != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "enum_query_string_array", enumQueryStringArray)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "enum_query_string_array", enumQueryStringArray)); } if (enumQueryString != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_string", enumQueryString)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_string", enumQueryString)); } if (enumQueryInteger != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_integer", enumQueryInteger)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_integer", enumQueryInteger)); } if (enumQueryDouble != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_double", enumQueryDouble)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "enum_query_double", enumQueryDouble)); } if (enumHeaderStringArray != null) localVarRequestOptions.HeaderParameters.Add("enum_header_string_array", Org.OpenAPITools.Client.ClientUtils.ParameterToString(enumHeaderStringArray)); // header parameter @@ -2336,39 +2276,15 @@ namespace Org.OpenAPITools.Api var localVarAccept = Org.OpenAPITools.Client.ClientUtils.SelectHeaderAccept(_accepts); if (localVarAccept != null) localVarRequestOptions.HeaderParameters.Add("Accept", localVarAccept); - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_string_group", requiredStringGroup)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_int64_group", requiredInt64Group)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_string_group", requiredStringGroup)); + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_int64_group", requiredInt64Group)); if (stringGroup != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "string_group", stringGroup)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "string_group", stringGroup)); } if (int64Group != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "int64_group", int64Group)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "int64_group", int64Group)); } localVarRequestOptions.HeaderParameters.Add("required_boolean_group", Org.OpenAPITools.Client.ClientUtils.ParameterToString(requiredBooleanGroup)); // header parameter if (booleanGroup != null) @@ -2433,39 +2349,15 @@ namespace Org.OpenAPITools.Api foreach (var _accept in _accepts) localVarRequestOptions.HeaderParameters.Add("Accept", _accept); - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_string_group", requiredStringGroup)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_int64_group", requiredInt64Group)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_string_group", requiredStringGroup)); + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "required_int64_group", requiredInt64Group)); if (stringGroup != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "string_group", stringGroup)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "string_group", stringGroup)); } if (int64Group != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "int64_group", int64Group)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "int64_group", int64Group)); } localVarRequestOptions.HeaderParameters.Add("required_boolean_group", Org.OpenAPITools.Client.ClientUtils.ParameterToString(requiredBooleanGroup)); // header parameter if (booleanGroup != null) @@ -2797,53 +2689,23 @@ namespace Org.OpenAPITools.Api if (pipe != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "pipe", pipe)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "pipe", pipe)); } if (ioutil != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "ioutil", ioutil)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "ioutil", ioutil)); } if (http != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("space", "http", http)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("space", "http", http)); } if (url != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "url", url)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "url", url)); } if (context != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("multi", "context", context)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("multi", "context", context)); } @@ -2925,53 +2787,23 @@ namespace Org.OpenAPITools.Api if (pipe != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "pipe", pipe)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "pipe", pipe)); } if (ioutil != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "ioutil", ioutil)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "ioutil", ioutil)); } if (http != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("space", "http", http)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("space", "http", http)); } if (url != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "url", url)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "url", url)); } if (context != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("multi", "context", context)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("multi", "context", context)); } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs index ebf022334c..492bae795c 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/FakeClassnameTags123Api.cs @@ -244,13 +244,7 @@ namespace Org.OpenAPITools.Api // authentication (api_key_query) required if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("api_key_query"))) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "api_key_query", this.Configuration.GetApiKeyWithPrefix("api_key_query"))) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "api_key_query", this.Configuration.GetApiKeyWithPrefix("api_key_query"))); } // make the HTTP request @@ -313,13 +307,7 @@ namespace Org.OpenAPITools.Api // authentication (api_key_query) required if (!String.IsNullOrEmpty(this.Configuration.GetApiKeyWithPrefix("api_key_query"))) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "api_key_query", this.Configuration.GetApiKeyWithPrefix("api_key_query"))) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "api_key_query", this.Configuration.GetApiKeyWithPrefix("api_key_query"))); } // make the HTTP request diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/PetApi.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/PetApi.cs index 5c4a624d26..0fe84290ce 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/PetApi.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/PetApi.cs @@ -851,13 +851,7 @@ namespace Org.OpenAPITools.Api if (status != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "status", status)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "status", status)); } // authentication (petstore_auth) required @@ -924,13 +918,7 @@ namespace Org.OpenAPITools.Api if (status != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "status", status)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "status", status)); } // authentication (petstore_auth) required @@ -996,13 +984,7 @@ namespace Org.OpenAPITools.Api if (tags != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "tags", tags)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "tags", tags)); } // authentication (petstore_auth) required @@ -1069,13 +1051,7 @@ namespace Org.OpenAPITools.Api if (tags != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "tags", tags)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("csv", "tags", tags)); } // authentication (petstore_auth) required diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/UserApi.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/UserApi.cs index 3f1585ce46..57e4e369cc 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/UserApi.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Api/UserApi.cs @@ -1100,23 +1100,11 @@ namespace Org.OpenAPITools.Api if (username != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "username", username)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "username", username)); } if (password != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "password", password)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "password", password)); } @@ -1183,23 +1171,11 @@ namespace Org.OpenAPITools.Api if (username != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "username", username)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "username", username)); } if (password != null) { - foreach (var _kvp in Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "password", password)) - { - foreach (var _kvpValue in _kvp.Value) - { - localVarRequestOptions.QueryParameters.Add(_kvp.Key, _kvpValue); - } - } + localVarRequestOptions.QueryParameters.Add(Org.OpenAPITools.Client.ClientUtils.ParameterToMultiMap("", "password", password)); } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ClientUtils.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ClientUtils.cs index dde50ca71d..764ea009ef 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ClientUtils.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/ClientUtils.cs @@ -11,13 +11,10 @@ using System; using System.Collections; -using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Text; using System.Text.RegularExpressions; -using System.Threading.Tasks; using KellermanSoftware.CompareNetObjects; namespace Org.OpenAPITools.Client @@ -63,15 +60,11 @@ namespace Org.OpenAPITools.Client { var parameters = new Multimap(); - if (IsCollection(value) && collectionFormat == "multi") + if (value is ICollection collection && collectionFormat == "multi") { - var valueCollection = value as IEnumerable; - if (valueCollection != null) + foreach (var item in collection) { - foreach (var item in valueCollection) - { - parameters.Add(name, ParameterToString(item)); - } + parameters.Add(name, ParameterToString(item)); } } else @@ -92,49 +85,26 @@ namespace Org.OpenAPITools.Client /// Formatted string. public static string ParameterToString(object obj, IReadableConfiguration configuration = null) { - if (obj is DateTime) + if (obj is DateTime dateTime) // Return a formatted date string - Can be customized with Configuration.DateTimeFormat // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // For example: 2009-06-15T13:45:30.0000000 - return ((DateTime)obj).ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); - else if (obj is DateTimeOffset) + return dateTime.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); + if (obj is DateTimeOffset dateTimeOffset) // Return a formatted date string - Can be customized with Configuration.DateTimeFormat // Defaults to an ISO 8601, using the known as a Round-trip date/time pattern ("o") // https://msdn.microsoft.com/en-us/library/az4se3k1(v=vs.110).aspx#Anchor_8 // For example: 2009-06-15T13:45:30.0000000 - return ((DateTimeOffset)obj).ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); - else if (obj is bool) - return (bool)obj ? "true" : "false"; - else - { - if (obj is IList) - { - var list = obj as IList; - var flattenedString = new StringBuilder(); - foreach (var param in list) - { - if (flattenedString.Length > 0) - flattenedString.Append(","); - flattenedString.Append(param); - } - return flattenedString.ToString(); - } - - return Convert.ToString (obj); - } + return dateTimeOffset.ToString((configuration ?? GlobalConfiguration.Instance).DateTimeFormat); + if (obj is bool boolean) + return boolean ? "true" : "false"; + if (obj is ICollection collection) + return string.Join(",", collection.Cast()); + + return Convert.ToString(obj); } - - /// - /// Check if generic object is a collection. - /// - /// - /// True if object is a collection type - private static bool IsCollection(object value) - { - return value is IList || value is ICollection; - } - + /// /// URL encode a string /// Credit/Ref: https://github.com/restsharp/RestSharp/blob/master/RestSharp/Extensions/StringExtensions.cs#L50 @@ -177,7 +147,7 @@ namespace Org.OpenAPITools.Client /// Encoded string. public static string Base64Encode(string text) { - return System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); + return Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(text)); } /// @@ -187,14 +157,9 @@ namespace Org.OpenAPITools.Client /// Byte array public static byte[] ReadAsBytes(Stream inputStream) { - byte[] buf = new byte[16*1024]; - using (MemoryStream ms = new MemoryStream()) + using (var ms = new MemoryStream()) { - int count; - while ((count = inputStream.Read(buf, 0, buf.Length)) > 0) - { - ms.Write(buf, 0, count); - } + inputStream.CopyTo(ms); return ms.ToArray(); } } diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Multimap.cs b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Multimap.cs index 4bde7f7ffe..b0449fb764 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Multimap.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClientCore/src/Org.OpenAPITools/Client/Multimap.cs @@ -11,7 +11,6 @@ using System; using System.Collections; -using System.Collections.Concurrent; using System.Collections.Generic; namespace Org.OpenAPITools.Client @@ -19,13 +18,13 @@ namespace Org.OpenAPITools.Client /// /// A dictionary in which one key has many associated values. /// - /// The type of the key + /// The type of the key /// The type of the value associated with the key. - public class Multimap : IDictionary> + public class Multimap : IDictionary> { #region Private Fields - private readonly ConcurrentDictionary> _dictionary; + private readonly Dictionary> _dictionary; #endregion Private Fields @@ -36,16 +35,16 @@ namespace Org.OpenAPITools.Client /// public Multimap() { - _dictionary = new ConcurrentDictionary>(); + _dictionary = new Dictionary>(); } /// /// Constructor with comparer. /// /// - public Multimap(IEqualityComparer comparer) + public Multimap(IEqualityComparer comparer) { - _dictionary = new ConcurrentDictionary>(comparer); + _dictionary = new Dictionary>(comparer); } #endregion Constructors @@ -56,7 +55,7 @@ namespace Org.OpenAPITools.Client /// To get the enumerator. /// /// Enumerator - public IEnumerator>> GetEnumerator() + public IEnumerator>> GetEnumerator() { return _dictionary.GetEnumerator(); } @@ -77,12 +76,25 @@ namespace Org.OpenAPITools.Client /// Add values to Multimap /// /// Key value pair - public void Add(KeyValuePair> item) + public void Add(KeyValuePair> item) { if (!TryAdd(item.Key, item.Value)) throw new InvalidOperationException("Could not add values to Multimap."); } + /// + /// Add Multimap to Multimap + /// + /// Multimap + public void Add(Multimap multimap) + { + foreach (var item in multimap) + { + if (!TryAdd(item.Key, item.Value)) + throw new InvalidOperationException("Could not add values to Multimap."); + } + } + /// /// Clear Multimap /// @@ -97,7 +109,7 @@ namespace Org.OpenAPITools.Client /// Key value pair /// Method needs to be implemented /// true if the Multimap contains the item; otherwise, false. - public bool Contains(KeyValuePair> item) + public bool Contains(KeyValuePair> item) { throw new NotImplementedException(); } @@ -110,7 +122,7 @@ namespace Org.OpenAPITools.Client /// from Multimap. The array must have zero-based indexing. /// The zero-based index in array at which copying begins. /// Method needs to be implemented - public void CopyTo(KeyValuePair>[] array, int arrayIndex) + public void CopyTo(KeyValuePair>[] array, int arrayIndex) { throw new NotImplementedException(); } @@ -121,7 +133,7 @@ namespace Org.OpenAPITools.Client /// Key value pair /// true if the item is successfully removed; otherwise, false. /// Method needs to be implemented - public bool Remove(KeyValuePair> item) + public bool Remove(KeyValuePair> item) { throw new NotImplementedException(); } @@ -129,24 +141,12 @@ namespace Org.OpenAPITools.Client /// /// Gets the number of items contained in the Multimap. /// - public int Count - { - get - { - return _dictionary.Count; - } - } + public int Count => _dictionary.Count; /// /// Gets a value indicating whether the Multimap is read-only. /// - public bool IsReadOnly - { - get - { - return false; - } - } + public bool IsReadOnly => false; /// /// Adds an item with the provided key and value to the Multimap. @@ -154,12 +154,11 @@ namespace Org.OpenAPITools.Client /// The object to use as the key of the item to add. /// The object to use as the value of the item to add. /// Thrown when couldn't add the value to Multimap. - public void Add(T key, IList value) + public void Add(TKey key, IList value) { if (value != null && value.Count > 0) { - IList list; - if (_dictionary.TryGetValue(key, out list)) + if (_dictionary.TryGetValue(key, out var list)) { foreach (var k in value) list.Add(k); } @@ -178,7 +177,7 @@ namespace Org.OpenAPITools.Client /// The key to locate in the Multimap. /// true if the Multimap contains an item with /// the key; otherwise, false. - public bool ContainsKey(T key) + public bool ContainsKey(TKey key) { return _dictionary.ContainsKey(key); } @@ -188,10 +187,9 @@ namespace Org.OpenAPITools.Client /// /// The key to locate in the Multimap. /// true if the item is successfully removed; otherwise, false. - public bool Remove(T key) + public bool Remove(TKey key) { - IList list; - return TryRemove(key, out list); + return TryRemove(key, out var _); } /// @@ -203,7 +201,7 @@ namespace Org.OpenAPITools.Client /// This parameter is passed uninitialized. /// true if the object that implements Multimap contains /// an item with the specified key; otherwise, false. - public bool TryGetValue(T key, out IList value) + public bool TryGetValue(TKey key, out IList value) { return _dictionary.TryGetValue(key, out value); } @@ -213,36 +211,21 @@ namespace Org.OpenAPITools.Client /// /// The key of the item to get or set. /// The value of the specified key. - public IList this[T key] + public IList this[TKey key] { - get - { - return _dictionary[key]; - } - set { _dictionary[key] = value; } + get => _dictionary[key]; + set => _dictionary[key] = value; } /// /// Gets a System.Collections.Generic.ICollection containing the keys of the Multimap. /// - public ICollection Keys - { - get - { - return _dictionary.Keys; - } - } + public ICollection Keys => _dictionary.Keys; /// /// Gets a System.Collections.Generic.ICollection containing the values of the Multimap. /// - public ICollection> Values - { - get - { - return _dictionary.Values; - } - } + public ICollection> Values => _dictionary.Values; /// /// Copy the items of the Multimap to an System.Array, @@ -253,7 +236,7 @@ namespace Org.OpenAPITools.Client /// The zero-based index in array at which copying begins. public void CopyTo(Array array, int index) { - ((ICollection) _dictionary).CopyTo(array, index); + ((ICollection)_dictionary).CopyTo(array, index); } /// @@ -262,19 +245,17 @@ namespace Org.OpenAPITools.Client /// The object to use as the key of the item to add. /// The object to use as the value of the item to add. /// Thrown when couldn't add value to Multimap. - public void Add(T key, TValue value) + public void Add(TKey key, TValue value) { if (value != null) { - IList list; - if (_dictionary.TryGetValue(key, out list)) + if (_dictionary.TryGetValue(key, out var list)) { list.Add(value); } else { - list = new List(); - list.Add(value); + list = new List { value }; if (!TryAdd(key, list)) throw new InvalidOperationException("Could not add value to Multimap."); } @@ -288,18 +269,27 @@ namespace Org.OpenAPITools.Client /** * Helper method to encapsulate generator differences between dictionary types. */ - private bool TryRemove(T key, out IList value) + private bool TryRemove(TKey key, out IList value) { - return _dictionary.TryRemove(key, out value); - + _dictionary.TryGetValue(key, out value); + return _dictionary.Remove(key); } /** * Helper method to encapsulate generator differences between dictionary types. */ - private bool TryAdd(T key, IList value) + private bool TryAdd(TKey key, IList value) { - return _dictionary.TryAdd(key, value); + try + { + _dictionary.Add(key, value); + } + catch (ArgumentException) + { + return false; + } + + return true; } #endregion Private Members }