Skip to content

Commit 7915a65

Browse files
committed
fix isBinary, CodegenParameter copy, add isBinary to php api client
1 parent 32dbf46 commit 7915a65

File tree

5 files changed

+49
-15
lines changed

5 files changed

+49
-15
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public CodegenParameter copy() {
110110
output.allowableValues = new HashMap<String, Object>(this.allowableValues);
111111
}
112112
output.vendorExtensions = this.vendorExtensions;
113+
output.isBinary = this.isBinary;
113114

114115
return output;
115116
}

modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1329,6 +1329,7 @@ public CodegenOperation fromOperation(String path, String httpMethod, Operation
13291329
cookieParams.add(p.copy());
13301330
} else if (param instanceof BodyParameter) {
13311331
p.isBodyParam = new Boolean(true);
1332+
p.isBinary = p.dataType.toLowerCase().startsWith("byte");
13321333
bodyParam = p;
13331334
bodyParams.add(p.copy());
13341335
} else if (param instanceof FormParameter) {
@@ -1422,7 +1423,7 @@ public CodegenResponse fromResponse(String responseCode, Response response) {
14221423
}
14231424
}
14241425
r.dataType = cm.datatype;
1425-
r.isBinary = cm.datatype.equals("byte[]");
1426+
r.isBinary = cm.datatype.toLowerCase().startsWith("byte");
14261427
if (cm.isContainer != null) {
14271428
r.simpleType = false;
14281429
r.containerType = cm.containerType;
@@ -1567,12 +1568,7 @@ public CodegenParameter fromParameter(Parameter param, Set<String> imports) {
15671568
CodegenProperty cp = fromProperty("property", prop);
15681569
if (cp != null) {
15691570
p.dataType = cp.datatype;
1570-
if (p.dataType.equals("byte[]")) {
1571-
p.isBinary = true;
1572-
}
1573-
else {
1574-
p.isBinary = false;
1575-
}
1571+
p.isBinary = cp.datatype.toLowerCase().startsWith("byte");
15761572
}
15771573
}
15781574
} else if (model instanceof ArrayModel) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ use \{{invokerPackage}}\ObjectSerializer;
188188
{{#bodyParams}}// body params
189189
$_tempBody = null;
190190
if (isset(${{paramName}})) {
191-
$_tempBody = ${{paramName}};
191+
{{^isBinary}}$_tempBody = ${{paramName}};{{/isBinary}}{{#isBinary}}$_tempBody = call_user_func_array('pack', array_merge(array('C*'), ${{paramName}}));{{/isBinary}}
192192
}{{/bodyParams}}
193193

194194
// for model (json/xml)

samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1026,7 +1026,7 @@ public function addPetUsingByteArrayWithHttpInfo($body = null)
10261026
// body params
10271027
$_tempBody = null;
10281028
if (isset($body)) {
1029-
$_tempBody = $body;
1029+
$_tempBody = call_user_func_array('pack', array_merge(array('C*'), $body));
10301030
}
10311031

10321032
// for model (json/xml)

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

Lines changed: 43 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -204,26 +204,62 @@ public function testUpdatePetWithForm()
204204
$this->assertSame($response->getName(), 'update pet with form');
205205
$this->assertSame($response->getStatus(), 'sold');
206206
}
207-
207+
208208
// test addPet and verify by the "id" and "name" of the response
209+
public function testAddPet()
210+
{
211+
// initialize the API client
212+
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
213+
$api_client = new Swagger\Client\ApiClient($config);
214+
$new_pet_id = 10005;
215+
$new_pet = new Swagger\Client\Model\Pet;
216+
$new_pet->setId($new_pet_id);
217+
$new_pet->setName("PHP Unit Test 2");
218+
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
219+
// add a new pet (model)
220+
$add_response = $pet_api->addPet($new_pet);
221+
// return nothing (void)
222+
$this->assertSame($add_response, NULL);
223+
// verify added Pet
224+
$response = $pet_api->getPetById($new_pet_id);
225+
$this->assertSame($response->getId(), $new_pet_id);
226+
$this->assertSame($response->getName(), 'PHP Unit Test 2');
227+
}
228+
229+
// test addPetUsingByteArray and verify by the "id" and "name" of the response
209230
public function testAddPetUsingByteArray()
210231
{
211232
// initialize the API client
212233
$config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2');
213234
$api_client = new Swagger\Client\ApiClient($config);
214-
$new_pet_id = 10001;
235+
236+
$new_pet_id = 10005;
215237
$new_pet = new Swagger\Client\Model\Pet;
216238
$new_pet->setId($new_pet_id);
217-
$new_pet->setName("PHP Unit Test");
239+
$new_pet->setName("PHP Unit Test 3");
240+
// new tag
241+
$tag= new Swagger\Client\Model\Tag;
242+
$tag->setId($new_pet_id); // use the same id as pet
243+
$tag->setName("test php tag");
244+
// new category
245+
$category = new Swagger\Client\Model\Category;
246+
$category->setId($new_pet_id); // use the same id as pet
247+
$category->setName("test php category");
248+
249+
$new_pet->setTags(array($tag));
250+
$new_pet->setCategory($category);
251+
218252
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
219253
// add a new pet (model)
220-
$add_response = $pet_api->addPetUsingByteArray(unpack('C*', $new_pet));
254+
$object_serializer = new Swagger\Client\ObjectSerializer();
255+
$pet_json_string = json_encode($object_serializer->sanitizeForSerialization($new_pet));
256+
$add_response = $pet_api->addPetUsingByteArray(unpack('C*', $pet_json_string));
221257
// return nothing (void)
222258
$this->assertSame($add_response, NULL);
223259
// verify added Pet
224260
$response = $pet_api->getPetById($new_pet_id);
225261
$this->assertSame($response->getId(), $new_pet_id);
226-
$this->assertSame($response->getName(), 'PHP Unit Test');
262+
$this->assertSame($response->getName(), 'PHP Unit Test 3');
227263
}
228264

229265

@@ -273,7 +309,8 @@ public function testGetPetByIdWithByteArray()
273309
$this->assertInternalType("array", $bytes);
274310

275311
$this->assertSame($json['id'], $pet_id);
276-
$this->assertSame($json['name'], 'PHP Unit Test');
312+
// not testing name as it's tested by addPetUsingByteArray
313+
//$this->assertSame($json['name'], 'PHP Unit Test');
277314
$this->assertSame($json['category']['id'], $pet_id);
278315
$this->assertSame($json['category']['name'], 'test php category');
279316
$this->assertSame($json['tags'][0]['id'], $pet_id);

0 commit comments

Comments
 (0)