Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

对整个项目接口生成时,存在部分生成了对应的operationId,但并没有生成对应的models的case;同时也存在大小写异常的case #81

Closed
BruceCham opened this issue Jan 26, 2024 · 11 comments

Comments

@BruceCham
Copy link

No description provided.

@BruceCham
Copy link
Author

lQLPJxRWtIUEjP_NA4DNB3Kw6h4iBI7F3eMFow0iFtlkAQ_1906_896
大小写异常的case


lQLPKG_32jGa_f_NBCbNBWCwZTaaDreQ3iAFow0iFtlkAA_1376_1062
model未生成的Case

@gogoyqj
Copy link
Owner

gogoyqj commented Jan 30, 2024

lQLPJxRWtIUEjP_NA4DNB3Kw6h4iBI7F3eMFow0iFtlkAQ_1906_896 大小写异常的case

lQLPKG_32jGa_f_NBCbNBWCwZTaaDreQ3iAFow0iFtlkAA_1376_1062 model未生成的Case

麻烦把出现问题的、最精简的yapi-swagger.json代码贴一下,我本地跑跑看为啥哈

@BruceCham
Copy link
Author

BruceCham commented Jan 30, 2024

@gogoyqj

最小可复现场景

"auto-service": "^4.0.9"
node v16.20.2
java --version openjdk 21.0.1

model丢失case

model丢失的case在v3中是正常的,只在v2中存在此case,但把无关的删除后,没有重现,暂时忽略吧

大小写异常 case

json2service.js

module.exports = {
  "url": "./swagger.json",
  "swaggerParser": {
    "-o": "src/services",
    // "-t": "plugins/types-only" // 存在大小写异常、model未生成的case
    "-t": "v3/plugins/typescript-tkit-autos" // 只存在大小写异常:与 types-only 是同一个文件异常,但命名规则不太一样
    // "-t": "v3/plugins/types-tkit" // 错误信息:Error: ENOENT: no such file or directory json 找不到
  },
  "guardConfig": {
    "mode": "safe"
  }
}

swagger.json

{
  "swagger": "2.0",
  "info": {
    "title": "unknown",
    "version": "last",
    "description": "unknown"
  },
  "tags": [
    {
      "name": "公共分类",
      "description": "公共分类"
    }
  ],
  "basePath": "",
  "schemes": [
    "http"
  ],
  "paths": {
    "/cungong/audit/auditsubmit": {
      "post": {
        "tags": [
          "公共分类"
        ],
        "summary": "提交",
        "description": "",
        "consumes": [
          "application/json"
        ],
        "parameters": [
          {
            "name": "CungongAuditAuditsubmitBody",
            "in": "body",
            "schema": {
              "type": "object",
              "properties": {
                "audit_district": {
                  "type": "integer",
                  "description": "id"
                },
                "is_confirm": {
                  "type": "integer",
                  "description": "1:确定"
                },
                "data": {
                  "type": "array",
                  "items": {
                    "type": "object",
                    "properties": {
                      "id": {
                        "type": "integer",
                        "description": "主键id"
                      },
                      "reason": {
                        "type": "array",
                        "items": {
                          "type": "integer",
                          "description": "reasonId"
                        }
                      },
                      "status": {
                        "type": "integer",
                        "description": "1:选中,2:非选中"
                      },
                      "is_test": {
                        "type": "number",
                        "description": "0:非测试数据 1:测试数据"
                      }
                    },
                    "required": [
                      "id",
                      "reason",
                      "status",
                      "is_test"
                    ]
                  }
                }
              },
              "required": [
                "audit_district",
                "data",
                "is_confirm"
              ]
            }
          }
        ],
        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "type": "object",
              "$schema": "http:https://json-schema.org/draft-04/schema#",
              "title": "CungongAuditAuditsubmit",
              "properties": {
                "errNo": {
                  "type": "integer"
                },
                "errstr": {
                  "type": "string"
                },
                "data": {
                  "type": "object",
                  "properties": {
                    "status": {
                      "type": "integer",
                      "description": "提交状态 1:成功,0失败"
                    },
                    "msg": {
                      "type": "string",
                      "description": "提示"
                    },
                    "list": {
                      "type": "array",
                      "items": {
                        "type": "integer",
                        "description": "审核数据id"
                      },
                      "description": "冲突的数据"
                    }
                  },
                  "required": [
                    "status",
                    "msg",
                    "list"
                  ]
                }
              },
              "required": [
                "errNo",
                "errstr",
                "data"
              ]
            }
          }
        },
        "operationId": "cungongAuditAuditsubmitPost"
      }
    }
  }
}

@BruceCham
Copy link
Author

