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 5b47038bf2d..b045af07a2e 100644 --- a/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/csharp-netcore/Configuration.mustache @@ -49,11 +49,13 @@ namespace {{packageName}}.Client string.Format("Error calling {0}: {1}", methodName, response.RawContent), response.RawContent, response.Headers); } - {{^netStandard}}if (status == 0) + {{^netStandard}} + if (status == 0) { return new ApiException(status, string.Format("Error calling {0}: {1}", methodName, response.ErrorText), response.ErrorText); - }{{/netStandard}} + } + {{/netStandard}} return null; }; @@ -82,6 +84,14 @@ namespace {{packageName}}.Client private string _dateTimeFormat = ISO8601_DATETIME_FORMAT; private string _tempFolderPath = Path.GetTempPath(); + {{#servers.0}} + + /// + /// Gets or sets the servers defined in the OpenAPI spec. + /// + /// The servers + private IList> _servers; + {{/servers.0}} /// /// HTTPSigning configuration @@ -102,6 +112,48 @@ namespace {{packageName}}.Client DefaultHeaders = new {{^net35}}Concurrent{{/net35}}Dictionary(); ApiKey = new {{^net35}}Concurrent{{/net35}}Dictionary(); ApiKeyPrefix = new {{^net35}}Concurrent{{/net35}}Dictionary(); + {{#servers}} + {{#-first}} + Servers = new List>() + { + {{/-first}} + { + new Dictionary { + {"url", "{{{url}}}"}, + {"description", "{{{description}}}{{^description}}No description provided{{/description}}"}, + {{#variables}} + {{#-first}} + { + "variables", new Dictionary { + {{/-first}} + { + "{{{name}}}", new Dictionary { + {"description", "{{{description}}}{{^description}}No description provided{{/description}}"}, + {"default_value", "{{{defaultValue}}}"}, + {{#enumValues}} + {{#-first}} + { + "enum_values", new List() { + {{/-first}} + "{{{.}}}"{{^-last}},{{/-last}} + {{#-last}} + } + } + {{/-last}} + {{/enumValues}} + } + }{{^-last}},{{/-last}} + {{#-last}} + } + } + {{/-last}} + {{/variables}} + } + }{{^-last}},{{/-last}} + {{#-last}} + }; + {{/-last}} + {{/servers}} // Setting Timeout has side effects (forces ApiClient creation). Timeout = 100000; @@ -337,6 +389,84 @@ namespace {{packageName}}.Client _apiKey = value; } } + {{#servers.0}} + + /// + /// Gets or sets the servers. + /// + /// The servers. + public virtual IList> Servers + { + get { return _servers; } + set + { + if (value == null) + { + throw new InvalidOperationException("Servers may not be null."); + } + _servers = value; + } + } + + /// + /// Returns URL based on server settings without providing values + /// for the variables + /// + /// Array index of the server settings. + /// The server URL. + public string GetServerUrl(int index) + { + return GetServerUrl(index, null); + } + + /// + /// Returns URL based on server settings. + /// + /// Array index of the server settings. + /// Dictionary of the variables and the corresponding values. + /// The server URL. + public string GetServerUrl(int index, Dictionary inputVariables) + { + if (index < 0 || index >= Servers.Count) + { + throw new InvalidOperationException($"Invalid index {index} when selecting the server. Must be less than {Servers.Count}."); + } + + if (inputVariables == null) + { + inputVariables = new Dictionary(); + } + + IReadOnlyDictionary server = Servers[index]; + string url = (string)server["url"]; + + // go through variable and assign a value + foreach (KeyValuePair variable in (IReadOnlyDictionary)server["variables"]) + { + + IReadOnlyDictionary serverVariables = (IReadOnlyDictionary)(variable.Value); + + if (inputVariables.ContainsKey(variable.Key)) + { + if (((List)serverVariables["enum_values"]).Contains(inputVariables[variable.Key])) + { + url = url.Replace("{" + variable.Key + "}", inputVariables[variable.Key]); + } + else + { + throw new InvalidOperationException($"The variable `{variable.Key}` in the server URL has invalid value #{inputVariables[variable.Key]}. Must be {(List)serverVariables["enum_values"]}"); + } + } + else + { + // use defualt value + url = url.Replace("{" + variable.Key + "}", (string)serverVariables["default_value"]); + } + } + + return url; + } + {{/servers.0}} /// /// Gets and Sets the HTTPSigningConfiuration @@ -431,4 +561,4 @@ namespace {{packageName}}.Client } #endregion Static Members } -} \ No newline at end of file +} diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs index cff914445f5..b3376d6045e 100644 --- a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/Api/PetApiTests.cs @@ -24,7 +24,7 @@ using Org.OpenAPITools.Model; namespace Org.OpenAPITools.Test { /// - /// Class for testing PetApi + /// Class for testing PetApi /// /// /// This file is automatically generated by OpenAPI Generator (https://openapi-generator.tech). diff --git a/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ConfigurationTests.cs b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ConfigurationTests.cs new file mode 100644 index 00000000000..bedfab04c34 --- /dev/null +++ b/samples/client/petstore/csharp-netcore/OpenAPIClient/src/Org.OpenAPITools.Test/ConfigurationTests.cs @@ -0,0 +1,46 @@ +using System; +using System.IO; +using System.Collections.Generic; +using System.Collections.ObjectModel; +using System.Linq; +using System.Reflection; +using RestSharp; +using Xunit; + +using Org.OpenAPITools.Client; +using Org.OpenAPITools.Api; +using Org.OpenAPITools.Model; + +namespace Org.OpenAPITools.Test +{ + /// + /// Class for testing Configuration + /// + public class ConfigurationTests + { + public ConfigurationTests() + { + } + + /// + /// Test GetServerUrl + /// + [Fact] + public void GetServerUrlTest() + { + Configuration c = new Configuration(); + // no variable (null) provided + Assert.Equal("https://localhost:8080/v2", c.GetServerUrl(1, null)); + // no variable (empty dictionary) provided + Assert.Equal("https://localhost:8080/v2", c.GetServerUrl(1, new Dictionary())); + + Assert.Equal("https://localhost:8080/v1", c.GetServerUrl(1, new Dictionary() { { "version", "v1" } })); + + Assert.Throws(() => c.GetServerUrl(1, new Dictionary() { { "version", "v3" } })); + + // test the first server (index 0) + Assert.Equal("http://petstore.swagger.io:80/v2", c.GetServerUrl(0)); + + } + } +} 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 e3a80be0f9c..70b9fd37343 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 @@ -55,7 +55,6 @@ namespace Org.OpenAPITools.Client string.Format("Error calling {0}: {1}", methodName, response.RawContent), response.RawContent, response.Headers); } - return null; }; @@ -85,6 +84,12 @@ namespace Org.OpenAPITools.Client private string _dateTimeFormat = ISO8601_DATETIME_FORMAT; private string _tempFolderPath = Path.GetTempPath(); + /// + /// Gets or sets the servers defined in the OpenAPI spec. + /// + /// The servers + private IList> _servers; + /// /// HTTPSigning configuration /// @@ -104,6 +109,66 @@ namespace Org.OpenAPITools.Client DefaultHeaders = new ConcurrentDictionary(); ApiKey = new ConcurrentDictionary(); ApiKeyPrefix = new ConcurrentDictionary(); + Servers = new List>() + { + { + new Dictionary { + {"url", "http://{server}.swagger.io:{port}/v2"}, + {"description", "petstore server"}, + { + "variables", new Dictionary { + { + "server", new Dictionary { + {"description", "No description provided"}, + {"default_value", "petstore"}, + { + "enum_values", new List() { + "petstore", + "qa-petstore", + "dev-petstore" + } + } + } + }, + { + "port", new Dictionary { + {"description", "No description provided"}, + {"default_value", "80"}, + { + "enum_values", new List() { + "80", + "8080" + } + } + } + } + } + } + } + }, + { + new Dictionary { + {"url", "https://localhost:8080/{version}"}, + {"description", "The local server"}, + { + "variables", new Dictionary { + { + "version", new Dictionary { + {"description", "No description provided"}, + {"default_value", "v2"}, + { + "enum_values", new List() { + "v1", + "v2" + } + } + } + } + } + } + } + } + }; // Setting Timeout has side effects (forces ApiClient creation). Timeout = 100000; @@ -340,6 +405,82 @@ namespace Org.OpenAPITools.Client } } + /// + /// Gets or sets the servers. + /// + /// The servers. + public virtual IList> Servers + { + get { return _servers; } + set + { + if (value == null) + { + throw new InvalidOperationException("Servers may not be null."); + } + _servers = value; + } + } + + /// + /// Returns URL based on server settings without providing values + /// for the variables + /// + /// Array index of the server settings. + /// The server URL. + public string GetServerUrl(int index) + { + return GetServerUrl(index, null); + } + + /// + /// Returns URL based on server settings. + /// + /// Array index of the server settings. + /// Dictionary of the variables and the corresponding values. + /// The server URL. + public string GetServerUrl(int index, Dictionary inputVariables) + { + if (index < 0 || index >= Servers.Count) + { + throw new InvalidOperationException($"Invalid index {index} when selecting the server. Must be less than {Servers.Count}."); + } + + if (inputVariables == null) + { + inputVariables = new Dictionary(); + } + + IReadOnlyDictionary server = Servers[index]; + string url = (string)server["url"]; + + // go through variable and assign a value + foreach (KeyValuePair variable in (IReadOnlyDictionary)server["variables"]) + { + + IReadOnlyDictionary serverVariables = (IReadOnlyDictionary)(variable.Value); + + if (inputVariables.ContainsKey(variable.Key)) + { + if (((List)serverVariables["enum_values"]).Contains(inputVariables[variable.Key])) + { + url = url.Replace("{" + variable.Key + "}", inputVariables[variable.Key]); + } + else + { + throw new InvalidOperationException($"The variable `{variable.Key}` in the server URL has invalid value #{inputVariables[variable.Key]}. Must be {(List)serverVariables["enum_values"]}"); + } + } + else + { + // use defualt value + url = url.Replace("{" + variable.Key + "}", (string)serverVariables["default_value"]); + } + } + + return url; + } + /// /// Gets and Sets the HTTPSigningConfiuration /// @@ -427,4 +568,4 @@ namespace Org.OpenAPITools.Client } #endregion Static Members } -} \ No newline at end of file +} 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 0de61db1c17..36261099cd6 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 @@ -89,6 +89,12 @@ namespace Org.OpenAPITools.Client private string _dateTimeFormat = ISO8601_DATETIME_FORMAT; private string _tempFolderPath = Path.GetTempPath(); + /// + /// Gets or sets the servers defined in the OpenAPI spec. + /// + /// The servers + private IList> _servers; + /// /// HTTPSigning configuration /// @@ -108,6 +114,66 @@ namespace Org.OpenAPITools.Client DefaultHeaders = new ConcurrentDictionary(); ApiKey = new ConcurrentDictionary(); ApiKeyPrefix = new ConcurrentDictionary(); + Servers = new List>() + { + { + new Dictionary { + {"url", "http://{server}.swagger.io:{port}/v2"}, + {"description", "petstore server"}, + { + "variables", new Dictionary { + { + "server", new Dictionary { + {"description", "No description provided"}, + {"default_value", "petstore"}, + { + "enum_values", new List() { + "petstore", + "qa-petstore", + "dev-petstore" + } + } + } + }, + { + "port", new Dictionary { + {"description", "No description provided"}, + {"default_value", "80"}, + { + "enum_values", new List() { + "80", + "8080" + } + } + } + } + } + } + } + }, + { + new Dictionary { + {"url", "https://localhost:8080/{version}"}, + {"description", "The local server"}, + { + "variables", new Dictionary { + { + "version", new Dictionary { + {"description", "No description provided"}, + {"default_value", "v2"}, + { + "enum_values", new List() { + "v1", + "v2" + } + } + } + } + } + } + } + } + }; // Setting Timeout has side effects (forces ApiClient creation). Timeout = 100000; @@ -344,6 +410,82 @@ namespace Org.OpenAPITools.Client } } + /// + /// Gets or sets the servers. + /// + /// The servers. + public virtual IList> Servers + { + get { return _servers; } + set + { + if (value == null) + { + throw new InvalidOperationException("Servers may not be null."); + } + _servers = value; + } + } + + /// + /// Returns URL based on server settings without providing values + /// for the variables + /// + /// Array index of the server settings. + /// The server URL. + public string GetServerUrl(int index) + { + return GetServerUrl(index, null); + } + + /// + /// Returns URL based on server settings. + /// + /// Array index of the server settings. + /// Dictionary of the variables and the corresponding values. + /// The server URL. + public string GetServerUrl(int index, Dictionary inputVariables) + { + if (index < 0 || index >= Servers.Count) + { + throw new InvalidOperationException($"Invalid index {index} when selecting the server. Must be less than {Servers.Count}."); + } + + if (inputVariables == null) + { + inputVariables = new Dictionary(); + } + + IReadOnlyDictionary server = Servers[index]; + string url = (string)server["url"]; + + // go through variable and assign a value + foreach (KeyValuePair variable in (IReadOnlyDictionary)server["variables"]) + { + + IReadOnlyDictionary serverVariables = (IReadOnlyDictionary)(variable.Value); + + if (inputVariables.ContainsKey(variable.Key)) + { + if (((List)serverVariables["enum_values"]).Contains(inputVariables[variable.Key])) + { + url = url.Replace("{" + variable.Key + "}", inputVariables[variable.Key]); + } + else + { + throw new InvalidOperationException($"The variable `{variable.Key}` in the server URL has invalid value #{inputVariables[variable.Key]}. Must be {(List)serverVariables["enum_values"]}"); + } + } + else + { + // use defualt value + url = url.Replace("{" + variable.Key + "}", (string)serverVariables["default_value"]); + } + } + + return url; + } + /// /// Gets and Sets the HTTPSigningConfiuration /// @@ -432,4 +574,4 @@ namespace Org.OpenAPITools.Client } #endregion Static Members } -} \ No newline at end of file +}