Skip to content

Commit f54185c

Browse files
committed
add binary support for php response
1 parent 078f68a commit f54185c

File tree

9 files changed

+236
-7
lines changed

9 files changed

+236
-7
lines changed

modules/swagger-codegen/src/main/java/io/swagger/codegen/languages/PhpClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,7 @@ public PhpClientCodegen() {
9797
typeMapping.put("array", "array");
9898
typeMapping.put("list", "array");
9999
typeMapping.put("object", "object");
100+
typeMapping.put("binary", "ByteArray");
100101

101102
cliOptions.add(new CliOption(CodegenConstants.MODEL_PACKAGE, CodegenConstants.MODEL_PACKAGE_DESC));
102103
cliOptions.add(new CliOption(CodegenConstants.API_PACKAGE, CodegenConstants.API_PACKAGE_DESC));

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ class ApiClient
230230
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
231231
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
232232
// return raw body if response is a file
233-
if ($responseType == '\SplFileObject') {
233+
if ($responseType == '\SplFileObject' || $responseType == 'ByteArray') {
234234
return array($http_body, $response_info['http_code'], $http_header);
235235
}
236236

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ class ObjectSerializer
241241
$values[] = $this->deserialize($value, $subClass);
242242
}
243243
$deserialized = $values;
244+
} elseif ($class === 'ByteArray') { // byte array
245+
$deserialized = unpack('C*', (string)$data);
244246
} elseif ($class === '\DateTime') {
245247
$deserialized = new \DateTime($data);
246248
} elseif (in_array($class, array({{&primitives}}))) {
Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
<Properties StartupItem="SwaggerClientTest.csproj">
22
<MonoDevelop.Ide.Workspace ActiveConfiguration="Debug" />
3-
<MonoDevelop.Ide.Workbench ActiveDocument="TestPet.cs">
3+
<MonoDevelop.Ide.Workbench ActiveDocument="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs">
44
<Files>
55
<File FileName="TestPet.cs" Line="182" Column="4" />
6-
<File FileName="TestConfiguration.cs" Line="17" Column="7" />
7-
<File FileName="TestPet.cs" Line="288" Column="11" />
6+
<File FileName="TestConfiguration.cs" Line="1" Column="1" />
87
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Api/PetApi.cs" Line="1" Column="1" />
98
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/ApiClient.cs" Line="1" Column="1" />
9+
<File FileName="TestApiClient.cs" Line="1" Column="1" />
10+
<File FileName="Lib/SwaggerClient/src/main/csharp/IO/Swagger/Client/Configuration.cs" Line="1" Column="1" />
1011
</Files>
1112
<Pads>
1213
<Pad Id="MonoDevelop.NUnit.TestPad">
1314
<State name="__root__">
1415
<Node name="SwaggerClientTest" expanded="True">
15-
<Node name="SwaggerClientTest" selected="True" />
16+
<Node name="SwaggerClientTest" expanded="True">
17+
<Node name="SwaggerClientTest" expanded="True">
18+
<Node name="TestApiClient" expanded="True">
19+
<Node name="TestApiClient" expanded="True" />
20+
</Node>
21+
</Node>
22+
</Node>
1623
</Node>
1724
</State>
1825
</Pad>
@@ -22,4 +29,4 @@
2229
<BreakpointStore />
2330
</MonoDevelop.Ide.DebuggingService.Breakpoints>
2431
<MonoDevelop.Ide.DebuggingService.PinnedWatches />
25-
</Properties>
32+
</Properties>

samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.csproj.FilesWrittenAbsolute.txt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs
2+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png
3+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb
4+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll
5+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll
6+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.dll.mdb
7+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/Newtonsoft.Json.dll
8+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/nunit.framework.dll
9+
/Users/williamcheng/Code/tmp/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/RestSharp.dll
110
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/.NETFramework,Version=v4.5.AssemblyAttribute.cs
211
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/obj/Debug/SwaggerClientTest.swagger-logo.png
312
/Users/williamcheng/Code/wing328/swagger-codegen/samples/client/petstore/csharp/SwaggerClientTest/bin/Debug/SwaggerClientTest.dll.mdb

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

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,4 +873,190 @@ public function uploadFileWithHttpInfo($pet_id, $additional_metadata = null, $fi
873873
}
874874
}
875875

