2

(招摇的新手)

在 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 本身,而不是类似 APIMatic 的替代品。

4

1 回答 1

4

实现AbstractJavaCodegen(或实现它的子类)并重载 postProcessOperations 函数以将前缀添加到操作(CodegenOperation 类的 operationId 属性)。有关构建和运行自定义代码生成的说明,请参阅制作您自己的代码生成模块

伪代码:

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;
    }
}
于 2017-03-28T06:16:24.603 回答