Skip to content

Commit 806b99e

Browse files
authored
[PHP][php-nextgen] List all possible return types (fix #17113) (#20335)
* [PHP][php-nextgen] List all possible return types (fix #17113) * Switch to LinkedHashSet
1 parent 6180fea commit 806b99e

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

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

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,7 @@
3434
import org.slf4j.LoggerFactory;
3535

3636
import java.io.File;
37-
import java.util.ArrayList;
38-
import java.util.Collections;
39-
import java.util.EnumSet;
40-
import java.util.List;
41-
import java.util.Map;
37+
import java.util.*;
4238

4339
public class PhpNextgenClientCodegen extends AbstractPhpCodegen {
4440
@SuppressWarnings("hiding")
@@ -189,14 +185,29 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
189185
objs = super.postProcessOperationsWithModels(objs, allModels);
190186
OperationMap operations = objs.getOperations();
191187
for (CodegenOperation operation : operations.getOperation()) {
192-
if (operation.returnType == null) {
188+
Set<String> phpReturnTypeOptions = new LinkedHashSet<>();
189+
Set<String> docReturnTypeOptions = new LinkedHashSet<>();
190+
191+
for (CodegenResponse response : operation.responses) {
192+
if (response.dataType != null) {
193+
String returnType = response.dataType;
194+
if (response.isArray || response.isMap) {
195+
// PHP does not understand array type hinting so we strip it
196+
// The phpdoc will still contain the array type hinting
197+
returnType = "array";
198+
}
199+
200+
phpReturnTypeOptions.add(returnType);
201+
docReturnTypeOptions.add(response.dataType);
202+
}
203+
}
204+
205+
if (phpReturnTypeOptions.isEmpty()) {
193206
operation.vendorExtensions.putIfAbsent("x-php-return-type", "void");
207+
operation.vendorExtensions.putIfAbsent("x-php-doc-return-type", "void");
194208
} else {
195-
if (operation.returnProperty.isContainer) { // array or map
196-
operation.vendorExtensions.putIfAbsent("x-php-return-type", "array");
197-
} else {
198-
operation.vendorExtensions.putIfAbsent("x-php-return-type", operation.returnType);
199-
}
209+
operation.vendorExtensions.putIfAbsent("x-php-return-type", String.join("|", phpReturnTypeOptions));
210+
operation.vendorExtensions.putIfAbsent("x-php-doc-return-type", String.join("|", docReturnTypeOptions));
200211
}
201212

202213
for (CodegenParameter param : operation.allParams) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ use {{invokerPackage}}\ObjectSerializer;
164164
*
165165
* @throws ApiException on non-2xx response or if the response body is not in the expected format
166166
* @throws InvalidArgumentException
167-
* @return {{#returnType}}{{#responses}}{{#dataType}}{{^-first}}|{{/-first}}{{/dataType}}{{{dataType}}}{{/responses}}{{/returnType}}{{^returnType}}void{{/returnType}}
167+
* @return {{{vendorExtensions.x-php-doc-return-type}}}
168168
{{#isDeprecated}}
169169
* @deprecated
170170
{{/isDeprecated}}

0 commit comments

Comments
 (0)