@gogoyqj
还有个问题,生成的request参数,下划线格式自动变成了驼峰式,示例如下

request 参数 audit_district  
生成的文件中变成了 auditDistrict

@gogoyqj
Copy link
Owner

gogoyqj commented Feb 1, 2024

收到,我看看

@gogoyqj
Copy link
Owner

gogoyqj commented Feb 2, 2024

@BruceCham
json2service.js里的建议配置严格模式,safe其实是为了兼容直接基于operationId生产方法名、存在operationId不固定的bug

  "guardConfig": {
    "mode": "strict" // 默认即严格模式
  }

问题1:model缺失的问题

我这边确实暂也不能复现哈

问题2:大小写异常……

这个其实并不算异常,而是之前技术实现上的缺陷,我是在mustache模板里给参数名拼凑了前缀,所以不能保证一定遵循驼峰命名,这个目前没有办法解决,建议配置eslint或者tslint的ignore规则、直接将src/service目录ignore或者exclude

问题3:snake case变成camel case

json2service.js里配置了"-t": "v3/plugins/typescript-tkit-autos",意味着会使用openAPI v3的jar去转换代码,v3的jar会自动把snake case转换成camel case,PS:我好像忘记把v3的jar里这个默认设置给修正过来也没有提供可定制化的入口~这可能需要一段时间才能修正过来哈

因为你的swagger.json openAPI v2,可以不配置-t参数,这样就会使用v2 的jar去生成代码,如此就不会把snake case转换成camel case了;

@BruceCham
Copy link
Author

@gogoyqj model丢失的问题能复现了,好像是两个接口返回的一样时候,可能会造成另外一个没有生成

json2service.js

module.exports = {
  "url": "./swagger.json",
  "swaggerParser": {
    "-o": "src/test",
    "-t": "plugins/types-only"
  },
  "guardConfig": {
    "mode": "strict",
    "validUrlReg": /cungong/g
  }
}

swagger.json

{
  "swagger": "2.0",
  "info": {
    "title": "unknown",
    "version": "last",
    "description": "unknown"
  },
  "tags": [
    {
      "name": "cungong",
      "description": "cungongDesc"
    }
  ],
  "basePath": "",
  "schemes": [
    "http"
  ],
  "paths": {
    "/cungong/ai/marktask": {
      "post": {
        "tags": [
          "cungong"
        ],
        "summary": "任务标注",
        "description": "",
        "consumes": [
          "multipart/form-data"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "formData",
            "required": true,
            "description": "任务 ID",
            "type": "string"
          },
          {
            "name": "mark",
            "in": "formData",
            "required": true,
            "description": "1-正常 2-违规",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "$schema": "http:https://json-schema.org/draft-04/schema#",
              "type": "object",
              "properties": {
                "errNo": {
                  "type": "number"
                },
                "errstr": {
                  "type": "string"
                },
                "data": {
                  "type": "number",
                  "description": "记录 ID"
                }
              },
              "title": "CungongAiMarktask"
            }
          }
        },
        "operationId": "cungongAiMarktaskPost"
      }
    },
    "/cungong/ai/addtask": {
      "post": {
        "tags": [
          "cungong"
        ],
        "summary": "新增任务",
        "description": "",
        "consumes": [
          "multipart/form-data"
        ],
        "parameters": [
          {
            "name": "title",
            "in": "formData",
            "required": true,
            "description": "限制长度 200",
            "type": "string"
          },
          {
            "name": "demand",
            "in": "formData",
            "required": false,
            "description": "限制长度 200",
            "type": "string"
          },
          {
            "name": "word_count",
            "in": "formData",
            "required": true,
            "description": "字数范围:1:300-500 2:500-800 3:800+",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "$schema": "http:https://json-schema.org/draft-04/schema#",
              "type": "object",
              "properties": {
                "errNo": {
                  "type": "number"
                },
                "errstr": {
                  "type": "string"
                },
                "data": {
                  "type": "number",
                  "description": "记录 ID"
                }
              },
              "title": "CungongAiAddtask"
            }
          }
        },
        "operationId": "cungongAiAddtaskPost"
      }
    }
  }
}

@gogoyqj
Copy link
Owner

gogoyqj commented Feb 2, 2024

@gogoyqj model丢失的问题能复现了,好像是两个接口返回的一样时候,可能会造成另外一个没有生成

json2service.js

module.exports = {
  "url": "./swagger.json",
  "swaggerParser": {
    "-o": "src/test",
    "-t": "plugins/types-only"
  },
  "guardConfig": {
    "mode": "strict",
    "validUrlReg": /cungong/g
  }
}

swagger.json

