Skip to content

[PHP] update deserializer to handle object type and add more test cases #2316

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 1 commit into from
Mar 5, 2016
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
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ class ObjectSerializer
$values[] = self::deserialize($value, $subClass);
}
$deserialized = $values;
} elseif ($class === 'object') {
settype($data, 'array');
$deserialized = $data;
} elseif ($class === '\DateTime') {
$deserialized = new \DateTime($data);
} elseif (in_array($class, array({{&primitives}}))) {
Expand Down
29 changes: 29 additions & 0 deletions modules/swagger-codegen/src/test/resources/2_0/petstore.json
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,35 @@
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"xml": {
"name": "photoUrl",
"wrapped": true
},
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"xml": {
"name": "tag",
"wrapped": true
},
"items": {
"$ref": "#/definitions/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": [
"available",
"pending",
"sold"
]
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,42 +51,60 @@ class InlineResponse200 implements ArrayAccess
* @var string[]
*/
static $swaggerTypes = array(
'tags' => '\Swagger\Client\Model\Tag[]',
'id' => 'int',
'category' => 'object',
'name' => 'string'
'status' => 'string',
'name' => 'string',
'photo_urls' => 'string[]'
);

/**
* Array of attributes where the key is the local name, and the value is the original name
* @var string[]
*/
static $attributeMap = array(
'tags' => 'tags',
'id' => 'id',
'category' => 'category',
'name' => 'name'
'status' => 'status',
'name' => 'name',
'photo_urls' => 'photoUrls'
);

/**
* Array of attributes to setter functions (for deserialization of responses)
* @var string[]
*/
static $setters = array(
'tags' => 'setTags',
'id' => 'setId',
'category' => 'setCategory',
'name' => 'setName'
'status' => 'setStatus',
'name' => 'setName',
'photo_urls' => 'setPhotoUrls'
);

/**
* Array of attributes to getter functions (for serialization of requests)
* @var string[]
*/
static $getters = array(
'tags' => 'getTags',
'id' => 'getId',
'category' => 'getCategory',
'name' => 'getName'
'status' => 'getStatus',
'name' => 'getName',
'photo_urls' => 'getPhotoUrls'
);


/**
* $tags
* @var \Swagger\Client\Model\Tag[]
*/
protected $tags;

/**
* $id
* @var int
Expand All @@ -99,12 +117,24 @@ class InlineResponse200 implements ArrayAccess
*/
protected $category;

/**
* $status pet status in the store
* @var string
*/
protected $status;

/**
* $name
* @var string
*/
protected $name;

/**
* $photo_urls
* @var string[]
*/
protected $photo_urls;


/**
* Constructor
Expand All @@ -113,12 +143,36 @@ class InlineResponse200 implements ArrayAccess
public function __construct(array $data = null)
{
if ($data != null) {
$this->tags = $data["tags"];
$this->id = $data["id"];
$this->category = $data["category"];
$this->status = $data["status"];
$this->name = $data["name"];
$this->photo_urls = $data["photo_urls"];
}
}

/**
* Gets tags
* @return \Swagger\Client\Model\Tag[]
*/
public function getTags()
{
return $this->tags;
}

/**
* Sets tags
* @param \Swagger\Client\Model\Tag[] $tags
* @return $this
*/
public function setTags($tags)
{

$this->tags = $tags;
return $this;
}

/**
* Gets id
* @return int
Expand Down Expand Up @@ -161,6 +215,30 @@ public function setCategory($category)
return $this;
}

/**
* Gets status
* @return string
*/
public function getStatus()
{
return $this->status;
}

/**
* Sets status
* @param string $status pet status in the store
* @return $this
*/
public function setStatus($status)
{
$allowed_values = array("available", "pending", "sold");
if (!in_array($status, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for 'status', must be one of 'available', 'pending', 'sold'");
}
$this->status = $status;
return $this;
}

/**
* Gets name
* @return string
Expand All @@ -182,6 +260,27 @@ public function setName($name)
return $this;
}

/**
* Gets photo_urls
* @return string[]
*/
public function getPhotoUrls()
{
return $this->photo_urls;
}

/**
* Sets photo_urls
* @param string[] $photo_urls
* @return $this
*/
public function setPhotoUrls($photo_urls)
{

$this->photo_urls = $photo_urls;
return $this;
}

/**
* Returns true if offset exists. False otherwise.
* @param integer $offset Offset
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,9 @@ public static function deserialize($data, $class, $httpHeaders=null)
$values[] = self::deserialize($value, $subClass);
}
$deserialized = $values;
} elseif ($class === 'object') {
settype($data, 'array');
$deserialized = $data;
} elseif ($class === '\DateTime') {
$deserialized = new \DateTime($data);
} elseif (in_array($class, array('integer', 'int', 'void', 'number', 'object', 'double', 'float', 'byte', 'DateTime', 'string', 'mixed', 'boolean', 'bool'))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,31 @@ public function testGetPetById()
$this->assertSame($response->getTags()[0]->getId(), $pet_id);
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
}

// test getPetById with a Pet object (id 10005)
public function testGetPetByIdInObject()
{
// initialize the API client without host
$pet_id = 10005; // ID of pet that needs to be fetched
$pet_api = new Swagger\Client\Api\PetAPI();
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
// return Pet (inline model)
$response = $pet_api->getPetByIdInObject($pet_id);
$this->assertInstanceOf('Swagger\Client\Model\InlineResponse200', $response);
$this->assertSame($response->getId(), $pet_id);
$this->assertSame($response->getName(), 'PHP Unit Test');
$this->assertSame($response->getPhotoUrls()[0], 'http://test_php_unit_test.com');

// category is type "object"
$this->assertInternalType('array', $response->getCategory());
$this->assertSame($response->getCategory()['id'], $pet_id);
$this->assertSame($response->getCategory()['name'], 'test php category');

$this->assertSame($response->getTags()[0]->getId(), $pet_id);
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
}

// test getPetByIdWithHttpInfo with a Pet object (id 10005)
public function testGetPetByIdWithHttpInfo()
{
// initialize the API client without host
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,61 @@
class StoreApiTest extends \PHPUnit_Framework_TestCase
{

// add a new pet (id 10005) to ensure the pet object is available for all the tests
public static function setUpBeforeClass() {
// for error reporting (need to run with php5.3 to get no warning)
//ini_set('display_errors', 1);
//error_reporting(~0);
// new pet
$new_pet_id = 10005;
$new_pet = new Swagger\Client\Model\Pet;
$new_pet->setId($new_pet_id);
$new_pet->setName("PHP Unit Test");
$new_pet->setStatus("available");
// new tag
$tag= new Swagger\Client\Model\Tag;
$tag->setId($new_pet_id); // use the same id as pet
$tag->setName("test php tag");
// new category
$category = new Swagger\Client\Model\Category;
$category->setId($new_pet_id); // use the same id as pet
$category->setName("test php category");

$new_pet->setTags(array($tag));
$new_pet->setCategory($category);

$pet_api = new Swagger\Client\Api\PetAPI();
// add a new pet (model)
$add_response = $pet_api->addPet($new_pet);
}

// test get inventory
public function testGetInventory()
{
// initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config);
$store_api = new Swagger\Client\Api\StoreAPI($api_client);
// get inventory
$get_response = $store_api->getInventory();

$this->assertInternalType("int", $get_response['available']);

}
// add a new pet (id 10005) to ensure the pet object is available for all the tests
public static function setUpBeforeClass() {
// for error reporting (need to run with php5.3 to get no warning)
//ini_set('display_errors', 1);
//error_reporting(~0);
// new pet
$new_pet_id = 10005;
$new_pet = new Swagger\Client\Model\Pet;
$new_pet->setId($new_pet_id);
$new_pet->setName("PHP Unit Test");
$new_pet->setStatus("available");
// new tag
$tag= new Swagger\Client\Model\Tag;
$tag->setId($new_pet_id); // use the same id as pet
$tag->setName("test php tag");
// new category
$category = new Swagger\Client\Model\Category;
$category->setId($new_pet_id); // use the same id as pet
$category->setName("test php category");

$new_pet->setTags(array($tag));
$new_pet->setCategory($category);

$pet_api = new Swagger\Client\Api\PetAPI();
// add a new pet (model)
$add_response = $pet_api->addPet($new_pet);
}

// test get inventory
public function testGetInventory()
{
// initialize the API client
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient($config);
$store_api = new Swagger\Client\Api\StoreAPI($api_client);
// get inventory
$get_response = $store_api->getInventory();

$this->assertInternalType("array", $get_response);
$this->assertInternalType("int", $get_response['available']);
}

// test get inventory
public function testGetInventoryInObject()
{
// initialize the API client
//$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
$api_client = new Swagger\Client\ApiClient();
$store_api = new Swagger\Client\Api\StoreAPI($api_client);
// get inventory
$get_response = $store_api->getInventoryInObject();

$this->assertInternalType("array", $get_response);
$this->assertInternalType("int", $get_response['available']);
}

}

Expand Down