From 0e6c58361f55499385c27cde291e66e4ffabdc68 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Tue, 22 Jan 2019 23:45:47 +0800 Subject: [PATCH 1/4] add multiple server support --- .../main/resources/php/Configuration.mustache | 78 ++++++++++++++++ .../OpenAPIClient-php/lib/Configuration.php | 88 +++++++++++++++++++ .../tests/ConfigurationTest.php | 56 ++++++++++++ 3 files changed, 222 insertions(+) create mode 100644 samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php diff --git a/modules/openapi-generator/src/main/resources/php/Configuration.mustache b/modules/openapi-generator/src/main/resources/php/Configuration.mustache index 44c80ecb7c85..ea99453d4ae2 100644 --- a/modules/openapi-generator/src/main/resources/php/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/php/Configuration.mustache @@ -419,4 +419,82 @@ class Configuration return $keyWithPrefix; } + + /** + * Returns an array of host setting + * + * @return an array of host setting + */ + public function getHostSettings() + { + return array( + {{#servers}} + array( + "url" => "{{{url}}}", + "description" => "{{{description}}}{{^description}}No description provided{{/description}}", + {{#variables}} + {{#-first}} + "variables" => array( + {{/-first}} + "{{{name}}}" => array( + "description" => "{{{description}}}{{^description}}No description provided{{/description}}", + "default_value" => "{{{defaultValue}}}", + {{#enumValues}} + {{#-first}} + "enum_values" => array( + {{/-first}} + "{{{.}}}"{{^-last}},{{/-last}} + {{#-last}} + ) + {{/-last}} + {{/enumValues}} + ){{^-last}},{{/-last}} + {{#-last}} + ) + {{/-last}} + {{/variables}} + ){{^-last}},{{/-last}} + {{/servers}} + ); + } + + /** + * Returns URL based on index and variables + * + * @param index array index of the host settings + * @param variables hash of variable and the corresponding value + * @return URL based on host settings + */ + public function getHostFromSettings($index, $variables) + { + if (null === $variables) { + $variables = array(); + } + + $hosts = $this->getHostSettings(); + + // check array index out of bound + if ($index < 0 || $index > sizeof($hosts)) { + throw new \InvalidArgumentException("Invalid index $index when selecting the server. Must be less than ".sizeof(servers)); + } + + $host = $hosts[$index]; + $url = $host["url"]; + + // go through variable and assign a value + foreach ($host["variables"] as $name => $variable) { + if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user + if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum + $url = str_replace("{".$name."}", $variables[$name], $url); + } else { + throw new \InvalidArgumentException("The variable `$name` in the server URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); + } + } else { + // use default value + $url = str_replace("{".$name."}", $variable["default_value"], $url); + } + } + + return $url; + } } diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index cc602482c0eb..f5df0dda7431 100644 --- a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -426,4 +426,92 @@ public function getApiKeyWithPrefix($apiKeyIdentifier) return $keyWithPrefix; } + + /** + * Returns an array of host setting + * + * @return an array of host setting + */ + public function getHostSettings() + { + return array( + array( + "url" => "http://{server}.swagger.io:{port}/v2", + "description" => "petstore server", + "variables" => array( + "server" => array( + "description" => "No description provided", + "default_value" => "petstore", + "enum_values" => array( + "petstore", + "qa-petstore", + "dev-petstore" + ) + ), + "port" => array( + "description" => "No description provided", + "default_value" => "80", + "enum_values" => array( + "80", + "8080" + ) + ) + ) + ), + array( + "url" => "https://localhost:8080/{version}", + "description" => "The local server", + "variables" => array( + "version" => array( + "description" => "No description provided", + "default_value" => "v2", + "enum_values" => array( + "v1", + "v2" + ) + ) + ) + ) + ); + } + + /** + * Returns URL based on index and variables + * + * @param index array index of the host settings + * @param variables hash of variable and the corresponding value + * @return URL based on host settings + */ + public function getHostFromSettings($index, $variables) + { + if (null === $variables) { + $variables = array(); + } + + $hosts = $this->getHostSettings(); + + // check array index out of bound + if ($index < 0 || $index > sizeof($hosts)) { + throw new \InvalidArgumentException("Invalid index $index when selecting the server. Must be less than ".sizeof(servers)); + } + + $host = $hosts[$index]; + $url = $host["url"]; + + // go through variable and assign a value + foreach ($host["variables"] as $name => $variable) { + if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user + if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum + $url = str_replace("{".$name."}", $variables[$name], $url); + } else { + throw new \InvalidArgumentException("The variable `$name` in the server URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); + } + } else { + // use default value + $url = str_replace("{".$name."}", $variable["default_value"], $url); + } + } + + return $url; + } } diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php new file mode 100644 index 000000000000..ed8e4ce4823b --- /dev/null +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php @@ -0,0 +1,56 @@ +getHostSettings(); + + $this->assertCount(2, $servers); + $this->assertSame("http://{server}.swagger.io:{port}/v2", $servers[0]["url"]); + $this->assertSame("petstore", $servers[0]["variables"]["server"]["default_value"]); + $this->assertSame("80", $servers[0]["variables"]["port"]["default_value"]); + $this->assertSame(array("80", "8080"), $servers[0]["variables"]["port"]["enum_values"]); + } + + /** + * Test server settings + */ + public function testServerUrl() + { + $config = new Configuration(); + // default value + $url = $config->getHostFromSettings(0, null); + $this->assertSame("http://petstore.swagger.io:80/v2", $url); + + // using a variable + $url = $config->getHostFromSettings(0, array("server" => "dev-petstore")); + $this->assertSame("http://dev-petstore.swagger.io:80/v2", $url); + + // using 2 variables + $url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8080")); + $this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url); + } + + /** + * Test server settings with invalid vaues + * @expectedException InvalidArgumentException + * @expectedExceptionMessage The variable `port` in the server URL has invalid value 8. Must be 80,8080 + */ + public function testServerUrlWithInvalidValues() + { + // using 2 variables with invalid values + $config = new Configuration(); + $url = $config->getHostFromSettings(0, array("server" => "dev-petstore", "port" => "8")); + $this->assertSame("http://dev-petstore.swagger.io:8080/v2", $url); + } +} From 5adb797981a08cbfb18bfe9a4c0ce812f18e759b Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 23 Jan 2019 00:14:18 +0800 Subject: [PATCH 2/4] update php samples --- .../OpenAPIClient-php/lib/Configuration.php | 55 +++++++++++++++++++ 1 file changed, 55 insertions(+) diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index cc602482c0eb..6b78cdcfaaa8 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -426,4 +426,59 @@ public function getApiKeyWithPrefix($apiKeyIdentifier) return $keyWithPrefix; } + + /** + * Returns an array of host setting + * + * @return an array of host setting + */ + public function getHostSettings() + { + return array( + array( + "url" => "http://petstore.swagger.io:80/v2", + "description" => "No description provided", + ) + ); + } + + /** + * Returns URL based on index and variables + * + * @param index array index of the host settings + * @param variables hash of variable and the corresponding value + * @return URL based on host settings + */ + public function getHostFromSettings($index, $variables) + { + if (null === $variables) { + $variables = array(); + } + + $hosts = $this->getHostSettings(); + + // check array index out of bound + if ($index < 0 || $index > sizeof($hosts)) { + throw new \InvalidArgumentException("Invalid index $index when selecting the server. Must be less than ".sizeof(servers)); + } + + $host = $hosts[$index]; + $url = $host["url"]; + + // go through variable and assign a value + foreach ($host["variables"] as $name => $variable) { + if (array_key_exists($name, $variables)) { // check to see if it's in the variables provided by the user + if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum + $url = str_replace("{".$name."}", $variables[$name], $url); + } else { + throw new \InvalidArgumentException("The variable `$name` in the server URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); + } + } else { + // use default value + $url = str_replace("{".$name."}", $variable["default_value"], $url); + } + } + + return $url; + } } From 9ad9d1f40daee71b5d3893211d605b8b3b30e213 Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 23 Jan 2019 01:09:36 +0800 Subject: [PATCH 3/4] update wording --- .../src/main/resources/php/Configuration.mustache | 10 +++++----- .../php/OpenAPIClient-php/lib/Configuration.php | 10 +++++----- .../php/OpenAPIClient-php/lib/Configuration.php | 10 +++++----- .../php/OpenAPIClient-php/tests/ConfigurationTest.php | 6 +++--- 4 files changed, 18 insertions(+), 18 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php/Configuration.mustache b/modules/openapi-generator/src/main/resources/php/Configuration.mustache index ea99453d4ae2..bf0d4d8882e8 100644 --- a/modules/openapi-generator/src/main/resources/php/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/php/Configuration.mustache @@ -421,9 +421,9 @@ class Configuration } /** - * Returns an array of host setting + * Returns an array of host settings * - * @return an array of host setting + * @return an array of host settings */ public function getHostSettings() { @@ -459,7 +459,7 @@ class Configuration } /** - * Returns URL based on index and variables + * Returns URL based on the index and variables * * @param index array index of the host settings * @param variables hash of variable and the corresponding value @@ -475,7 +475,7 @@ class Configuration // check array index out of bound if ($index < 0 || $index > sizeof($hosts)) { - throw new \InvalidArgumentException("Invalid index $index when selecting the server. Must be less than ".sizeof(servers)); + throw new \InvalidArgumentException("Invalid index $index when selecting the host. Must be less than ".sizeof($hosts)); } $host = $hosts[$index]; @@ -487,7 +487,7 @@ class Configuration if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum $url = str_replace("{".$name."}", $variables[$name], $url); } else { - throw new \InvalidArgumentException("The variable `$name` in the server URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); + throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); } } else { // use default value diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index 6b78cdcfaaa8..0c886905b11f 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -428,9 +428,9 @@ public function getApiKeyWithPrefix($apiKeyIdentifier) } /** - * Returns an array of host setting + * Returns an array of host settings * - * @return an array of host setting + * @return an array of host settings */ public function getHostSettings() { @@ -443,7 +443,7 @@ public function getHostSettings() } /** - * Returns URL based on index and variables + * Returns URL based on the index and variables * * @param index array index of the host settings * @param variables hash of variable and the corresponding value @@ -459,7 +459,7 @@ public function getHostFromSettings($index, $variables) // check array index out of bound if ($index < 0 || $index > sizeof($hosts)) { - throw new \InvalidArgumentException("Invalid index $index when selecting the server. Must be less than ".sizeof(servers)); + throw new \InvalidArgumentException("Invalid index $index when selecting the host. Must be less than ".sizeof($hosts)); } $host = $hosts[$index]; @@ -471,7 +471,7 @@ public function getHostFromSettings($index, $variables) if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum $url = str_replace("{".$name."}", $variables[$name], $url); } else { - throw new \InvalidArgumentException("The variable `$name` in the server URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); + throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); } } else { // use default value diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index f5df0dda7431..9a370bd21f96 100644 --- a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -428,9 +428,9 @@ public function getApiKeyWithPrefix($apiKeyIdentifier) } /** - * Returns an array of host setting + * Returns an array of host settings * - * @return an array of host setting + * @return an array of host settings */ public function getHostSettings() { @@ -476,7 +476,7 @@ public function getHostSettings() } /** - * Returns URL based on index and variables + * Returns URL based on the index and variables * * @param index array index of the host settings * @param variables hash of variable and the corresponding value @@ -492,7 +492,7 @@ public function getHostFromSettings($index, $variables) // check array index out of bound if ($index < 0 || $index > sizeof($hosts)) { - throw new \InvalidArgumentException("Invalid index $index when selecting the server. Must be less than ".sizeof(servers)); + throw new \InvalidArgumentException("Invalid index $index when selecting the host. Must be less than ".sizeof($hosts)); } $host = $hosts[$index]; @@ -504,7 +504,7 @@ public function getHostFromSettings($index, $variables) if (in_array($variables[$name], $variable["enum_values"])) { // check to see if the value is in the enum $url = str_replace("{".$name."}", $variables[$name], $url); } else { - throw new \InvalidArgumentException("The variable `$name` in the server URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); + throw new \InvalidArgumentException("The variable `$name` in the host URL has invalid value ".$variables[$name].". Must be ".join(',', $variable["enum_values"])."."); } } else { // use default value diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php index ed8e4ce4823b..524019f1d1c9 100644 --- a/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php @@ -42,11 +42,11 @@ public function testServerUrl() } /** - * Test server settings with invalid vaues + * Test host settings with invalid vaues * @expectedException InvalidArgumentException - * @expectedExceptionMessage The variable `port` in the server URL has invalid value 8. Must be 80,8080 + * @expectedExceptionMessage The variable `port` in the host URL has invalid value 8. Must be 80,8080 */ - public function testServerUrlWithInvalidValues() + public function testHostUrlWithInvalidValues() { // using 2 variables with invalid values $config = new Configuration(); From 0c693945d30ff72b930926aab60bdee23d32dfcc Mon Sep 17 00:00:00 2001 From: William Cheng Date: Wed, 23 Jan 2019 23:02:13 +0800 Subject: [PATCH 4/4] make variables optional --- .../src/main/resources/php/Configuration.mustache | 4 ++-- .../petstore/php/OpenAPIClient-php/lib/Configuration.php | 4 ++-- .../petstore/php/OpenAPIClient-php/lib/Configuration.php | 4 ++-- .../php/OpenAPIClient-php/tests/ConfigurationTest.php | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/modules/openapi-generator/src/main/resources/php/Configuration.mustache b/modules/openapi-generator/src/main/resources/php/Configuration.mustache index bf0d4d8882e8..32de830191c5 100644 --- a/modules/openapi-generator/src/main/resources/php/Configuration.mustache +++ b/modules/openapi-generator/src/main/resources/php/Configuration.mustache @@ -462,10 +462,10 @@ class Configuration * Returns URL based on the index and variables * * @param index array index of the host settings - * @param variables hash of variable and the corresponding value + * @param variables hash of variable and the corresponding value (optional) * @return URL based on host settings */ - public function getHostFromSettings($index, $variables) + public function getHostFromSettings($index, $variables = null) { if (null === $variables) { $variables = array(); diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index 0c886905b11f..641f72e76636 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -446,10 +446,10 @@ public function getHostSettings() * Returns URL based on the index and variables * * @param index array index of the host settings - * @param variables hash of variable and the corresponding value + * @param variables hash of variable and the corresponding value (optional) * @return URL based on host settings */ - public function getHostFromSettings($index, $variables) + public function getHostFromSettings($index, $variables = null) { if (null === $variables) { $variables = array(); diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php index 9a370bd21f96..42a68f7a3e0c 100644 --- a/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/lib/Configuration.php @@ -479,10 +479,10 @@ public function getHostSettings() * Returns URL based on the index and variables * * @param index array index of the host settings - * @param variables hash of variable and the corresponding value + * @param variables hash of variable and the corresponding value (optional) * @return URL based on host settings */ - public function getHostFromSettings($index, $variables) + public function getHostFromSettings($index, $variables = null) { if (null === $variables) { $variables = array(); diff --git a/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php index 524019f1d1c9..983b871fd40a 100644 --- a/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php +++ b/samples/openapi3/client/petstore/php/OpenAPIClient-php/tests/ConfigurationTest.php @@ -29,7 +29,7 @@ public function testServerUrl() { $config = new Configuration(); // default value - $url = $config->getHostFromSettings(0, null); + $url = $config->getHostFromSettings(0); $this->assertSame("http://petstore.swagger.io:80/v2", $url); // using a variable