{
  "swagger": "2.0",
  "info": {
    "title": "unknown",
    "version": "last",
    "description": "unknown"
  },
  "tags": [
    {
      "name": "cungong",
      "description": "cungongDesc"
    }
  ],
  "basePath": "",
  "schemes": [
    "http"
  ],
  "paths": {
    "/cungong/ai/marktask": {
      "post": {
        "tags": [
          "cungong"
        ],
        "summary": "任务标注",
        "description": "",
        "consumes": [
          "multipart/form-data"
        ],
        "parameters": [
          {
            "name": "id",
            "in": "formData",
            "required": true,
            "description": "任务 ID",
            "type": "string"
          },
          {
            "name": "mark",
            "in": "formData",
            "required": true,
            "description": "1-正常 2-违规",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "$schema": "http:https://json-schema.org/draft-04/schema#",
              "type": "object",
              "properties": {
                "errNo": {
                  "type": "number"
                },
                "errstr": {
                  "type": "string"
                },
                "data": {
                  "type": "number",
                  "description": "记录 ID"
                }
              },
              "title": "CungongAiMarktask"
            }
          }
        },
        "operationId": "cungongAiMarktaskPost"
      }
    },
    "/cungong/ai/addtask": {
      "post": {
        "tags": [
          "cungong"
        ],
        "summary": "新增任务",
        "description": "",
        "consumes": [
          "multipart/form-data"
        ],
        "parameters": [
          {
            "name": "title",
            "in": "formData",
            "required": true,
            "description": "限制长度 200",
            "type": "string"
          },
          {
            "name": "demand",
            "in": "formData",
            "required": false,
            "description": "限制长度 200",
            "type": "string"
          },
          {
            "name": "word_count",
            "in": "formData",
            "required": true,
            "description": "字数范围:1:300-500 2:500-800 3:800+",
            "type": "string"
          }
        ],
        "responses": {
          "200": {
            "description": "successful operation",
            "schema": {
              "$schema": "http:https://json-schema.org/draft-04/schema#",
              "type": "object",
              "properties": {
                "errNo": {
                  "type": "number"
                },
                "errstr": {
                  "type": "string"
                },
                "data": {
                  "type": "number",
                  "description": "记录 ID"
                }
              },
              "title": "CungongAiAddtask"
            }
          }
        },
        "operationId": "cungongAiAddtaskPost"
      }
    }
  }
}

这看起来像是 v2 jar的bug,我换成v3就可以了,如果只生成类型的话,可以只用v3(我猜测v2可能有一个合并相同类型的逻辑在)

{
"url": "./swagger.json",
  "swaggerParser": {
    "-o": "src/test",
    "-t": "v3/plugins/types-only"
  },
  "guardConfig": {
    "mode": "strict",
    "validUrlReg": /cungong/g
  }
}

image

@BruceCham
Copy link
Author

BruceCham commented Feb 2, 2024

@gogoyqj
在配置中加入如下内容,可以解决 v3 里面camel命名的问题。

{
   "url": "./swagger.json",
  "swaggerParser": {
    "-o": "src/test",
    "-t": "v3/plugins/types-only",
    "-DmodelPropertyNaming=snake_case": " "
  },
  "guardConfig": {
    "mode": "strict",
    "validUrlReg": /cungong/g
  }
}

但还有个问题一直存在:选择 v3/plugins/types-only 时,java generate 没有生成 tmp/*_json下的文件,一直报错,目前v3 types-only无法使用 ... #55

@gogoyqj
Copy link
Owner

gogoyqj commented Feb 4, 2024

@gogoyqj 在配置中加入如下内容,可以解决 v3 里面camel命名的问题。

{
   "url": "./swagger.json",
  "swaggerParser": {
    "-o": "src/test",
    "-t": "v3/plugins/types-only",
    "-DmodelPropertyNaming=snake_case": " "
  },
  "guardConfig": {
    "mode": "strict",
    "validUrlReg": /cungong/g
  }
}

但还有个问题一直存在:选择 v3/plugins/types-only 时,java generate 没有生成 tmp/*_json下的文件,一直报错,目前v3 types-only无法使用 ... #55

@BruceCham 为何我这边尝试是可以的(莫非是因为你snake_case的值多了一个空格?):

module.exports = {
  "url": "./swagger.json",
  "swaggerParser": {
    "-o": "src/test",
    "-t": "v3/plugins/types-only",
    "-DmodelPropertyNaming=snake_case": "",
  },
  "guardConfig": {
    "mode": "strict",
    "validUrlReg": /cungong/g
  }
}

image

@BruceCham
Copy link
Author

@gogoyqj #82 重新整理到新的issue了

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants