Skip to content

Commit 123bf19

Browse files
committed
Merge pull request #2316 from wing328/php_add_test_for_object
[PHP] update deserializer to handle object type and add more test cases
2 parents fd8ec21 + b5c0f17 commit 123bf19

File tree

6 files changed

+217
-46
lines changed

6 files changed

+217
-46
lines changed

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ class ObjectSerializer
241241
$values[] = self::deserialize($value, $subClass);
242242
}
243243
$deserialized = $values;
244+
} elseif ($class === 'object') {
245+
settype($data, 'array');
246+
$deserialized = $data;
244247
} elseif ($class === '\DateTime') {
245248
$deserialized = new \DateTime($data);
246249
} elseif (in_array($class, array({{&primitives}}))) {

modules/swagger-codegen/src/test/resources/2_0/petstore.json

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,35 @@
353353
"name": {
354354
"type": "string",
355355
"example": "doggie"
356+
},
357+
"photoUrls": {
358+
"type": "array",
359+
"xml": {
360+
"name": "photoUrl",
361+
"wrapped": true
362+
},
363+
"items": {
364+
"type": "string"
365+
}
366+
},
367+
"tags": {
368+
"type": "array",
369+
"xml": {
370+
"name": "tag",
371+
"wrapped": true
372+
},
373+
"items": {
374+
"$ref": "#/definitions/Tag"
375+
}
376+
},
377+
"status": {
378+
"type": "string",
379+
"description": "pet status in the store",
380+
"enum": [
381+
"available",
382+
"pending",
383+
"sold"
384+
]
356385
}
357386
}
358387
}

samples/client/petstore/php/SwaggerClient-php/lib/Model/InlineResponse200.php

Lines changed: 103 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,42 +51,60 @@ class InlineResponse200 implements ArrayAccess
5151
* @var string[]
5252
*/
5353
static $swaggerTypes = array(
54+
'tags' => '\Swagger\Client\Model\Tag[]',
5455
'id' => 'int',
5556
'category' => 'object',
56-
'name' => 'string'
57+
'status' => 'string',
58+
'name' => 'string',
59+
'photo_urls' => 'string[]'
5760
);
5861

5962
/**
6063
* Array of attributes where the key is the local name, and the value is the original name
6164
* @var string[]
6265
*/
6366
static $attributeMap = array(
67+
'tags' => 'tags',
6468
'id' => 'id',
6569
'category' => 'category',
66-
'name' => 'name'
70+
'status' => 'status',
71+
'name' => 'name',
72+
'photo_urls' => 'photoUrls'
6773
);
6874

6975
/**
7076
* Array of attributes to setter functions (for deserialization of responses)
7177
* @var string[]
7278
*/
7379
static $setters = array(
80+
'tags' => 'setTags',
7481
'id' => 'setId',
7582
'category' => 'setCategory',
76-
'name' => 'setName'
83+
'status' => 'setStatus',
84+
'name' => 'setName',
85+
'photo_urls' => 'setPhotoUrls'
7786
);
7887

7988
/**
8089
* Array of attributes to getter functions (for serialization of requests)
8190
* @var string[]
8291
*/
8392
static $getters = array(
93+
'tags' => 'getTags',
8494
'id' => 'getId',
8595
'category' => 'getCategory',
86-
'name' => 'getName'
96+
'status' => 'getStatus',
97+
'name' => 'getName',
98+
'photo_urls' => 'getPhotoUrls'
8799
);
88100

89101

102+
/**
103+
* $tags
104+
* @var \Swagger\Client\Model\Tag[]
105+
*/
106+
protected $tags;
107+
90108
/**
91109
* $id
92110
* @var int
@@ -99,12 +117,24 @@ class InlineResponse200 implements ArrayAccess
99117
*/
100118
protected $category;
101119

120+
/**
121+
* $status pet status in the store
122+
* @var string
123+
*/
124+
protected $status;
125+
102126
/**
103127
* $name
104128
* @var string
105129
*/
106130
protected $name;
107131

132+
/**
133+
* $photo_urls
134+
* @var string[]
135+
*/
136+
protected $photo_urls;
137+
108138

109139
/**
110140
* Constructor
@@ -113,12 +143,36 @@ class InlineResponse200 implements ArrayAccess
113143
public function __construct(array $data = null)
114144
{
115145
if ($data != null) {
146+
$this->tags = $data["tags"];
116147
$this->id = $data["id"];
117148
$this->category = $data["category"];
149+
$this->status = $data["status"];
118150
$this->name = $data["name"];
151+
$this->photo_urls = $data["photo_urls"];
119152
}
120153
}
121154

155+
/**
156+
* Gets tags
157+
* @return \Swagger\Client\Model\Tag[]
158+
*/
159+
public function getTags()
160+
{
161+
return $this->tags;
162+
}
163+
164+
/**
165+
* Sets tags
166+
* @param \Swagger\Client\Model\Tag[] $tags
167+
* @return $this
168+
*/
169+
public function setTags($tags)
170+
{
171+
172+
$this->tags = $tags;
173+
return $this;
174+
}
175+
122176
/**
123177
* Gets id
124178
* @return int
@@ -161,6 +215,30 @@ public function setCategory($category)
161215
return $this;
162216
}
163217

218+
/**
219+
* Gets status
220+
* @return string
221+
*/
222+
public function getStatus()
223+
{
224+
return $this->status;
225+
}
226+
227+
/**
228+
* Sets status
229+
* @param string $status pet status in the store
230+
* @return $this
231+
*/
232+
public function setStatus($status)
233+
{
234+
$allowed_values = array("available", "pending", "sold");
235+
if (!in_array($status, $allowed_values)) {
236+
throw new \InvalidArgumentException("Invalid value for 'status', must be one of 'available', 'pending', 'sold'");
237+
}
238+
$this->status = $status;
239+
return $this;
240+
}
241+
164242
/**
165243
* Gets name
166244
* @return string
@@ -182,6 +260,27 @@ public function setName($name)
182260
return $this;
183261
}
184262

263+
/**
264+
* Gets photo_urls
265+
* @return string[]
266+
*/
267+
public function getPhotoUrls()
268+
{
269+
return $this->photo_urls;
270+
}
271+
272+
/**
273+
* Sets photo_urls
274+
* @param string[] $photo_urls
275+
* @return $this
276+
*/
277+
public function setPhotoUrls($photo_urls)
278+
{
279+
280+
$this->photo_urls = $photo_urls;
281+
return $this;
282+
}
283+
185284
/**
186285
* Returns true if offset exists. False otherwise.
187286
* @param integer $offset Offset

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ public static function deserialize($data, $class, $httpHeaders=null)
241241
$values[] = self::deserialize($value, $subClass);
242242
}
243243
$deserialized = $values;
244+
} elseif ($class === 'object') {
245+
settype($data, 'array');
246+
$deserialized = $data;
244247
} elseif ($class === '\DateTime') {
245248
$deserialized = new \DateTime($data);
246249
} elseif (in_array($class, array('integer', 'int', 'void', 'number', 'object', 'double', 'float', 'byte', 'DateTime', 'string', 'mixed', 'boolean', 'bool'))) {

samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,31 @@ public function testGetPetById()
110110
$this->assertSame($response->getTags()[0]->getId(), $pet_id);
111111
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
112112
}
113-
113+
114114
// test getPetById with a Pet object (id 10005)
115+
public function testGetPetByIdInObject()
116+
{
117+
// initialize the API client without host
118+
$pet_id = 10005; // ID of pet that needs to be fetched
119+
$pet_api = new Swagger\Client\Api\PetAPI();
120+
$pet_api->getApiClient()->getConfig()->setApiKey('api_key', '111222333444555');
121+
// return Pet (inline model)
122+
$response = $pet_api->getPetByIdInObject($pet_id);
123+
$this->assertInstanceOf('Swagger\Client\Model\InlineResponse200', $response);
124+
$this->assertSame($response->getId(), $pet_id);
125+
$this->assertSame($response->getName(), 'PHP Unit Test');
126+
$this->assertSame($response->getPhotoUrls()[0], 'http://test_php_unit_test.com');
127+
128+
// category is type "object"
129+
$this->assertInternalType('array', $response->getCategory());
130+
$this->assertSame($response->getCategory()['id'], $pet_id);
131+
$this->assertSame($response->getCategory()['name'], 'test php category');
132+
133+
$this->assertSame($response->getTags()[0]->getId(), $pet_id);
134+
$this->assertSame($response->getTags()[0]->getName(), 'test php tag');
135+
}
136+
137+
// test getPetByIdWithHttpInfo with a Pet object (id 10005)
115138
public function testGetPetByIdWithHttpInfo()
116139
{
117140
// initialize the API client without host

samples/client/petstore/php/SwaggerClient-php/tests/StoreApiTest.php

Lines changed: 55 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,61 @@
55
class StoreApiTest extends \PHPUnit_Framework_TestCase
66
{
77

8-
// add a new pet (id 10005) to ensure the pet object is available for all the tests
9-
public static function setUpBeforeClass() {
10-
// for error reporting (need to run with php5.3 to get no warning)
11-
//ini_set('display_errors', 1);
12-
//error_reporting(~0);
13-
// new pet
14-
$new_pet_id = 10005;
15-
$new_pet = new Swagger\Client\Model\Pet;
16-
$new_pet->setId($new_pet_id);
17-
$new_pet->setName("PHP Unit Test");
18-
$new_pet->setStatus("available");
19-
// new tag
20-
$tag= new Swagger\Client\Model\Tag;
21-
$tag->setId($new_pet_id); // use the same id as pet
22-
$tag->setName("test php tag");
23-
// new category
24-
$category = new Swagger\Client\Model\Category;
25-
$category->setId($new_pet_id); // use the same id as pet
26-
$category->setName("test php category");
27-
28-
$new_pet->setTags(array($tag));
29-
$new_pet->setCategory($category);
30-
31-
$pet_api = new Swagger\Client\Api\PetAPI();
32-
// add a new pet (model)
33-
$add_response = $pet_api->addPet($new_pet);
34-
}
35-
36-
// test get inventory
37-
public function testGetInventory()
38-
{
39-
// initialize the API client
40-
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
41-
$api_client = new Swagger\Client\ApiClient($config);
42-
$store_api = new Swagger\Client\Api\StoreAPI($api_client);
43-
// get inventory
44-
$get_response = $store_api->getInventory();
45-
46-
$this->assertInternalType("int", $get_response['available']);
47-
48-
}
8+
// add a new pet (id 10005) to ensure the pet object is available for all the tests
9+
public static function setUpBeforeClass() {
10+
// for error reporting (need to run with php5.3 to get no warning)
11+
//ini_set('display_errors', 1);
12+
//error_reporting(~0);
13+
// new pet
14+
$new_pet_id = 10005;
15+
$new_pet = new Swagger\Client\Model\Pet;
16+
$new_pet->setId($new_pet_id);
17+
$new_pet->setName("PHP Unit Test");
18+
$new_pet->setStatus("available");
19+
// new tag
20+
$tag= new Swagger\Client\Model\Tag;
21+
$tag->setId($new_pet_id); // use the same id as pet
22+
$tag->setName("test php tag");
23+
// new category
24+
$category = new Swagger\Client\Model\Category;
25+
$category->setId($new_pet_id); // use the same id as pet
26+
$category->setName("test php category");
27+
28+
$new_pet->setTags(array($tag));
29+
$new_pet->setCategory($category);
30+
31+
$pet_api = new Swagger\Client\Api\PetAPI();
32+
// add a new pet (model)
33+
$add_response = $pet_api->addPet($new_pet);
34+
}
35+
36+
// test get inventory
37+
public function testGetInventory()
38+
{
39+
// initialize the API client
40+
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
41+
$api_client = new Swagger\Client\ApiClient($config);
42+
$store_api = new Swagger\Client\Api\StoreAPI($api_client);
43+
// get inventory
44+
$get_response = $store_api->getInventory();
45+
46+
$this->assertInternalType("array", $get_response);
47+
$this->assertInternalType("int", $get_response['available']);
48+
}
49+
50+
// test get inventory
51+
public function testGetInventoryInObject()
52+
{
53+
// initialize the API client
54+
//$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
55+
$api_client = new Swagger\Client\ApiClient();
56+
$store_api = new Swagger\Client\Api\StoreAPI($api_client);
57+
// get inventory
58+
$get_response = $store_api->getInventoryInObject();
59+
60+
$this->assertInternalType("array", $get_response);
61+
$this->assertInternalType("int", $get_response['available']);
62+
}
4963

5064
}
5165

0 commit comments

Comments
 (0)