add support for obj, remove null from serialized json string

This commit is contained in:
wing328 2015-05-31 09:49:24 +08:00
parent 7d7ed15fdc
commit 83d069d053
4 changed files with 32 additions and 3 deletions

View File

@ -82,6 +82,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
typeMapping.put("map", "map"); typeMapping.put("map", "map");
typeMapping.put("array", "array"); typeMapping.put("array", "array");
typeMapping.put("list", "array"); typeMapping.put("list", "array");
typeMapping.put("object", "object");
supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json")); supportingFiles.add(new SupportingFile("composer.mustache", packagePath, "composer.json"));
supportingFiles.add(new SupportingFile("configuration.mustache", packagePath + "/lib", "Configuration.php")); supportingFiles.add(new SupportingFile("configuration.mustache", packagePath + "/lib", "Configuration.php"));

View File

@ -278,8 +278,10 @@ class APIClient {
} else if (is_object($data)) { } else if (is_object($data)) {
$values = array(); $values = array();
foreach (array_keys($data::$swaggerTypes) as $property) { foreach (array_keys($data::$swaggerTypes) as $property) {
if ($data->$property !== null) {
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property); $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
} }
}
$sanitized = $values; $sanitized = $values;
} else { } else {
$sanitized = (string)$data; $sanitized = (string)$data;
@ -383,7 +385,7 @@ class APIClient {
$deserialized = $values; $deserialized = $values;
} elseif ($class == 'DateTime') { } elseif ($class == 'DateTime') {
$deserialized = new \DateTime($data); $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); settype($data, $class);
$deserialized = $data; $deserialized = $data;
} else { } else {

View File

@ -283,8 +283,10 @@ class APIClient {
} else if (is_object($data)) { } else if (is_object($data)) {
$values = array(); $values = array();
foreach (array_keys($data::$swaggerTypes) as $property) { foreach (array_keys($data::$swaggerTypes) as $property) {
if ($data->$property !== null) {
$values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property); $values[$data::$attributeMap[$property]] = $this->sanitizeForSerialization($data->$property);
} }
}
$sanitized = $values; $sanitized = $values;
} else { } else {
$sanitized = (string)$data; $sanitized = (string)$data;

View File

@ -8,13 +8,37 @@ require_once('SwaggerClient-php/SwaggerClient.php');
$petId = 10005; // ID of pet that needs to be fetched $petId = 10005; // ID of pet that needs to be fetched
try { try {
// get pet by id
//$pet_api = new SwaggerClient\PetAPI($api_client); //$pet_api = new SwaggerClient\PetAPI($api_client);
$pet_api = new SwaggerClient\PetAPI(); $pet_api = new SwaggerClient\PetAPI();
// return Pet (model) // return Pet (model)
$response = $pet_api->getPetById($petId); $response = $pet_api->getPetById($petId);
var_dump($response); 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) { } catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n"; echo 'Caught exception: ', $e->getMessage(), "\n";
} }
?> ?>