mirror of
https://github.com/OpenAPITools/openapi-generator.git
synced 2025-06-22 08:40:50 +00:00
Merge pull request #1891 from wing328/php_binary_support
[PHP] addd binary support to response and body parameter
This commit is contained in:
commit
e0738d2bc6
@ -110,6 +110,7 @@ public class CodegenParameter {
|
||||
output.allowableValues = new HashMap<String, Object>(this.allowableValues);
|
||||
}
|
||||
output.vendorExtensions = this.vendorExtensions;
|
||||
output.isBinary = this.isBinary;
|
||||
|
||||
return output;
|
||||
}
|
||||
|
@ -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) {
|
||||
|
@ -97,6 +97,7 @@ public class PhpClientCodegen extends DefaultCodegen implements CodegenConfig {
|
||||
typeMapping.put("array", "array");
|
||||
typeMapping.put("list", "array");
|
||||
typeMapping.put("object", "object");
|
||||
typeMapping.put("binary", "ByteArray");
|
||||
|
||||
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
|
||||
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));
|
||||
|
@ -230,7 +230,7 @@ class ApiClient
|
||||
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
|
||||
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
|
||||
// return raw body if response is a file
|
||||
if ($responseType == '\SplFileObject') {
|
||||
if ($responseType == '\SplFileObject' || $responseType == 'ByteArray') {
|
||||
return array($http_body, $response_info['http_code'], $http_header);
|
||||
}
|
||||
|
||||
|
@ -241,6 +241,8 @@ class ObjectSerializer
|
||||
$values[] = $this->deserialize($value, $subClass);
|
||||
}
|
||||
$deserialized = $values;
|
||||
} elseif ($class === 'ByteArray') { // byte array
|
||||
$deserialized = unpack('C*', (string)$data);
|
||||
} elseif ($class === '\DateTime') {
|
||||
$deserialized = new \DateTime($data);
|
||||
} elseif (in_array($class, array({{&primitives}}))) {
|
||||
|
@ -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)
|
||||
|
@ -1,18 +1,25 @@
|
||||
<Properties StartupItem="SwaggerClientTest.csproj">
|
||||
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
|
||||
<MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
|
||||
<MonoDevelop.Ide.Workbench ActiveDocument="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs">
|
||||
<Files>
|
||||
<File FileName="TestPet.cs" Line="182" Column="4" />
|
||||
<File FileName="TestConfiguration.cs" Line="17" Column="7" />
|
||||
<File FileName="TestPet.cs" Line="288" Column="11" />
|
||||
<File FileName="TestConfiguration.cs" Line="1" Column="1" />
|
||||
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs" Line="1" Column="1" />
|
||||
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs" Line="1" Column="1" />
|
||||
<File FileName="TestApiClient.cs" Line="1" Column="1" />
|
||||
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs" Line="1" Column="1" />
|
||||
</Files>
|
||||
<Pads>
|
||||
<Pad Id="MonoDevelop.NUnit.TestPad">
|
||||
<State name="__root__">
|
||||
<Node name="SwaggerClientTest" expanded="True">
|
||||
<Node name="SwaggerClientTest" selected="True" />
|
||||
<Node name="SwaggerClientTest" expanded="True">
|
||||
<Node name="SwaggerClientTest" expanded="True">
|
||||
<Node name="TestApiClient" expanded="True">
|
||||
<Node name="TestApiClient" expanded="True" />
|
||||
</Node>
|
||||
</Node>
|
||||
</Node>
|
||||
</Node>
|
||||
</State>
|
||||
</Pad>
|
||||
|
@ -1,3 +1,12 @@
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll
|
||||
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll
|
||||
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs
|
||||
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png
|
||||
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb
|
||||
|
@ -873,4 +873,190 @@ class PetApi
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* getPetByIdWithByteArray
|
||||
*
|
||||
* Fake endpoint to test byte array return by 'Find pet by ID'
|
||||
*
|
||||
* @param int $pet_id ID of pet that needs to be fetched (required)
|
||||
* @return ByteArray
|
||||
* @throws \Swagger\Client\ApiException on non-2xx response
|
||||
*/
|
||||
public function getPetByIdWithByteArray($pet_id)
|
||||
{
|
||||
list($response, $statusCode, $httpHeader) = $this->getPetByIdWithByteArrayWithHttpInfo ($pet_id);
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* getPetByIdWithByteArrayWithHttpInfo
|
||||
*
|
||||
* Fake endpoint to test byte array return by 'Find pet by ID'
|
||||
*
|
||||
* @param int $pet_id ID of pet that needs to be fetched (required)
|
||||
* @return Array of ByteArray, HTTP status code, HTTP response headers (array of strings)
|
||||
* @throws \Swagger\Client\ApiException on non-2xx response
|
||||
*/
|
||||
public function getPetByIdWithByteArrayWithHttpInfo($pet_id)
|
||||
{
|
||||
|
||||
// verify the required parameter 'pet_id' is set
|
||||
if ($pet_id === null) {
|
||||
throw new \InvalidArgumentException('Missing the required parameter $pet_id when calling getPetByIdWithByteArray');
|
||||
}
|
||||
|
||||
// parse inputs
|
||||
$resourcePath = "/pet/{petId}?testing_byte_array=true";
|
||||
$httpBody = '';
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = ApiClient::selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array());
|
||||
|
||||
|
||||
|
||||
// path params
|
||||
|
||||
if ($pet_id !== null) {
|
||||
$resourcePath = str_replace(
|
||||
"{" . "petId" . "}",
|
||||
$this->apiClient->getSerializer()->toPathValue($pet_id),
|
||||
$resourcePath
|
||||
);
|
||||
}
|
||||
// default format to json
|
||||
$resourcePath = str_replace("{format}", "json", $resourcePath);
|
||||
|
||||
|
||||
|
||||
|
||||
// for model (json/xml)
|
||||
if (isset($_tempBody)) {
|
||||
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
|
||||
} elseif (count($formParams) > 0) {
|
||||
$httpBody = $formParams; // for HTTP post (form)
|
||||
}
|
||||
|
||||
// this endpoint requires API key authentication
|
||||
$apiKey = $this->apiClient->getApiKeyWithPrefix('api_key');
|
||||
if (strlen($apiKey) !== 0) {
|
||||
$headerParams['api_key'] = $apiKey;
|
||||
}
|
||||
|
||||
|
||||
// make the API Call
|
||||
try {
|
||||
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
||||
$resourcePath, 'GET',
|
||||
$queryParams, $httpBody,
|
||||
$headerParams, 'ByteArray'
|
||||
);
|
||||
|
||||
if (!$response) {
|
||||
return array(null, $statusCode, $httpHeader);
|
||||
}
|
||||
|
||||
return array($this->apiClient->getSerializer()->deserialize($response, 'ByteArray', $httpHeader), $statusCode, $httpHeader);
|
||||
|
||||
} catch (ApiException $e) {
|
||||
switch ($e->getCode()) {
|
||||
case 200:
|
||||
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), 'ByteArray', $e->getResponseHeaders());
|
||||
$e->setResponseObject($data);
|
||||
break;
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* addPetUsingByteArray
|
||||
*
|
||||
* Fake endpoint to test byte array in body parameter for adding a new pet to the store
|
||||
*
|
||||
* @param ByteArray $body Pet object in the form of byte array (optional)
|
||||
* @return void
|
||||
* @throws \Swagger\Client\ApiException on non-2xx response
|
||||
*/
|
||||
public function addPetUsingByteArray($body = null)
|
||||
{
|
||||
list($response, $statusCode, $httpHeader) = $this->addPetUsingByteArrayWithHttpInfo ($body);
|
||||
return $response;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* addPetUsingByteArrayWithHttpInfo
|
||||
*
|
||||
* Fake endpoint to test byte array in body parameter for adding a new pet to the store
|
||||
*
|
||||
* @param ByteArray $body Pet object in the form of byte array (optional)
|
||||
* @return Array of null, HTTP status code, HTTP response headers (array of strings)
|
||||
* @throws \Swagger\Client\ApiException on non-2xx response
|
||||
*/
|
||||
public function addPetUsingByteArrayWithHttpInfo($body = null)
|
||||
{
|
||||
|
||||
|
||||
// parse inputs
|
||||
$resourcePath = "/pet?testing_byte_array=true";
|
||||
$httpBody = '';
|
||||
$queryParams = array();
|
||||
$headerParams = array();
|
||||
$formParams = array();
|
||||
$_header_accept = ApiClient::selectHeaderAccept(array('application/json', 'application/xml'));
|
||||
if (!is_null($_header_accept)) {
|
||||
$headerParams['Accept'] = $_header_accept;
|
||||
}
|
||||
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array('application/json','application/xml'));
|
||||
|
||||
|
||||
|
||||
|
||||
// default format to json
|
||||
$resourcePath = str_replace("{format}", "json", $resourcePath);
|
||||
|
||||
|
||||
// body params
|
||||
$_tempBody = null;
|
||||
if (isset($body)) {
|
||||
$_tempBody = call_user_func_array('pack', array_merge(array('C*'), $body));
|
||||
}
|
||||
|
||||
// for model (json/xml)
|
||||
if (isset($_tempBody)) {
|
||||
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
|
||||
} elseif (count($formParams) > 0) {
|
||||
$httpBody = $formParams; // for HTTP post (form)
|
||||
}
|
||||
|
||||
// this endpoint requires OAuth (access token)
|
||||
if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
|
||||
$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
|
||||
}
|
||||
|
||||
// make the API Call
|
||||
try {
|
||||
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
|
||||
$resourcePath, 'POST',
|
||||
$queryParams, $httpBody,
|
||||
$headerParams
|
||||
);
|
||||
|
||||
return array(null, $statusCode, $httpHeader);
|
||||
|
||||
} catch (ApiException $e) {
|
||||
switch ($e->getCode()) {
|
||||
}
|
||||
|
||||
throw $e;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -230,7 +230,7 @@ class ApiClient
|
||||
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
|
||||
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
|
||||
// return raw body if response is a file
|
||||
if ($responseType == '\SplFileObject') {
|
||||
if ($responseType == '\SplFileObject' || $responseType == 'ByteArray') {
|
||||
return array($http_body, $response_info['http_code'], $http_header);
|
||||
}
|
||||
|
||||
|
@ -241,6 +241,8 @@ class ObjectSerializer
|
||||
$values[] = $this->deserialize($value, $subClass);
|
||||
}
|
||||
$deserialized = $values;
|
||||
} elseif ($class === 'ByteArray') { // byte array
|
||||
$deserialized = unpack('C*', (string)$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'))) {
|
||||
|
@ -211,10 +211,10 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
|
||||
// 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->addPet($new_pet);
|
||||
@ -223,9 +223,47 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
|
||||
// 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');
|
||||
}
|
||||
|
||||
|
||||
|
||||
// test upload file
|
||||
public function testUploadFile()
|
||||
{
|
||||
@ -255,6 +293,29 @@ class PetApiTest extends \PHPUnit_Framework_TestCase
|
||||
$this->assertInternalType("int", $get_response['pending']);
|
||||
}
|
||||
|
||||
// test byte array response
|
||||
public function testGetPetByIdWithByteArray()
|
||||
{
|
||||
// initialize the API client
|
||||
$config = new Swagger\Client\Configuration();
|
||||
$config->setHost('http://petstore.swagger.io/v2');
|
||||
$api_client = new Swagger\Client\APIClient($config);
|
||||
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
|
||||
// test getPetByIdWithByteArray
|
||||
$pet_id = 10005;
|
||||
$bytes = $pet_api->getPetByIdWithByteArray($pet_id);
|
||||
$json = json_decode(call_user_func_array('pack', array_merge(array('C*'), $bytes )), true);
|
||||
|
||||
$this->assertInternalType("array", $bytes);
|
||||
|
||||
$this->assertSame($json['id'], $pet_id);
|
||||
// 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);
|
||||
$this->assertSame($json['tags'][0]['name'], 'test php tag');
|
||||
}
|
||||
}
|
||||
|
||||
?>
|
||||
|
Loading…
x
Reference in New Issue
Block a user