From fee8acef9722b8b790d1c72eac1cfc5290bcec4b Mon Sep 17 00:00:00 2001 From: wing328 Date: Fri, 15 Jan 2016 00:20:36 +0800 Subject: [PATCH] fix isBinary, CodegenParameter copy, add isBinary to php api client --- .../io/swagger/codegen/CodegenParameter.java | 1 + .../io/swagger/codegen/DefaultCodegen.java | 10 ++-- .../src/main/resources/php/api.mustache | 2 +- .../php/SwaggerClient-php/lib/Api/PetApi.php | 2 +- .../SwaggerClient-php/tests/PetApiTest.php | 51 ++++++++++++++++--- 5 files changed, 50 insertions(+), 16 deletions(-) diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java index ab934075a539..9bf2858161d0 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/CodegenParameter.java @@ -110,6 +110,7 @@ public class CodegenParameter { output.allowableValues = new HashMap(this.allowableValues); } output.vendorExtensions = this.vendorExtensions; + output.isBinary = this.isBinary; return output; } diff --git a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java index 1056e854f8a2..abc4d045315e 100644 --- a/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java +++ b/modules/swagger-codegen/src/main/java/io/swagger/codegen/DefaultCodegen.java @@ -1329,6 +1329,7 @@ public class DefaultCodegen { cookieParams.add(p.copy()); } else if (param instanceof BodyParameter) { p.isBodyParam = new Boolean(true); + p.isBinary = p.dataType.toLowerCase().startsWith("byte"); bodyParam = p; bodyParams.add(p.copy()); } else if (param instanceof FormParameter) { @@ -1422,7 +1423,7 @@ public class DefaultCodegen { } } r.dataType = cm.datatype; - r.isBinary = cm.datatype.equals("byte[]"); + r.isBinary = cm.datatype.toLowerCase().startsWith("byte"); if (cm.isContainer != null) { r.simpleType = false; r.containerType = cm.containerType; @@ -1567,12 +1568,7 @@ public class DefaultCodegen { CodegenProperty cp = fromProperty("property", prop); if (cp != null) { p.dataType = cp.datatype; - if (p.dataType.equals("byte[]")) { - p.isBinary = true; - } - else { - p.isBinary = false; - } + p.isBinary = cp.datatype.toLowerCase().startsWith("byte"); } } } else if (model instanceof ArrayModel) { diff --git a/modules/swagger-codegen/src/main/resources/php/api.mustache b/modules/swagger-codegen/src/main/resources/php/api.mustache index c0c497ff418a..6d1a3d570f7c 100644 --- a/modules/swagger-codegen/src/main/resources/php/api.mustache +++ b/modules/swagger-codegen/src/main/resources/php/api.mustache @@ -188,7 +188,7 @@ use \{{invokerPackage}}\ObjectSerializer; {{#bodyParams}}// body params $_tempBody = null; if (isset(${{paramName}})) { - $_tempBody = ${{paramName}}; + {{^isBinary}}$_tempBody = ${{paramName}};{{/isBinary}}{{#isBinary}}$_tempBody = call_user_func_array('pack', array_merge(array('C*'), ${{paramName}}));{{/isBinary}} }{{/bodyParams}} // for model (json/xml) diff --git a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php index bc1a5729bf52..66e8f34df094 100644 --- a/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php +++ b/samples/client/petstore/php/SwaggerClient-php/lib/Api/PetApi.php @@ -1026,7 +1026,7 @@ class PetApi // body params $_tempBody = null; if (isset($body)) { - $_tempBody = $body; + $_tempBody = call_user_func_array('pack', array_merge(array('C*'), $body)); } // for model (json/xml) diff --git a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php index 345c42b408c6..dfc459712092 100644 --- a/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php +++ b/samples/client/petstore/php/SwaggerClient-php/tests/PetApiTest.php @@ -204,26 +204,62 @@ class PetApiTest extends \PHPUnit_Framework_TestCase $this->assertSame($response->getName(), 'update pet with form'); $this->assertSame($response->getStatus(), 'sold'); } - + // test addPet and verify by the "id" and "name" of the response - public function testAddPetUsingByteArray() + public function testAddPet() { // initialize the API client $config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); $api_client = new Swagger\Client\ApiClient($config); - $new_pet_id = 10001; + $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->setName("PHP Unit Test 2"); $pet_api = new Swagger\Client\Api\PetAPI($api_client); // add a new pet (model) - $add_response = $pet_api->addPetUsingByteArray(unpack('C*', $new_pet)); + $add_response = $pet_api->addPet($new_pet); // return nothing (void) $this->assertSame($add_response, NULL); // verify added Pet $response = $pet_api->getPetById($new_pet_id); $this->assertSame($response->getId(), $new_pet_id); - $this->assertSame($response->getName(), 'PHP Unit Test'); + $this->assertSame($response->getName(), 'PHP Unit Test 2'); + } + + // test addPetUsingByteArray and verify by the "id" and "name" of the response + public function testAddPetUsingByteArray() + { + // initialize the API client + $config = (new Swagger\Client\Configuration())->setHost('http://petstore.swagger.io/v2'); + $api_client = new Swagger\Client\ApiClient($config); + + $new_pet_id = 10005; + $new_pet = new Swagger\Client\Model\Pet; + $new_pet->setId($new_pet_id); + $new_pet->setName("PHP Unit Test 3"); + // 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($api_client); + // add a new pet (model) + $object_serializer = new Swagger\Client\ObjectSerializer(); + $pet_json_string = json_encode($object_serializer->sanitizeForSerialization($new_pet)); + $add_response = $pet_api->addPetUsingByteArray(unpack('C*', $pet_json_string)); + // return nothing (void) + $this->assertSame($add_response, NULL); + // verify added Pet + $response = $pet_api->getPetById($new_pet_id); + $this->assertSame($response->getId(), $new_pet_id); + $this->assertSame($response->getName(), 'PHP Unit Test 3'); } @@ -273,7 +309,8 @@ class PetApiTest extends \PHPUnit_Framework_TestCase $this->assertInternalType("array", $bytes); $this->assertSame($json['id'], $pet_id); - $this->assertSame($json['name'], 'PHP Unit Test'); + // not testing name as it's tested by addPetUsingByteArray + //$this->assertSame($json['name'], 'PHP Unit Test'); $this->assertSame($json['category']['id'], $pet_id); $this->assertSame($json['category']['name'], 'test php category'); $this->assertSame($json['tags'][0]['id'], $pet_id);