如何在 swagger codegen 中设置方法名称前缀?

How to set method name prefix in swagger codegen?

(Swagger 新手)

在swagger规范文件中,operationId是操作的名称,对应HTTP方法。

例如,

 "/pet/findByStatus": {
      "get": {
        "tags": [
          "pet"
        ],
        "summary": "Finds Pets by status",
        "description": "Multiple status values can be provided with comma separated strings",
        "operationId": "findPetsByStatus",

如上所示,operationId = findPetsByStatus。假设我想在我的 java 代码中为所有 get 操作生成一个前缀,前缀 = 'get_'.

例如,我希望 swagger codegen 生成与前缀为 'get_' 的 HTTP GET 方法对应的所有操作。具体来说,在上面,它可能会生成:get_findPetsByStatus.

有没有办法告诉 swagger codegen 给方法加上前缀?

请注意,我想使用 swagger-codegen 本身,而不是类似 APIMatic 的替代品。

实施 AbstractJavaCodegen (or a subclass that implements it) and overload the postProcessOperations function to prepend prefixes to operations (operationId property of the CodegenOperation class). See making-your-own-codegen-modules 以获取构建说明和 运行 自定义代码生成器。

伪代码:

public class MyCodegen extends AbstractJavaCodegen{ \or 
    [...]
    @Override
    public Map<String, Object> postProcessOperations(Map<String, Object> objs) {
        super.postProcessOperations(objs);
        Map<String, Object> operations = (Map<String, Object>) objs.get("operations");
        if (operations != null) {
            List<CodegenOperation> ops = (List<CodegenOperation>) operations.get("operation");
            for (CodegenOperation operation : ops) {
                if(operation.httpMethod.equals("GET"){
                    operation.operationId = "get_" + operation.operationId;
                }[...]
            }
        }
        return objs;
    }
}