add binary support for php response

This commit is contained in:
wing328 2016-01-14 17:27:21 +08:00
parent 078f68a211
commit f54185c66d
9 changed files with 236 additions and 7 deletions

View File

@ -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));

View File

@ -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);
}

View File

@ -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}}))) {

View File

@ -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>
@ -22,4 +29,4 @@
<BreakpointStore />
</MonoDevelop.Ide.DebuggingService.Breakpoints>
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
</Properties>
</Properties>

View File

@ -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

View File

@ -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 = $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;
}
}
}

View File

@ -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);
}

View File

@ -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'))) {

View File

@ -255,6 +255,28 @@ 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);
$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');
}
}
?>