Skip to content

Commit eb77d91

Browse files
committed
Issue #352 - Make Api return object instead of array
1 parent 4060fcb commit eb77d91

File tree

17 files changed

+4962
-729
lines changed

17 files changed

+4962
-729
lines changed

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/PhpClientCodegen.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ public void processOpts() {
105105

106106
supportingFiles.add(new SupportingFile("ApiException.mustache", toSrcPath(invokerPackage, srcBasePath), "ApiException.php"));
107107
supportingFiles.add(new SupportingFile("Configuration.mustache", toSrcPath(invokerPackage, srcBasePath), "Configuration.php"));
108+
supportingFiles.add(new SupportingFile("ApiResponse.mustache", toSrcPath(invokerPackage, srcBasePath), "ApiResponse.php"));
108109
supportingFiles.add(new SupportingFile("ObjectSerializer.mustache", toSrcPath(invokerPackage, srcBasePath), "ObjectSerializer.php"));
109110
supportingFiles.add(new SupportingFile("ModelInterface.mustache", toSrcPath(modelPackage, srcBasePath), "ModelInterface.php"));
110111
supportingFiles.add(new SupportingFile("HeaderSelector.mustache", toSrcPath(invokerPackage, srcBasePath), "HeaderSelector.php"));
Lines changed: 133 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,133 @@
1+
<?php
2+
/**
3+
* ApiResponse
4+
* PHP version 5
5+
*
6+
* @category Class
7+
* @package {{invokerPackage}}
8+
* @author OpenAPI Generator team
9+
* @link https://openapi-generator.tech
10+
*/
11+
12+
{{>partial_header}}
13+
/**
14+
* NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech).
15+
* https://openapi-generator.tech
16+
* Do not edit the class manually.
17+
*/
18+
19+
namespace {{invokerPackage}};
20+
21+
/**
22+
* ApiResponse Class Doc Comment
23+
* PHP version 5
24+
*
25+
* @category Class
26+
* @package {{invokerPackage}}
27+
* @author OpenAPI Generator team
28+
* @link https://openapi-generator.tech
29+
*/
30+
class ApiResponse
31+
{
32+
/**
33+
* Message (response) from API call
34+
*
35+
* @var mixed
36+
*/
37+
protected $message;
38+
/**
39+
* Response status code
40+
*
41+
* @var int
42+
*/
43+
protected $statusCode;
44+
/**
45+
* Message headers
46+
*
47+
* @var string[]
48+
*/
49+
protected $headers;
50+
51+
/**
52+
* Constructor
53+
*
54+
* @param mixed $message Message (response) from API call
55+
* @param int $statusCode Response status code
56+
* @param string[] $headers Message headers
57+
*/
58+
public function __construct($message, $statusCode, $headers)
59+
{
60+
$this->message = $message;
61+
$this->statusCode = $statusCode;
62+
$this->headers = $headers;
63+
}
64+
65+
/**
66+
* Sets message (response) from API call
67+
*
68+
* @param mixed $message Message (response) from API call
69+
*
70+
* @return $this
71+
*/
72+
public function setMessage($message)
73+
{
74+
$this->message = $message;
75+
return $this;
76+
}
77+
78+
/**
79+
* Gets message (response) from API call
80+
*
81+
* @return mixed Message (response) from API call
82+
*/
83+
public function getMessage()
84+
{
85+
return $this->message;
86+
}
87+
88+
/**
89+
* Sets response status code
90+
*
91+
* @param int $statusCode Response status code
92+
*
93+
* @return $this
94+
*/
95+
public function setStatusCode($statusCode)
96+
{
97+
$this->statusCode = $statusCode;
98+
return $this;
99+
}
100+
101+
/**
102+
* Gets response status code
103+
*
104+
* @return int Response status code
105+
*/
106+
public function getStatusCode()
107+
{
108+
return $this->statusCode;
109+
}
110+
111+
/**
112+
* Sets message headers
113+
*
114+
* @param string[] $headers Message headers
115+
*
116+
* @return $this
117+
*/
118+
public function setHeaders($headers)
119+
{
120+
$this->headers = $headers;
121+
return $this;
122+
}
123+
124+
/**
125+
* Gets message headers
126+
*
127+
* @return string[] Message headers
128+
*/
129+
public function getHeaders()
130+
{
131+
return $this->headers;
132+
}
133+
}

modules/openapi-generator/src/main/resources/php/api.mustache

Lines changed: 86 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ use GuzzleHttp\Psr7\MultipartStream;
2525
use GuzzleHttp\Psr7\Request;
2626
use GuzzleHttp\RequestOptions;
2727
use {{invokerPackage}}\ApiException;
28+
use {{invokerPackage}}\ApiResponse;
2829
use {{invokerPackage}}\Configuration;
2930
use {{invokerPackage}}\HeaderSelector;
3031
use {{invokerPackage}}\ObjectSerializer;
@@ -99,12 +100,16 @@ use {{invokerPackage}}\ObjectSerializer;
99100
*/
100101
public function {{operationId}}({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
101102
{
102-
{{#returnType}}list($response) = {{/returnType}}$this->{{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
103-
return $response;{{/returnType}}
103+
{{#returnType}}$apiResponse = {{/returnType}}$this->{{operationId}}WithApiResponse({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});{{#returnType}}
104+
return $apiResponse->getMessage();{{/returnType}}
104105
}
105106

107+
106108
/**
107109
* Operation {{{operationId}}}WithHttpInfo
110+
*
111+
* (method preserved to keep backward compatibility)
112+
*
108113
{{#summary}}
109114
*
110115
* {{{summary}}}
@@ -120,9 +125,41 @@ use {{invokerPackage}}\ObjectSerializer;
120125
*
121126
* @throws \{{invokerPackage}}\ApiException on non-2xx response
122127
* @throws \InvalidArgumentException
123-
* @return array of {{#returnType}}{{#responses}}{{#dataType}}{{^-first}}|{{/-first}}{{/dataType}}{{{dataType}}}{{/responses}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings)
128+
* @return array of {{#returnType}}{{#responses}}{{#dataType}}{{^-first}}|{{/-first}}{{/dataType}}{{{dataType}}}{{/responses}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (strings[])
124129
*/
125130
public function {{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
131+
{
132+
$apiResponse = $this->{{operationId}}WithApiResponse({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
133+
134+
return [
135+
$apiResponse->getMessage(),
136+
$apiResponse->getStatusCode(),
137+
$apiResponse->getHeaders()
138+
];
139+
}
140+
141+
142+
143+
/**
144+
* Operation {{{operationId}}}WithApiResponse
145+
{{#summary}}
146+
*
147+
* {{{summary}}}
148+
{{/summary}}
149+
*
150+
{{#description}}
151+
* {{.}}
152+
*
153+
{{/description}}
154+
{{#allParams}}
155+
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
156+
{{/allParams}}
157+
*
158+
* @throws \{{invokerPackage}}\ApiException on non-2xx response
159+
* @throws \InvalidArgumentException
160+
* @return APIResponse with "message" property {{#returnType}}of type {{{returnType}}}{{/returnType}}{{^returnType}}equals to null{{/returnType}}
161+
*/
162+
public function {{operationId}}WithApiResponse({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
126163
{
127164
$request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
128165

@@ -171,11 +208,11 @@ use {{invokerPackage}}\ObjectSerializer;
171208
}
172209
}
173210

174-
return [
211+
return new ApiResponse(
175212
ObjectSerializer::deserialize($content, '{{dataType}}', []),
176213
$response->getStatusCode(),
177214
$response->getHeaders()
178-
];
215+
);
179216
{{/dataType}}
180217
{{#-last}}
181218
}
@@ -193,15 +230,15 @@ use {{invokerPackage}}\ObjectSerializer;
193230
}
194231
}
195232

196-
return [
233+
return new ApiResponse(
197234
ObjectSerializer::deserialize($content, $returnType, []),
198235
$response->getStatusCode(),
199236
$response->getHeaders()
200-
];
237+
);
201238
{{/returnType}}
202239
{{^returnType}}
203240

204-
return [null, $statusCode, $response->getHeaders()];
241+
return new ApiResponse(null, $statusCode, $response->getHeaders());
205242
{{/returnType}}
206243

207244
} catch (ApiException $e) {
@@ -241,17 +278,19 @@ use {{invokerPackage}}\ObjectSerializer;
241278
*/
242279
public function {{operationId}}Async({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
243280
{
244-
return $this->{{operationId}}AsyncWithHttpInfo({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
281+
return $this->{{operationId}}AsyncWithApiResponse({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
245282
->then(
246-
function ($response) {
247-
return $response[0];
283+
function ($apiResponse) {
284+
return $apiResponse->getMessage();
248285
}
249286
);
250287
}
251288

252289
/**
253290
* Operation {{{operationId}}}AsyncWithHttpInfo
254291
*
292+
* (method preserved to keep backward compatibility)
293+
*
255294
* {{{summary}}}
256295
*
257296
{{#description}}
@@ -267,13 +306,45 @@ use {{invokerPackage}}\ObjectSerializer;
267306
*/
268307
public function {{operationId}}AsyncWithHttpInfo({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
269308
{
309+
return $this->{{operationId}}AsyncWithApiResponse({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
310+
->then(
311+
function ($apiResponse) {
312+
return [
313+
$apiResponse->getMessage(),
314+
$apiResponse->getStatusCode(),
315+
$apiResponse->getHeaders()
316+
];
317+
}
318+
);
319+
}
320+
321+
/**
322+
* Operation {{{operationId}}}AsyncWithApiResponse
323+
*
324+
* {{{summary}}}
325+
*
326+
{{#description}}
327+
* {{.}}
328+
*
329+
{{/description}}
330+
{{#allParams}}
331+
* @param {{dataType}} ${{paramName}}{{#description}} {{description}}{{/description}} {{#required}}(required){{/required}}{{^required}}(optional{{#defaultValue}}, default to {{{.}}}{{/defaultValue}}){{/required}}
332+
{{/allParams}}
333+
*
334+
* @throws \InvalidArgumentException
335+
* @return \GuzzleHttp\Promise\PromiseInterface
336+
*/
337+
public function {{operationId}}AsyncWithApiResponse({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
338+
{
339+
{{#returnType}}
270340
$returnType = '{{returnType}}';
341+
{{/returnType}}
271342
$request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
272343

273344
return $this->client
274345
->sendAsync($request, $this->createHttpClientOption())
275346
->then(
276-
function ($response) use ($returnType) {
347+
function ($response){{#returnType}} use ($returnType){{/returnType}} {
277348
{{#returnType}}
278349
$responseBody = $response->getBody();
279350
if ($returnType === '\SplFileObject') {
@@ -285,14 +356,14 @@ use {{invokerPackage}}\ObjectSerializer;
285356
}
286357
}
287358

288-
return [
359+
return new ApiResponse(
289360
ObjectSerializer::deserialize($content, $returnType, []),
290361
$response->getStatusCode(),
291362
$response->getHeaders()
292-
];
363+
);
293364
{{/returnType}}
294365
{{^returnType}}
295-
return [null, $response->getStatusCode(), $response->getHeaders()];
366+
return new ApiResponse(null, $response->getStatusCode(), $response->getHeaders());
296367
{{/returnType}}
297368
},
298369
function ($exception) {

0 commit comments

Comments
 (0)