[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.
This commit is contained in:
Guillaume Turri 2025-05-09 23:46:48 +02:00
parent 36217ad7dc
commit 6e05331119

View File

@ -417,10 +417,16 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
op.vendorExtensions.put("x-comment-type", "void");
}
// Create a variable to add typing for return value of interface
if (op.returnType != null) {
op.vendorExtensions.put("x-return-type", "array|object|null");
} else {
if (op.returnType == null) {
op.vendorExtensions.put("x-return-type", "void");
} else if (op.isArray || op.isMap || isApplicationJsonOrApplicationXml(op)
|| !op.returnTypeIsPrimitive // it could make sense to remove it, but it would break retro-compatibility
) {
op.vendorExtensions.put("x-return-type", "array|object|null");
} else if ("binary".equals(op.returnProperty.dataFormat)) {
op.vendorExtensions.put("x-return-type", "mixed");
} else {
op.vendorExtensions.put("x-return-type", op.returnType);
}
// Add operation's authentication methods to whole interface
@ -438,6 +444,18 @@ public class PhpSymfonyServerCodegen extends AbstractPhpCodegen implements Codeg
return objs;
}
private boolean isApplicationJsonOrApplicationXml(CodegenOperation op) {
if (op.produces != null) {
for(Map<String, String> produce : op.produces) {
if ("application/json".equalsIgnoreCase(produce.get("mediaType"))
|| "application/xml".equalsIgnoreCase(produce.get("mediaType"))) {
return true;
}
}
}
return false;
}
@Override
public ModelsMap postProcessModels(ModelsMap objs) {
objs = super.postProcessModels(objs);