Skip to content

Commit 945ef23

Browse files
committed
[php Symfony] fix return type for non json/xml api
This fixes the generated returned type of controller methods for endpoint with a response declared like content: text/plain: schema: type: <boolean|string|integer|number> or for content: image/png: schema: type: string format: binary Without this commit the generated method *had to* return a value that matched "array|object|null", which does not work in this case. This commit makes it possible to return the proper type.
1 parent 36217ad commit 945ef23

File tree

1 file changed

+20
-2
lines changed

1 file changed

+20
-2
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -417,10 +417,16 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
417417
op.vendorExtensions.put("x-comment-type", "void");
418418
}
419419
// Create a variable to add typing for return value of interface
420-
if (op.returnType != null) {
420+
if (op.returnType == null) {
421+
op.vendorExtensions.put("x-return-type", "void");
422+
} else if (op.isArray || op.isMap || isApplicationJsonOrApplicationXml(op)
423+
|| !op.returnTypeIsPrimitive // it could make sense to remove it, but it would break retro-compatibility
424+
) {
421425
op.vendorExtensions.put("x-return-type", "array|object|null");
426+
} else if ("binary".equals(op.returnProperty.dataFormat)) {
427+
op.vendorExtensions.put("x-return-type", "mixed");
422428
} else {
423-
op.vendorExtensions.put("x-return-type", "void");
429+
op.vendorExtensions.put("x-return-type", op.returnType);
424430
}
425431

426432
// Add operation's authentication methods to whole interface
@@ -438,6 +444,18 @@ public OperationsMap postProcessOperationsWithModels(OperationsMap objs, List<Mo
438444
return objs;
439445
}
440446

447+
private boolean isApplicationJsonOrApplicationXml(CodegenOperation op) {
448+
if (op.produces != null) {
449+
for(Map<String, String> produce : op.produces) {
450+
String mediaType = produce.get("mediaType");
451+
if (isJsonMimeType(mediaType) || isXmlMimeType(mediaType)) {
452+
return true;
453+
}
454+
}
455+
}
456+
return false;
457+
}
458+
441459
@Override
442460
public ModelsMap postProcessModels(ModelsMap objs) {
443461
objs = super.postProcessModels(objs);

0 commit comments

Comments
 (0)