Skip to content

Commit 2ec18a1

Browse files
committed
Merge branch 'wing328-php_object_support' into develop_2.0
2 parents 26d9215 + 34072fa commit 2ec18a1

File tree

4 files changed

+41
-17
lines changed

4 files changed

+41
-17
lines changed

modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public PhpClientCodegen() {
8282
typeMapping.put("map", "map");
8383
typeMapping.put("array", "array");
8484
typeMapping.put("list", "array");
85+
typeMapping.put("object", "object");
8586

8687
supportingFiles.add(new SupportingFile("composer.mustache", packagePath.replace('/', File.separatorChar), "composer.json"));
8788
supportingFiles.add(new SupportingFile("configuration.mustache", (packagePath + "/lib").replace('/', File.separatorChar), "Configuration.php"));

modules/swagger-codegen/src/main/resources/php/APIClient.mustache

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -182,13 +182,10 @@ class ApiClient {
182182
$this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
183183
184184
# construct the http header
185-
if ($headerParams != null) {
186-
# add default header
187-
$headerParams = array_merge((array)self::$default_header, $headerParams);
185+
$headerParams = array_merge((array)self::$default_header, (array)$headerParams);
188186
189-
foreach ($headerParams as $key => $val) {
190-
$headers[] = "$key: $val";
191-
}
187+
foreach ($headerParams as $key => $val) {
188+
$headers[] = "$key: $val";
192189
}
193190

194191
// form data
@@ -292,7 +289,9 @@ class ApiClient {
292289
} else if (is_object($data)) {
293290
$values = array();
294291
foreach (array_keys($data::$swaggerTypes) as $property) {
295-
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
292+
if ($data->$property !== null) {
293+
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
294+
}
296295
}
297296
$sanitized = $values;
298297
} else {
@@ -397,7 +396,7 @@ class ApiClient {
397396
$deserialized = $values;
398397
} elseif ($class == 'DateTime') {
399398
$deserialized = new \DateTime($data);
400-
} elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool'))) {
399+
} elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool', 'object'))) {
401400
settype($data, $class);
402401
$deserialized = $data;
403402
} else {

samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,10 @@ public function callApi($resourcePath, $method, $queryParams, $postData,
187187
$this->updateParamsForAuth($headerParams, $queryParams, $authSettings);
188188

189189
# construct the http header
190-
if ($headerParams != null) {
191-
# add default header
192-
$headerParams = array_merge((array)self::$default_header, $headerParams);
190+
$headerParams = array_merge((array)self::$default_header, (array)$headerParams);
193191

194-
foreach ($headerParams as $key => $val) {
195-
$headers[] = "$key: $val";
196-
}
192+
foreach ($headerParams as $key => $val) {
193+
$headers[] = "$key: $val";
197194
}
198195

199196
// form data
@@ -297,7 +294,9 @@ protected function sanitizeForSerialization($data)
297294
} else if (is_object($data)) {
298295
$values = array();
299296
foreach (array_keys($data::$swaggerTypes) as $property) {
300-
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
297+
if ($data->$property !== null) {
298+
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
299+
}
301300
}
302301
$sanitized = $values;
303302
} else {
@@ -402,7 +401,7 @@ public static function deserialize($data, $class)
402401
$deserialized = $values;
403402
} elseif ($class == 'DateTime') {
404403
$deserialized = new \DateTime($data);
405-
} elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool'))) {
404+
} elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool', 'object'))) {
406405
settype($data, $class);
407406
$deserialized = $data;
408407
} else {

samples/client/petstore/php/test.php

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,45 @@
1212

1313
$petId = 10005; // ID of pet that needs to be fetched
1414
try {
15+
// get pet by id
1516
//$pet_api = new SwaggerClient\PetAPI($api_client);
1617
$pet_api = new SwaggerClient\PetAPI();
18+
// test default header
19+
$pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903");
1720
// return Pet (model)
1821
$response = $pet_api->getPetById($petId);
1922
var_dump($response);
2023

2124
// test upload file (exception)
2225
$upload_response = $pet_api->uploadFile($petId, "test meta", NULL);
2326

24-
} catch (SwaggerClient\ApiException $e) {
27+
// add pet (post json)
28+
$new_pet_id = 10005;
29+
$new_pet = new SwaggerClient\models\Pet;
30+
$new_pet->id = $new_pet_id;
31+
$new_pet->name = "PHP Unit Test";
32+
// new tag
33+
$tag= new SwaggerClient\models\Tag;
34+
$tag->id = $new_pet_id; // use the same id as pet
35+
//$tag->name = "test php tag";
36+
// new category
37+
$category = new SwaggerClient\models\Category;
38+
$category->id = 0; // use the same id as pet
39+
//$category->name = "test php category";
40+
41+
$new_pet->tags = array($tag);
42+
$new_pet->category = $category;
43+
44+
$pet_api = new SwaggerClient\PetAPI();
45+
// add a new pet (model)
46+
$add_response = $pet_api->addPet($new_pet);
47+
48+
} catch (Exception $e) {
2549
echo 'Caught exception: ', $e->getMessage(), "\n";
2650
echo 'HTTP response headers: ', $e->getResponseHeaders(), "\n";
2751
echo 'HTTP response body: ', $e->getResponseBody(), "\n";
2852
echo 'HTTP status code: ', $e->getCode(), "\n";
2953
}
3054

55+
3156
?>

0 commit comments

Comments
 (0)