Skip to content

Commit d19a2fa

Browse files
fMadsackintosh
authored andcommitted
[php] Correctly handle multiple valid responses (#128)
* #125: Correctly handle multiple valid responses * Fixed defaultResponses * Fixed void-returns * Fixed multiple return-types without an actual type * Update the samples under openapi3 folder
1 parent 3c9cf1d commit d19a2fa

14 files changed

Lines changed: 672 additions & 68 deletions

File tree

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

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ use {{invokerPackage}}\ObjectSerializer;
9595
*
9696
* @throws \{{invokerPackage}}\ApiException on non-2xx response
9797
* @throws \InvalidArgumentException
98-
* @return {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}void{{/returnType}}
98+
* @return {{#returnType}}{{#responses}}{{#dataType}}{{^-first}}|{{/-first}}{{/dataType}}{{{dataType}}}{{/responses}}{{/returnType}}{{^returnType}}void{{/returnType}}
9999
*/
100100
public function {{operationId}}({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
101101
{
@@ -120,11 +120,10 @@ use {{invokerPackage}}\ObjectSerializer;
120120
*
121121
* @throws \{{invokerPackage}}\ApiException on non-2xx response
122122
* @throws \InvalidArgumentException
123-
* @return array of {{#returnType}}{{{returnType}}}{{/returnType}}{{^returnType}}null{{/returnType}}, HTTP status code, HTTP response headers (array of strings)
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)
124124
*/
125125
public function {{operationId}}WithHttpInfo({{#allParams}}${{paramName}}{{^required}} = {{#defaultValue}}{{{.}}}{{/defaultValue}}{{^defaultValue}}null{{/defaultValue}}{{/required}}{{#hasMore}}, {{/hasMore}}{{/allParams}})
126126
{
127-
$returnType = '{{returnType}}';
128127
$request = $this->{{operationId}}Request({{#allParams}}${{paramName}}{{#hasMore}}, {{/hasMore}}{{/allParams}});
129128

130129
try {
@@ -154,8 +153,36 @@ use {{invokerPackage}}\ObjectSerializer;
154153
$response->getBody()
155154
);
156155
}
157-
158156
{{#returnType}}
157+
{{#responses}}
158+
{{#-first}}
159+
160+
$responseBody = $response->getBody();
161+
switch($statusCode) {
162+
{{/-first}}
163+
{{#dataType}}
164+
{{^isWildcard}}case {{code}}:{{/isWildcard}}{{#isWildcard}}default:{{/isWildcard}}
165+
if ('{{dataType}}' === '\SplFileObject') {
166+
$content = $responseBody; //stream goes to serializer
167+
} else {
168+
$content = $responseBody->getContents();
169+
if ('{{dataType}}' !== 'string') {
170+
$content = json_decode($content);
171+
}
172+
}
173+
174+
return [
175+
ObjectSerializer::deserialize($content, '{{dataType}}', []),
176+
$response->getStatusCode(),
177+
$response->getHeaders()
178+
];
179+
{{/dataType}}
180+
{{#-last}}
181+
}
182+
{{/-last}}
183+
{{/responses}}
184+
185+
$returnType = '{{returnType}}';
159186
$responseBody = $response->getBody();
160187
if ($returnType === '\SplFileObject') {
161188
$content = $responseBody; //stream goes to serializer
@@ -173,6 +200,7 @@ use {{invokerPackage}}\ObjectSerializer;
173200
];
174201
{{/returnType}}
175202
{{^returnType}}
203+
176204
return [null, $statusCode, $response->getHeaders()];
177205
{{/returnType}}
178206

samples/client/petstore-security-test/php/OpenAPIClient-php/lib/Api/FakeApi.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,6 @@ public function testCodeInjectEndRnNR($unknown_base_type = null)
116116
*/
117117
public function testCodeInjectEndRnNRWithHttpInfo($unknown_base_type = null)
118118
{
119-
$returnType = '';
120119
$request = $this->testCodeInjectEndRnNRRequest($unknown_base_type);
121120

122121
try {

samples/client/petstore/php/OpenAPIClient-php/lib/Api/AnotherFakeApi.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public function testSpecialTags($client)
117117
*/
118118
public function testSpecialTagsWithHttpInfo($client)
119119
{
120-
$returnType = '\OpenAPI\Client\Model\Client';
121120
$request = $this->testSpecialTagsRequest($client);
122121

123122
try {
@@ -148,6 +147,26 @@ public function testSpecialTagsWithHttpInfo($client)
148147
);
149148
}
150149

150+
$responseBody = $response->getBody();
151+
switch($statusCode) {
152+
case 200:
153+
if ('\OpenAPI\Client\Model\Client' === '\SplFileObject') {
154+
$content = $responseBody; //stream goes to serializer
155+
} else {
156+
$content = $responseBody->getContents();
157+
if ('\OpenAPI\Client\Model\Client' !== 'string') {
158+
$content = json_decode($content);
159+
}
160+
}
161+
162+
return [
163+
ObjectSerializer::deserialize($content, '\OpenAPI\Client\Model\Client', []),
164+
$response->getStatusCode(),
165+
$response->getHeaders()
166+
];
167+
}
168+
169+
$returnType = '\OpenAPI\Client\Model\Client';
151170
$responseBody = $response->getBody();
152171
if ($returnType === '\SplFileObject') {
153172
$content = $responseBody; //stream goes to serializer

samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeApi.php

Lines changed: 100 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,6 @@ public function fakeOuterBooleanSerialize($body = null)
113113
*/
114114
public function fakeOuterBooleanSerializeWithHttpInfo($body = null)
115115
{
116-
$returnType = 'bool';
117116
$request = $this->fakeOuterBooleanSerializeRequest($body);
118117

119118
try {
@@ -144,6 +143,26 @@ public function fakeOuterBooleanSerializeWithHttpInfo($body = null)
144143
);
145144
}
146145

146+
$responseBody = $response->getBody();
147+
switch($statusCode) {
148+
case 200:
149+
if ('bool' === '\SplFileObject') {
150+
$content = $responseBody; //stream goes to serializer
151+
} else {
152+
$content = $responseBody->getContents();
153+
if ('bool' !== 'string') {
154+
$content = json_decode($content);
155+
}
156+
}
157+
158+
return [
159+
ObjectSerializer::deserialize($content, 'bool', []),
160+
$response->getStatusCode(),
161+
$response->getHeaders()
162+
];
163+
}
164+
165+
$returnType = 'bool';
147166
$responseBody = $response->getBody();
148167
if ($returnType === '\SplFileObject') {
149168
$content = $responseBody; //stream goes to serializer
@@ -360,7 +379,6 @@ public function fakeOuterCompositeSerialize($outer_composite = null)
360379
*/
361380
public function fakeOuterCompositeSerializeWithHttpInfo($outer_composite = null)
362381
{
363-
$returnType = '\OpenAPI\Client\Model\OuterComposite';
364382
$request = $this->fakeOuterCompositeSerializeRequest($outer_composite);
365383

366384
try {
@@ -391,6 +409,26 @@ public function fakeOuterCompositeSerializeWithHttpInfo($outer_composite = null)
391409
);
392410
}
393411

412+
$responseBody = $response->getBody();
413+
switch($statusCode) {
414+
case 200:
415+
if ('\OpenAPI\Client\Model\OuterComposite' === '\SplFileObject') {
416+
$content = $responseBody; //stream goes to serializer
417+
} else {
418+
$content = $responseBody->getContents();
419+
if ('\OpenAPI\Client\Model\OuterComposite' !== 'string') {
420+
$content = json_decode($content);
421+
}
422+
}
423+
424+
return [
425+
ObjectSerializer::deserialize($content, '\OpenAPI\Client\Model\OuterComposite', []),
426+
$response->getStatusCode(),
427+
$response->getHeaders()
428+
];
429+
}
430+
431+
$returnType = '\OpenAPI\Client\Model\OuterComposite';
394432
$responseBody = $response->getBody();
395433
if ($returnType === '\SplFileObject') {
396434
$content = $responseBody; //stream goes to serializer
@@ -607,7 +645,6 @@ public function fakeOuterNumberSerialize($body = null)
607645
*/
608646
public function fakeOuterNumberSerializeWithHttpInfo($body = null)
609647
{
610-
$returnType = 'float';
611648
$request = $this->fakeOuterNumberSerializeRequest($body);
612649

613650
try {
@@ -638,6 +675,26 @@ public function fakeOuterNumberSerializeWithHttpInfo($body = null)
638675
);
639676
}
640677

678+
$responseBody = $response->getBody();
679+
switch($statusCode) {
680+
case 200:
681+
if ('float' === '\SplFileObject') {
682+
$content = $responseBody; //stream goes to serializer
683+
} else {
684+
$content = $responseBody->getContents();
685+
if ('float' !== 'string') {
686+
$content = json_decode($content);
687+
}
688+
}
689+
690+
return [
691+
ObjectSerializer::deserialize($content, 'float', []),
692+
$response->getStatusCode(),
693+
$response->getHeaders()
694+
];
695+
}
696+
697+
$returnType = 'float';
641698
$responseBody = $response->getBody();
642699
if ($returnType === '\SplFileObject') {
643700
$content = $responseBody; //stream goes to serializer
@@ -854,7 +911,6 @@ public function fakeOuterStringSerialize($body = null)
854911
*/
855912
public function fakeOuterStringSerializeWithHttpInfo($body = null)
856913
{
857-
$returnType = 'string';
858914
$request = $this->fakeOuterStringSerializeRequest($body);
859915

860916
try {
@@ -885,6 +941,26 @@ public function fakeOuterStringSerializeWithHttpInfo($body = null)
885941
);
886942
}
887943

944+
$responseBody = $response->getBody();
945+
switch($statusCode) {
946+
case 200:
947+
if ('string' === '\SplFileObject') {
948+
$content = $responseBody; //stream goes to serializer
949+
} else {
950+
$content = $responseBody->getContents();
951+
if ('string' !== 'string') {
952+
$content = json_decode($content);
953+
}
954+
}
955+
956+
return [
957+
ObjectSerializer::deserialize($content, 'string', []),
958+
$response->getStatusCode(),
959+
$response->getHeaders()
960+
];
961+
}
962+
963+
$returnType = 'string';
888964
$responseBody = $response->getBody();
889965
if ($returnType === '\SplFileObject') {
890966
$content = $responseBody; //stream goes to serializer
@@ -1102,7 +1178,6 @@ public function testBodyWithQueryParams($query, $user)
11021178
*/
11031179
public function testBodyWithQueryParamsWithHttpInfo($query, $user)
11041180
{
1105-
$returnType = '';
11061181
$request = $this->testBodyWithQueryParamsRequest($query, $user);
11071182

11081183
try {
@@ -1336,7 +1411,6 @@ public function testClientModel($client)
13361411
*/
13371412
public function testClientModelWithHttpInfo($client)
13381413
{
1339-
$returnType = '\OpenAPI\Client\Model\Client';
13401414
$request = $this->testClientModelRequest($client);
13411415

13421416
try {
@@ -1367,6 +1441,26 @@ public function testClientModelWithHttpInfo($client)
13671441
);
13681442
}
13691443

1444+
$responseBody = $response->getBody();
1445+
switch($statusCode) {
1446+
case 200:
1447+
if ('\OpenAPI\Client\Model\Client' === '\SplFileObject') {
1448+
$content = $responseBody; //stream goes to serializer
1449+
} else {
1450+
$content = $responseBody->getContents();
1451+
if ('\OpenAPI\Client\Model\Client' !== 'string') {
1452+
$content = json_decode($content);
1453+
}
1454+
}
1455+
1456+
return [
1457+
ObjectSerializer::deserialize($content, '\OpenAPI\Client\Model\Client', []),
1458+
$response->getStatusCode(),
1459+
$response->getHeaders()
1460+
];
1461+
}
1462+
1463+
$returnType = '\OpenAPI\Client\Model\Client';
13701464
$responseBody = $response->getBody();
13711465
if ($returnType === '\SplFileObject') {
13721466
$content = $responseBody; //stream goes to serializer
@@ -1618,7 +1712,6 @@ public function testEndpointParameters($number, $double, $pattern_without_delimi
16181712
*/
16191713
public function testEndpointParametersWithHttpInfo($number, $double, $pattern_without_delimiter, $byte, $integer = null, $int32 = null, $int64 = null, $float = null, $string = null, $binary = null, $date = null, $date_time = null, $password = null, $callback = null)
16201714
{
1621-
$returnType = '';
16221715
$request = $this->testEndpointParametersRequest($number, $double, $pattern_without_delimiter, $byte, $integer, $int32, $int64, $float, $string, $binary, $date, $date_time, $password, $callback);
16231716

16241717
try {
@@ -2014,7 +2107,6 @@ public function testEnumParameters($enum_header_string_array = null, $enum_heade
20142107
*/
20152108
public function testEnumParametersWithHttpInfo($enum_header_string_array = null, $enum_header_string = '-efg', $enum_query_string_array = null, $enum_query_string = '-efg', $enum_query_integer = null, $enum_query_double = null, $enum_form_string_array = '$', $enum_form_string = '-efg')
20162109
{
2017-
$returnType = '';
20182110
$request = $this->testEnumParametersRequest($enum_header_string_array, $enum_header_string, $enum_query_string_array, $enum_query_string, $enum_query_integer, $enum_query_double, $enum_form_string_array, $enum_form_string);
20192111

20202112
try {
@@ -2284,7 +2376,6 @@ public function testInlineAdditionalProperties($request_body)
22842376
*/
22852377
public function testInlineAdditionalPropertiesWithHttpInfo($request_body)
22862378
{
2287-
$returnType = '';
22882379
$request = $this->testInlineAdditionalPropertiesRequest($request_body);
22892380

22902381
try {
@@ -2506,7 +2597,6 @@ public function testJsonFormData($param, $param2)
25062597
*/
25072598
public function testJsonFormDataWithHttpInfo($param, $param2)
25082599
{
2509-
$returnType = '';
25102600
$request = $this->testJsonFormDataRequest($param, $param2);
25112601

25122602
try {

samples/client/petstore/php/OpenAPIClient-php/lib/Api/FakeClassnameTags123Api.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ public function testClassname($client)
117117
*/
118118
public function testClassnameWithHttpInfo($client)
119119
{
120-
$returnType = '\OpenAPI\Client\Model\Client';
121120
$request = $this->testClassnameRequest($client);
122121

123122
try {
@@ -148,6 +147,26 @@ public function testClassnameWithHttpInfo($client)
148147
);
149148
}
150149

150+
$responseBody = $response->getBody();
151+
switch($statusCode) {
152+
case 200:
153+
if ('\OpenAPI\Client\Model\Client' === '\SplFileObject') {
154+
$content = $responseBody; //stream goes to serializer
155+
} else {
156+
$content = $responseBody->getContents();
157+
if ('\OpenAPI\Client\Model\Client' !== 'string') {
158+
$content = json_decode($content);
159+
}
160+
}
161+
162+
return [
163+
ObjectSerializer::deserialize($content, '\OpenAPI\Client\Model\Client', []),
164+
$response->getStatusCode(),
165+
$response->getHeaders()
166+
];
167+
}
168+
169+
$returnType = '\OpenAPI\Client\Model\Client';
151170
$responseBody = $response->getBody();
152171
if ($returnType === '\SplFileObject') {
153172
$content = $responseBody; //stream goes to serializer

0 commit comments

Comments
 (0)