From 83d069d05341c9136221b340cf958a9ce54a8745 Mon Sep 17 00:00:00 2001 From: wing328 Date: Sun, 31 May 2015 09:49:24 +0800 Subject: [PATCH 1/4] add support for obj, remove null from serialized json string --- .../codegen/languages/PhpClientCodegen.java | 1 + .../src/main/resources/php/APIClient.mustache | 6 +++-- .../php/SwaggerClient-php/lib/APIClient.php | 4 +++- samples/client/petstore/php/test.php | 24 +++++++++++++++++++ 4 files changed, 32 insertions(+), 3 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java index 55d358154f8..9d29baffbe2 100644 --- a/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java +++ b/modules/swagger-codegen/src/main/java/com/wordnik/swagger/codegen/languages/PhpClientCodegen.java @@ -82,6 +82,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig { typeMapping.put("map", "map"); typeMapping.put("array", "array"); typeMapping.put("list", "array"); + typeMapping.put("object", "object"); supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json")); supportingFiles.add(new SupportingFile("configuration.mustache", packagePath + "/lib", "Configuration.php")); diff --git a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache index 70717056b9e..9d360b6482a 100644 --- a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache @@ -278,7 +278,9 @@ class APIClient { } else if (is_object($data)) { $values = array(); foreach (array_keys($data::$swaggerTypes) as $property) { - $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property); + if ($data->$property !== null) { + $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property); + } } $sanitized = $values; } else { @@ -383,7 +385,7 @@ class APIClient { $deserialized = $values; } elseif ($class == 'DateTime') { $deserialized = new \DateTime($data); - } elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool'))) { + } elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool', 'object'))) { settype($data, $class); $deserialized = $data; } else { diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php index f7002fba18d..44766040a7a 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php @@ -283,7 +283,9 @@ class APIClient { } else if (is_object($data)) { $values = array(); foreach (array_keys($data::$swaggerTypes) as $property) { - $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property); + if ($data->$property !== null) { + $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property); + } } $sanitized = $values; } else { diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index 41c206ee6b0..78f8b225665 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -8,13 +8,37 @@ require_once('SwaggerClient-php/SwaggerClient.php'); $petId = 10005; // ID of pet that needs to be fetched try { + // get pet by id //$pet_api = new SwaggerClient\PetAPI($api_client); $pet_api = new SwaggerClient\PetAPI(); // return Pet (model) $response = $pet_api->getPetById($petId); var_dump($response); + + // add pet (post json) + $new_pet_id = 10005; + $new_pet = new SwaggerClient\models\Pet; + $new_pet->id = $new_pet_id; + $new_pet->name = "PHP Unit Test"; + // new tag + $tag= new SwaggerClient\models\Tag; + $tag->id = $new_pet_id; // use the same id as pet + //$tag->name = "test php tag"; + // new category + $category = new SwaggerClient\models\Category; + $category->id = 0; // use the same id as pet + //$category->name = "test php category"; + + $new_pet->tags = array($tag); + $new_pet->category = $category; + + $pet_api = new SwaggerClient\PetAPI(); + // add a new pet (model) + $add_response = $pet_api->addPet($new_pet); + } catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } + ?> From 8d708c2442b7f1c3760cde84a85acfe4f2e35daf Mon Sep 17 00:00:00 2001 From: wing328 Date: Sun, 31 May 2015 11:17:59 +0800 Subject: [PATCH 2/4] add test case for default header --- .../src/main/resources/php/APIClient.mustache | 2 +- .../petstore/php/SwaggerClient-php/lib/APIClient.php | 11 ++++------- samples/client/petstore/php/test.php | 4 ++++ 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache index 9d360b6482a..aa85a2b1a37 100644 --- a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache @@ -182,7 +182,7 @@ class APIClient { $this->updateParamsForAuth($headerParams, $queryParams, $authSettings); # construct the http header - if ($headerParams != null) { + if ($headerParams !== null) { # add default header $headerParams = array_merge((array)self::$default_header, $headerParams); diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php index 44766040a7a..de75c7252e1 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/APIClient.php @@ -187,13 +187,10 @@ class APIClient { $this->updateParamsForAuth($headerParams, $queryParams, $authSettings); # construct the http header - if ($headerParams != null) { - # add default header - $headerParams = array_merge((array)self::$default_header, $headerParams); + $headerParams = array_merge((array)self::$default_header, (array)$headerParams); - foreach ($headerParams as $key => $val) { - $headers[] = "$key: $val"; - } + foreach ($headerParams as $key => $val) { + $headers[] = "$key: $val"; } // form data @@ -390,7 +387,7 @@ class APIClient { $deserialized = $values; } elseif ($class == 'DateTime') { $deserialized = new \DateTime($data); - } elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool'))) { + } elseif (in_array($class, array('string', 'int', 'float', 'double', 'bool', 'object'))) { settype($data, $class); $deserialized = $data; } else { diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index 78f8b225665..7bc755f72e6 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -2,6 +2,8 @@ //require_once('vendor/autoload.php'); require_once('SwaggerClient-php/SwaggerClient.php'); +$c = array_merge((array)$a, (array)$b); + // initialize the API client //$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); //$api_client->addDefaultHeader("test1", "value1"); @@ -11,6 +13,8 @@ try { // get pet by id //$pet_api = new SwaggerClient\PetAPI($api_client); $pet_api = new SwaggerClient\PetAPI(); + // test default header + $pet_api->getApiClient()->addDefaultHeader("TEST_API_KEY", "09182sdkanafndsl903"); // return Pet (model) $response = $pet_api->getPetById($petId); var_dump($response); From f357f4c9d75fae5ebf40729b9bfdb74e80733e9b Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 1 Jun 2015 14:24:09 +0800 Subject: [PATCH 3/4] fix default header --- .../src/main/resources/php/APIClient.mustache | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache index aa85a2b1a37..cbde8987da9 100644 --- a/modules/swagger-codegen/src/main/resources/php/APIClient.mustache +++ b/modules/swagger-codegen/src/main/resources/php/APIClient.mustache @@ -182,13 +182,10 @@ class APIClient { $this->updateParamsForAuth($headerParams, $queryParams, $authSettings); # construct the http header - if ($headerParams !== null) { - # add default header - $headerParams = array_merge((array)self::$default_header, $headerParams); + $headerParams = array_merge((array)self::$default_header, (array)$headerParams); - foreach ($headerParams as $key => $val) { - $headers[] = "$key: $val"; - } + foreach ($headerParams as $key => $val) { + $headers[] = "$key: $val"; } // form data From 762a3279ea68a6124073112b2ff5b840524e41de Mon Sep 17 00:00:00 2001 From: wing328 Date: Mon, 1 Jun 2015 14:50:57 +0800 Subject: [PATCH 4/4] clean up test.php --- samples/client/petstore/php/test.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/samples/client/petstore/php/test.php b/samples/client/petstore/php/test.php index 7bc755f72e6..de5f4ba3f12 100644 --- a/samples/client/petstore/php/test.php +++ b/samples/client/petstore/php/test.php @@ -2,8 +2,6 @@ //require_once('vendor/autoload.php'); require_once('SwaggerClient-php/SwaggerClient.php'); -$c = array_merge((array)$a, (array)$b); - // initialize the API client //$api_client = new SwaggerClient\APIClient('http://petstore.swagger.io/v2'); //$api_client->addDefaultHeader("test1", "value1");