Skip to content

Bug fix for PHP client POST (model) and add more PHP test cases #659

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 16, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,6 @@ target
.lib
atlassian-ide-plugin.xml
.DS_Store

samples/client/petstore/php/SwaggerClient-php/composer.lock
samples/client/petstore/php/SwaggerClient-php/vendor/
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ class APIClient {
$headers[] = $this->headerName . ": " . $this->headerValue;
}

if ((isset($headerName['Content-Type']) and strpos($headerName['Content-Type'], "multipart/form-data") === FALSE) and (is_object($postData) or is_array($postData))) {
// form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {
$postData = http_build_query($postData);
}
else if ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) { // json model
$postData = json_encode($this->sanitizeForSerialization($postData));
}

Expand Down
8 changes: 3 additions & 5 deletions modules/swagger-codegen/src/main/resources/php/api.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,9 @@ class {{classname}} {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
// source code generated by http://restunited.com
// for any feedback/issue, please send to feedback{at}restunited.com

// load models defined for endpoints
foreach (glob(dirname(__FILE__)."/lib/models/*.php") as $filename)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
<?php
// source code generated by http://restunited.com
// for any feedback/issue, please send to feedback{at}restunited.com

// load models defined for endpoints
foreach (glob(dirname(__FILE__)."/lib/models/*.php") as $filename)
{
Expand Down
17 changes: 10 additions & 7 deletions samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,11 @@ public function callAPI($resourcePath, $method, $queryParams, $postData,
$headers[] = $this->headerName . ": " . $this->headerValue;
}

if ((isset($headerName['Content-Type']) and strpos($headerName['Content-Type'], "multipart/form-data") === FALSE) and (is_object($postData) or is_array($postData))) {
// form data
if ($postData and in_array('Content-Type: application/x-www-form-urlencoded', $headers)) {
$postData = http_build_query($postData);
}
else if ((is_object($postData) or is_array($postData)) and !in_array('Content-Type: multipart/form-data', $headers)) { // json model
$postData = json_encode($this->sanitizeForSerialization($postData));
}

Expand Down Expand Up @@ -138,21 +142,20 @@ public function callAPI($resourcePath, $method, $queryParams, $postData,
$response_info = curl_getinfo($curl);

// Handle the response
if ($response_info['http_code'] == 0) {
throw new APIClientException("TIMEOUT: api call to " . $url .
" took more than 5s to return", 0, $response_info, $response);
if ($response === false) { // error, likely in the client side
throw new APIClientException("API Error ($url): ".curl_error($curl), 0, $response_info, $response);
} else if ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
$data = json_decode($response);
if (json_last_error() > 0) { // if response is a string
$data = $response;
}
} else if ($response_info['http_code'] == 401) {
} else if ($response_info['http_code'] == 401) { // server returns 401
throw new APIClientException("Unauthorized API request to " . $url .
": " . serialize($response), 0, $response_info, $response);
} else if ($response_info['http_code'] == 404) {
} else if ($response_info['http_code'] == 404) { // server returns 404
$data = null;
} else {
throw new APIClientException("Can't connect to the api: " . $url .
throw new APIClientException("Can't connect to the API: " . $url .
" response code: " .
$response_info['http_code'], 0, $response_info, $response);
}
Expand Down
64 changes: 24 additions & 40 deletions samples/client/petstore/php/SwaggerClient-php/lib/PetApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ public function updatePet($body) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -120,11 +118,9 @@ public function addPet($body) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -172,11 +168,9 @@ public function findPetsByStatus($status) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -230,11 +224,9 @@ public function findPetsByTags($tags) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -289,11 +281,9 @@ public function getPetById($pet_id) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -356,11 +346,9 @@ public function updatePetWithForm($pet_id, $name, $status) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -413,11 +401,9 @@ public function deletePet($api_key, $pet_id) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -474,11 +460,9 @@ public function uploadFile($pet_id, $additional_metadata, $file) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down
32 changes: 12 additions & 20 deletions samples/client/petstore/php/SwaggerClient-php/lib/StoreApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,9 @@ public function getInventory() {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -121,11 +119,9 @@ public function placeOrder($body) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -180,11 +176,9 @@ public function getOrderById($order_id) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -239,11 +233,9 @@ public function deleteOrder($order_id) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down
64 changes: 24 additions & 40 deletions samples/client/petstore/php/SwaggerClient-php/lib/UserApi.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,9 @@ public function createUser($body) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -120,11 +118,9 @@ public function createUsersWithArrayInput($body) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -173,11 +169,9 @@ public function createUsersWithListInput($body) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -229,11 +223,9 @@ public function loginUser($username, $password) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -283,11 +275,9 @@ public function logoutUser() {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -336,11 +326,9 @@ public function getUserByName($username) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -400,11 +388,9 @@ public function updateUser($username, $body) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down Expand Up @@ -453,11 +439,9 @@ public function deleteUser($username) {
// for model (json/xml)
if (isset($_tempBody)) {
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
}

// for HTTP post (form)
if (strpos($headerParams['Content-Type'], "application/x-www-form-urlencoded") !== FALSE) {
$httpBody = http_build_query($formParams);
} else if (count($formParams) > 0) {
// for HTTP post (form)
$httpBody = $formParams;
}

// make the API Call
Expand Down
Loading