876+
/**
877+
* getPetByIdWithByteArray
878+
*
879+
* Fake endpoint to test byte array return by 'Find pet by ID'
880+
*
881+
* @param int $pet_id ID of pet that needs to be fetched (required)
882+
* @return ByteArray
883+
* @throws \Swagger\Client\ApiException on non-2xx response
884+
*/
885+
public function getPetByIdWithByteArray($pet_id)
886+
{
887+
list($response, $statusCode, $httpHeader) = $this->getPetByIdWithByteArrayWithHttpInfo ($pet_id);
888+
return $response;
889+
}
890+
891+
892+
/**
893+
* getPetByIdWithByteArrayWithHttpInfo
894+
*
895+
* Fake endpoint to test byte array return by 'Find pet by ID'
896+
*
897+
* @param int $pet_id ID of pet that needs to be fetched (required)
898+
* @return Array of ByteArray, HTTP status code, HTTP response headers (array of strings)
899+
* @throws \Swagger\Client\ApiException on non-2xx response
900+
*/
901+
public function getPetByIdWithByteArrayWithHttpInfo($pet_id)
902+
{
903+
904+
// verify the required parameter 'pet_id' is set
905+
if ($pet_id === null) {
906+
throw new \InvalidArgumentException('Missing the required parameter $pet_id when calling getPetByIdWithByteArray');
907+
}
908+
909+
// parse inputs
910+
$resourcePath = "/pet/{petId}?testing_byte_array=true";
911+
$httpBody = '';
912+
$queryParams = array();
913+
$headerParams = array();
914+
$formParams = array();
915+
$_header_accept = ApiClient::selectHeaderAccept(array('application/json', 'application/xml'));
916+
if (!is_null($_header_accept)) {
917+
$headerParams['Accept'] = $_header_accept;
918+
}
919+
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array());
920+
921+
922+
923+
// path params
924+
925+
if ($pet_id !== null) {
926+
$resourcePath = str_replace(
927+
"{" . "petId" . "}",
928+
$this->apiClient->getSerializer()->toPathValue($pet_id),
929+
$resourcePath
930+
);
931+
}
932+
// default format to json
933+
$resourcePath = str_replace("{format}", "json", $resourcePath);
934+
935+
936+
937+
938+
// for model (json/xml)
939+
if (isset($_tempBody)) {
940+
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
941+
} elseif (count($formParams) > 0) {
942+
$httpBody = $formParams; // for HTTP post (form)
943+
}
944+
945+
// this endpoint requires API key authentication
946+
$apiKey = $this->apiClient->getApiKeyWithPrefix('api_key');
947+
if (strlen($apiKey) !== 0) {
948+
$headerParams['api_key'] = $apiKey;
949+
}
950+
951+
952+
// make the API Call
953+
try {
954+
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
955+
$resourcePath, 'GET',
956+
$queryParams, $httpBody,
957+
$headerParams, 'ByteArray'
958+
);
959+
960+
if (!$response) {
961+
return array(null, $statusCode, $httpHeader);
962+
}
963+
964+
return array($this->apiClient->getSerializer()->deserialize($response, 'ByteArray', $httpHeader), $statusCode, $httpHeader);
965+
966+
} catch (ApiException $e) {
967+
switch ($e->getCode()) {
968+
case 200:
969+
$data = $this->apiClient->getSerializer()->deserialize($e->getResponseBody(), 'ByteArray', $e->getResponseHeaders());
970+
$e->setResponseObject($data);
971+
break;
972+
}
973+
974+
throw $e;
975+
}
976+
}
977+
978+
/**
979+
* addPetUsingByteArray
980+
*
981+
* Fake endpoint to test byte array in body parameter for adding a new pet to the store
982+
*
983+
* @param ByteArray $body Pet object in the form of byte array (optional)
984+
* @return void
985+
* @throws \Swagger\Client\ApiException on non-2xx response
986+
*/
987+
public function addPetUsingByteArray($body = null)
988+
{
989+
list($response, $statusCode, $httpHeader) = $this->addPetUsingByteArrayWithHttpInfo ($body);
990+
return $response;
991+
}
992+
993+
994+
/**
995+
* addPetUsingByteArrayWithHttpInfo
996+
*
997+
* Fake endpoint to test byte array in body parameter for adding a new pet to the store
998+
*
999+
* @param ByteArray $body Pet object in the form of byte array (optional)
1000+
* @return Array of null, HTTP status code, HTTP response headers (array of strings)
1001+
* @throws \Swagger\Client\ApiException on non-2xx response
1002+
*/
1003+
public function addPetUsingByteArrayWithHttpInfo($body = null)
1004+
{
1005+
1006+
1007+
// parse inputs
1008+
$resourcePath = "/pet?testing_byte_array=true";
1009+
$httpBody = '';
1010+
$queryParams = array();
1011+
$headerParams = array();
1012+
$formParams = array();
1013+
$_header_accept = ApiClient::selectHeaderAccept(array('application/json', 'application/xml'));
1014+
if (!is_null($_header_accept)) {
1015+
$headerParams['Accept'] = $_header_accept;
1016+
}
1017+
$headerParams['Content-Type'] = ApiClient::selectHeaderContentType(array('application/json','application/xml'));
1018+
1019+
1020+
1021+
1022+
// default format to json
1023+
$resourcePath = str_replace("{format}", "json", $resourcePath);
1024+
1025+
1026+
// body params
1027+
$_tempBody = null;
1028+
if (isset($body)) {
1029+
$_tempBody = $body;
1030+
}
1031+
1032+
// for model (json/xml)
1033+
if (isset($_tempBody)) {
1034+
$httpBody = $_tempBody; // $_tempBody is the method argument, if present
1035+
} elseif (count($formParams) > 0) {
1036+
$httpBody = $formParams; // for HTTP post (form)
1037+
}
1038+
1039+
// this endpoint requires OAuth (access token)
1040+
if (strlen($this->apiClient->getConfig()->getAccessToken()) !== 0) {
1041+
$headerParams['Authorization'] = 'Bearer ' . $this->apiClient->getConfig()->getAccessToken();
1042+
}
1043+
1044+
// make the API Call
1045+
try {
1046+
list($response, $statusCode, $httpHeader) = $this->apiClient->callApi(
1047+
$resourcePath, 'POST',
1048+
$queryParams, $httpBody,
1049+
$headerParams
1050+
);
1051+
1052+
return array(null, $statusCode, $httpHeader);
1053+
1054+
} catch (ApiException $e) {
1055+
switch ($e->getCode()) {
1056+
}
1057+
1058+
throw $e;
1059+
}
1060+
}
1061+
8761062
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ public function callApi($resourcePath, $method, $queryParams, $postData, $header
230230
throw new ApiException("API call to $url timed out: ".serialize($response_info), 0, null, null);
231231
} elseif ($response_info['http_code'] >= 200 && $response_info['http_code'] <= 299 ) {
232232
// return raw body if response is a file
233-
if ($responseType == '\SplFileObject') {
233+
if ($responseType == '\SplFileObject' || $responseType == 'ByteArray') {
234234
return array($http_body, $response_info['http_code'], $http_header);
235235
}
236236

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,8 @@ public function deserialize($data, $class, $httpHeaders=null)
241241
$values[] = $this->deserialize($value, $subClass);
242242
}
243243
$deserialized = $values;
244+
} elseif ($class === 'ByteArray') { // byte array
245+
$deserialized = unpack('C*', (string)$data);
244246
} elseif ($class === '\DateTime') {
245247
$deserialized = new \DateTime($data);
246248
} 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: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,28 @@ public function testGetInventory()
255255
$this->assertInternalType("int", $get_response['pending']);
256256
}
257257

258+
// test byte array response
259+
public function testGetPetByIdWithByteArray()
260+
{
261+
// initialize the API client
262+
$config = new Swagger\Client\Configuration();
263+
$config->setHost('http://petstore.swagger.io/v2');
264+
$api_client = new Swagger\Client\APIClient($config);
265+
$pet_api = new Swagger\Client\Api\PetAPI($api_client);
266+
// test getPetByIdWithByteArray
267+
$pet_id = 10005;
268+
$bytes = $pet_api->getPetByIdWithByteArray($pet_id);
269+
$json = json_decode(call_user_func_array('pack', array_merge(array('C*'), $bytes )), true);
270+
271+
$this->assertInternalType("array", $bytes);
272+
273+
$this->assertSame($json['id'], $pet_id);
274+
$this->assertSame($json['name'], 'PHP Unit Test');
275+
$this->assertSame($json['category']['id'], $pet_id);
276+
$this->assertSame($json['category']['name'], 'test php category');
277+
$this->assertSame($json['tags'][0]['id'], $pet_id);
278+
$this->assertSame($json['tags'][0]['name'], 'test php tag');
279+
}
258280
}
259281

260282
?>

0 commit comments

Comments
 (0)