From e11a3d468e59749101e6cb0df71217869a40448d Mon Sep 17 00:00:00 2001 From: wing328 Date: Sun, 22 Nov 2015 18:35:45 +0800 Subject: [PATCH] update auth for php to skip empty apikey/username,password --- .../src/main/resources/php/api.mustache | 12 +++-- .../php/SwaggerClient-php/lib/Api/PetApi.php | 46 ++++++++++++------- .../SwaggerClient-php/lib/Api/StoreApi.php | 4 +- .../php/SwaggerClient-php/lib/ApiClient.php | 2 +- .../lib/ObjectSerializer.php | 2 +- 5 files changed, 43 insertions(+), 23 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index e3a9749c423..53a81030813 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -166,12 +166,18 @@ use \{{invokerPackage}}\ObjectSerializer; $httpBody = $formParams; // for HTTP post (form) } {{#authMethods}}{{#isApiKey}} + // this endpoint requires API key authentication $apiKey = $this->apiClient->getApiKeyWithPrefix('{{keyParamName}}'); - if (isset($apiKey)) { + if ($apiKey !== null) { {{#isKeyInHeader}}$headerParams['{{keyParamName}}'] = $apiKey;{{/isKeyInHeader}}{{#isKeyInQuery}}$queryParams['{{keyParamName}}'] = $apiKey;{{/isKeyInQuery}} }{{/isApiKey}} - {{#isBasic}}$headerParams['Authorization'] = 'Basic ' . base64_encode($this->apiClient->getConfig()->getUsername() . ":" . $this->apiClient->getConfig()->getPassword());{{/isBasic}} - {{#isOAuth}}$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();{{/isOAuth}} + {{#isBasic}}// this endpoint requires HTTP basic authentication + if ($this->apiClient->getConfig()->getUsername() !== null or $this->apiClient->getConfig()->getPassword() !== null) { + $headerParams['Authorization'] = 'Basic ' . base64_encode($this->apiClient->getConfig()->getUsername() . ":" . $this->apiClient->getConfig()->getPassword()); + }{{/isBasic}}{{#isOAuth}}// this endpoint requires OAuth (access token) + if ($this->apiClient->getConfig()->getAccessToken() !== null) { + $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + }{{/isOAuth}} {{/authMethods}} // make the API Call try diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php index a418ac90b43..bc03a7974d3 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php @@ -135,8 +135,10 @@ class PetApi $httpBody = $formParams; // for HTTP post (form) } - - $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + // this endpoint requires OAuth (access token) + if ($this->apiClient->getConfig()->getAccessToken() !== null) { + $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + } // make the API Call try @@ -200,8 +202,10 @@ class PetApi $httpBody = $formParams; // for HTTP post (form) } - - $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + // this endpoint requires OAuth (access token) + if ($this->apiClient->getConfig()->getAccessToken() !== null) { + $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + } // make the API Call try @@ -264,8 +268,10 @@ class PetApi $httpBody = $formParams; // for HTTP post (form) } - - $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + // this endpoint requires OAuth (access token) + if ($this->apiClient->getConfig()->getAccessToken() !== null) { + $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + } // make the API Call try @@ -340,8 +346,10 @@ class PetApi $httpBody = $formParams; // for HTTP post (form) } - - $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + // this endpoint requires OAuth (access token) + if ($this->apiClient->getConfig()->getAccessToken() !== null) { + $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + } // make the API Call try @@ -424,13 +432,13 @@ class PetApi $httpBody = $formParams; // for HTTP post (form) } + // this endpoint requires API key authentication $apiKey = $this->apiClient->getApiKeyWithPrefix('api_key'); - if (isset($apiKey)) { + if ($apiKey !== null) { $headerParams['api_key'] = $apiKey; } - // make the API Call try { @@ -526,8 +534,10 @@ class PetApi $httpBody = $formParams; // for HTTP post (form) } - - $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + // this endpoint requires OAuth (access token) + if ($this->apiClient->getConfig()->getAccessToken() !== null) { + $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + } // make the API Call try @@ -602,8 +612,10 @@ class PetApi $httpBody = $formParams; // for HTTP post (form) } - - $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + // this endpoint requires OAuth (access token) + if ($this->apiClient->getConfig()->getAccessToken() !== null) { + $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + } // make the API Call try @@ -694,8 +706,10 @@ class PetApi $httpBody = $formParams; // for HTTP post (form) } - - $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + // this endpoint requires OAuth (access token) + if ($this->apiClient->getConfig()->getAccessToken() !== null) { + $headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken(); + } // make the API Call try diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php index b64b52d2225..d709330b056 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/StoreApi.php @@ -130,13 +130,13 @@ class StoreApi $httpBody = $formParams; // for HTTP post (form) } + // this endpoint requires API key authentication $apiKey = $this->apiClient->getApiKeyWithPrefix('api_key'); - if (isset($apiKey)) { + if ($apiKey !== null) { $headerParams['api_key'] = $apiKey; } - // make the API Call try { diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php b/samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php index 982b8955a15..a0bb07273e4 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/ApiClient.php @@ -69,7 +69,7 @@ class ApiClient * Constructor of the class * @param Configuration $config config for this ApiClient */ - function __construct(Configuration $config = null) + public function __construct(Configuration $config = null) { if ($config == null) { $config = Configuration::getDefaultConfiguration(); diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php index efdc1c896ab..0d281b9d1fa 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/ObjectSerializer.php @@ -193,7 +193,7 @@ class ObjectSerializer $deserialized = $values; } elseif ($class === '\DateTime') { $deserialized = new \DateTime($data); - } elseif (in_array($class, array('void', 'bool', 'string', 'double', 'byte', 'mixed', 'integer', 'float', 'int', 'DateTime', 'number', 'boolean', 'object'))) { + } elseif (in_array($class, array('integer', 'int', 'void', 'number', 'object', 'double', 'float', 'byte', 'DateTime', 'string', 'mixed', 'boolean', 'bool'))) { settype($data, $class); $deserialized = $data; } elseif ($class === '\SplFileObject') {